0

I have a custom validation that is not evaluating, apparentlly. My validation validates dates but when I submit an invalid date, say 02/29/2017 (invalid not leap year) I get a message saying that the string is not valid for the object property. This is not good because in my country we speak portuguese and the English message really gets in the way, besides, I want to perform my custom validation.

    [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

   public class DataValida : ValidationAttribute
    {


        public override bool IsValid(object value)
        {

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



            return resultado;
        }
    }

Where does this message comes from?

enter image description here

Diego Alves
  • 2,462
  • 3
  • 32
  • 65
  • The `jquery.validate.js` plugin validates dates in `MM/dd/yyyy` format by default (therefore `29/02/2017` is not valid). You need to override the `$.validator` if you want to validate dates in a different culture. –  Mar 01 '18 at 21:44
  • Refer [this answer](https://stackoverflow.com/questions/39677035/date-of-birth-validation-keeps-showing/39682410#39682410) for an example –  Mar 01 '18 at 21:49

1 Answers1

0

You should not need a validator specifically for your language (the one you have specified will probably not work on a server as they are generally set to EN-US and UTC).

The basic (not simple) answer is that you need to provide resources that contain the messages

Take a look at this page: http://www.codiply.com/blog/localization-and-custom-validation-in-asp-net-mvc/

Neil
  • 11,059
  • 3
  • 31
  • 56