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.

Diego Alves
  • 2,462
  • 3
  • 32
  • 65

2 Answers2

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
}
Diego Alves
  • 2,462
  • 3
  • 32
  • 65
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; }
Oscar
  • 13,594
  • 8
  • 47
  • 75
  • 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. – Diego Alves Mar 01 '18 at 13:17
  • 1
    @DiegoAlves That seems like a business requirement that should be implemented in your business model, not inside a view model attribute.. – Oscar Mar 01 '18 at 13:22
  • 1
    @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. – CodingYoshi Mar 01 '18 at 13:31
  • In an asp.net mvc application Can you give a real-world example where I could put this validation. – Diego Alves Mar 01 '18 at 13:35
  • Well you can provide the min and max date and html5 will take care of it. But if it was some other rule like you can only enter the letter `a` only: you would write some javascript to ensure this. But on the sever side in your business layer you will have this rule too, just in case it was forgotten on the client or js was disabled. – CodingYoshi Mar 01 '18 at 13:39
  • There is also `RemoteValidatio` that allows calling the server through ajax. The idea is to make sure the user has a pleasant experience. – CodingYoshi Mar 01 '18 at 13:41
  • @DiegoAlves Diego, if you follow domain driven design rules, then those bussines rules belongs to domain layer. Basically, it states that business rules must be kept in your domain layer. See this: https://blogs.msdn.microsoft.com/cesardelatorre/2010/03/25/our-brand-new-ddd-n-layered-net-4-0-architecture-guide-book-and-sample-app-in-codeplex/ – Oscar Mar 01 '18 at 14:17
  • 1
    @diegoalves does not matter if you follow DDD, you can still have those rules in javascript on the client. – CodingYoshi Mar 01 '18 at 14:32