Rename error message

1

Next model :

[DisplayName("Data:")]
[ValueParser("ptdateparser")]
[AssertThat("DeadLine > Today()", ErrorMessage = "* Data deverá ser superior a data de hoje")]
[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/mm/yyyy}")]
[Required(ErrorMessage = "* Campo \"Data\" é obrigatório")]

I have the field that validates the date, I get the following warning:

  

The field "Data" should be a date.

How can I rename this warning?

Ex:

  

The "Date" field must be a date.

    
asked by anonymous 12.07.2018 / 01:27

2 answers

4

The machine's settings should be in Portuguese, so it would solve. If you can not do this at least the language needs to be installed, and you need to configure it to use our messages:

Thread.CurrentThread.CurrentUICulture = new CultureInfo("pt-BR");

Or even configure:

<system.web>
   <globalization responseEncoding="UTF-8"
                  requestEncoding="UTF-8"
                  culture="pt-BR"
                  uiCulture ="pt-BR"
                  enableClientBasedCulture="true" />
   .....
</system.web>

You can still use a new resource file by configuring the Global.asax section Application_start :

ClientDataTypeModelValidatorProvider.ResourceClassKey = "PtbrResources";

2     DefaultModelBinder.ResourceClassKey="PtbrResources";

Then create a PtbrResources.resx with the error messages you want. look in the default file for 'FieldMustBeDate The field {0} must be a date.'

Ifyoustillwanttocustomizethemessageyouhavesomepaths,oneofthem:

[DataType(DataType.Date,ErrorMessage="O campo \"Data\" deve ser uma data")]

A more complete and programmatic solution can be found in a blog from Microsoft .

Another article .

It is possible to do via library on the client, maybe using jQuery if it is the case that is using. you'll need to list the components for this:

PM> Install-Package jQuery.Validation.Globalize
PM> Install-Package jquery-globalize

There is a guy who has answered this here , see if he solves what he wants.

    
12.07.2018 / 05:20
1

I was able to solve it this way:

@Html.TextBoxFor(model => model.Date, new { @class = "form-control", data_val_date = "Personaliza a sua mensagem aqui !"})

Or you can try it this way:

@Html.TextBoxFor(model => model.Date, new
{
    @class = "form-control",
    data_val_date = String.Format("O campo '{0}' deve ser uma data válida, verifique.",  
                                    Html.DisplayNameFor(model => model.Date))
})
    
12.07.2018 / 18:37