1

I have read many topics on how to change the default error message language for form validation, but none are applicable to Net 8 with Razor pages (no MVC). My situation is a webapp in C# with razor pages and I have a simple form with two fields (name, surname) and they are mandatory, I would like the error message to be displayed in a language other than English.

Test.cs

namespace TestNamespace.Data
{
    public class Test
    {
        public string Nome { get; set; } = null!;
        public string Cognome { get; set; } = null!;
    }
}

Create.cshtml

@page
@model Test.Pages.Test.CreateModel

@{
    ViewData["Title"] = "Create";
}

<h1>Create</h1>

<h4>Test</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Test.Nome" class="control-label"></label>
                <input asp-for="Test.Nome" class="form-control" />
                <span asp-validation-for="Test.Nome" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Test.Cognome" class="control-label"></label>
                <input asp-for="Test.Cognome" class="form-control" />
                <span asp-validation-for="Test.Cognome" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

image of result

I also tried to create a Resources folder in the root containing a DataAnnotations.it.resx file, but this implies that I have to modify all the classes in the project. If I try to write the error message directly into the class, this works, but it is not a desired approach, because I would have to manually edit all the classes.

P.S.: Continuing with the tests, now I also have problems in managing the comma as a decimal separator, while in the validation of the field it generates an error asking me for the point as a decimal separator. The db field is set to decimal (6, 3)

1

1 Answer 1

1

how to change the default error message language for form validation, I try to write the error message directly into the class, this works, but it is not a desired approach, because I would have to manually edit all the classes

Firstly, using DataAnnotations Localization could help us to display different languages, but it still requires us to modify the data annotation attribute. Just like you test, we can definitely to set Required(ErrorMessage = "该字段必填") instead of Required(ErrorMessage = "This field is required") but it requires to change all properties in all model classes. What DataAnnotations Localization could help us is, defining a key-value pair in resx file such as RequiredErrorMessage: {0} 是必填的, then we replace the error message for Required attribute like Required(ErrorMessage = "RequiredErrorMessage"), then the validation result displayed in view will be the value we defined in resx file. See my test below.

enter image description here

This helped to save some of the work. And the DataAnnotation Localization requires us to add codes below in Program.cs

builder.Services.AddLocalization(o => o.ResourcesPath = "Resources");
builder.Services.AddMvc()
    .AddDataAnnotationsLocalization(options => {
        options.DataAnnotationLocalizerProvider = (type, factory) =>
            factory.Create(typeof(SharedResource));
        });

Here we used SharedResource which require us to create an empty SharedResource.cs file in the root folder of the project. Then since we defined the reource path to be Resources, we need to create a Resources in the root folder too, then let's create the resx file named SharedResource.resx which name is consistent with SharedResource.cs. Here's my project directory.

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

I had seen this solution in another post and I tried it, but it doesn't work. I will try it again following your instructions carefully. I'll keep you updated.
I tried your solution, but as I wrote above, I don't want to change all the classes, it would be a very long and inelegant job. I'm looking for a solution that acts globally on the data annotation language.
yes, just like what I mentioned above, it might reduce some work, but still requires to change all the classes

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.