Join us this Friday for AI in Action at the Virtual Happy Hour! This free virtual event is open to all—enroll now on Academy and don’t miss out.

 

Model form validation localization errormessage

Vote:
 

Hi 

I am trying to show localized error messages. I dont want hardcoded messages in my model. I have removed code to keep it simple.

My model

    public class BankIdNorModel : PageViewModel
    {
        public BankIdNorModel(LoginPage currentPage) : base(currentPage)
        {
        }

        [Required(ErrorMessage = "I want to get this text from my xml language files like this /formerror/ssnrequired")]
        [DataType(DataType.PhoneNumber)]
        public string Ssn { get; set; }

    }

My view

@using (Html.BeginForm())
{
    @Html.ValidationSummary()
    @Html.AntiForgeryToken()

    @Html.EditorFor(m => m.Ssn, new { htmlAttributes = new { @class = "form-control" } })
    
}

I guess i have to use ErrorMessageResourceType and ErrorMessageResourceName but how do i do that?

Cheers Øyvind

#145091
Feb 24, 2016 14:18
Vote:
 

You can use @Html.ValidationMessageFor, e.g @Html.ValidationMessageFor(m => m.Ssn, Html.Translate("/formerror/ssnrequired"))

#145093
Feb 24, 2016 15:10
Vote:
 

The suggestion from Tim works, but the error messages are always rendered with <span class="field-validation-valid"></span> or <span class="field-validation-error"></span>. This was confusing for me at least. :)

#197674
Oct 10, 2018 10:31
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.