0

My custom validation is working, I've tested it, but the error message is not showing.

My annotation:

    [Required(ErrorMessage = "O campo DATA DE NASCIMENTO é obrigatório")]
    [DataValida( ErrorMessage = "O campo DATA DE NASCIMENTO é inválido")]
    public DateTime DataNascimento { get; set; }

My validation definition:

public class DataValida : ValidationAttribute
{        
 public override bool IsValid(object value)
 {

    DateTime dt;
    bool result = DateTime.TryParse(value.ToString(),out  dt);

    return result;
   }

}

my view:

 <div>
            <label for="tbNome">Data Nascimento:</label>
            <input id="tbDataNascimento" type="text" maxlength="10" placeholder="DD/MM/AAAA" asp-for="DataNascimento" class="form-control" />
            <span asp-validation-for="DataNascimento" class="text-danger"></span>
        </div>

The required validation displays the message but my custom validation doesn't.

enter image description here

obs: I've disabled client-side validation.

4
  • It will not show because you have not implemented client side validation for the custom attribute. See Commented Mar 1, 2018 at 13:12
  • @CodingYoshi I am not using client-side validation. Commented Mar 1, 2018 at 13:22
  • What is your issue? Do you want the message to show or NOT to show? Commented Mar 1, 2018 at 13:29
  • There's something that I should have included, I considered it at first but didn't include, it is the action. I was redirecting when I shouldn't. Commented Mar 5, 2018 at 13:29

2 Answers 2

1

I was redirecting when Errors occurred.

if(ModelState.IsValid)
{
}
else
{
    return RedirectToAction("action");  //this will cause error messages not to show

   return View(objectToPass); //This will work
}
Sign up to request clarification or add additional context in comments.

Comments

0

If your requirement is just to be sure that a valid date have been entered by end user, simply use DataTypeAtribute instead of reinventing the wheel:

[DataType(DataType.DateTime)]
public DateTime DataNascimento { get; set; }

8 Comments

I need it to be custom. I am validating birth dates I will extend this functionality further. Prevent under age people from registering and etc.
@DiegoAlves That seems like a business requirement that should be implemented in your business model, not inside a view model attribute..
@oscar business requirement can be impemented in both client and server. Why make a trip to the server if you can write js or custom attribute to enforce rules. But you should never write them only at the client; never trust the client.
In an asp.net mvc application Can you give a real-world example where I could put this validation.
@diegoalves does not matter if you follow DDD, you can still have those rules in javascript on the client.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.