Rename error message

Asked

Viewed 88 times

1

Follows 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" must be a date.

How can I rename this notice?

Ex:

The "Date" field must be a date.

2 answers

4

The machine’s settings should be in English, so it would. If you cannot do this at least the language needs to be installed, and you need to configure 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."

Aba com edição de arquivo de recurso

If you still want to customize the message have some paths, one of them:

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

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

Other article.

It is possible to do via library on client, maybe using jQuery if that’s the case you are using. you will need isntsalr components for this:

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

There’s a boy who answered this here, see if you can figure out what you want.

I put in the Github for future reference.

  • @Matheusmiranda I edited.

  • I made it myself :), what I want is just rename warning.

1

I managed to solve it that way:

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

Or you could try it that 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))
})
  • It doesn’t solve the same problem, it solves another. To solve this I would have answered that.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.