The validation message contains '{PropertyName}' instead of the property name

I use free verification with unobtrusive client-side verification.

<fieldset class="edit-root-form"> <p> @Html.LabelFor(model => model.Login, UserRes.Login) @Html.TextBoxFor(model => model.Login) @Html.ValidationMessageFor(model => model.Login) </p> </fieldset> 

Free validation rule:

  this.RuleFor(x => x.Login).NotNull().EmailAddress() 

And I got the error message as follows: " {PropertyName} " should not be empty.

Created by html:

 <input data-val="true" data-val-regex="&amp;#39;{PropertyName}&amp;#39; is not a valid email address." data-val-required="&amp;#39;{PropertyName}&amp;#39; must not be empty." id="Login" name="Login" type="text" value="" class="input-validation-error"> 

Why won't MVC replace the name FileName ?

+4
source share
1 answer

Works great for me. I am using the latest version of FluentValidation (2.0.0.0) and ASP.NET MVC 3 RTM.

Model and validator:

 [Validator(typeof(MyViewModelValidator))] public class MyViewModel { public string Login { get; set; } } public class MyViewModelValidator : AbstractValidator<MyViewModel> { public MyViewModelValidator() { RuleFor(x => x.Login).NotNull().EmailAddress(); } } 

Controller:

 public class HomeController : Controller { public ActionResult Index() { return View(new MyViewModel()); } [HttpPost] public ActionResult Index(MyViewModel model) { return View(model); } } 

View:

 @model AppName.Models.MyViewModel <script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.LabelFor(model => model.Login, "Login") @Html.TextBoxFor(model => model.Login) @Html.ValidationMessageFor(model => model.Login) <input type="submit" value="OK" /> } 

Application_Start in Global.asax :

 protected void Application_Start() { AreaRegistration.RegisterAllAreas(); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); ModelValidatorProviders.Providers.Add(new FluentValidationModelValidatorProvider(new AttributedValidatorFactory())); } 

Two cases:

  • Leave the text field blank: 'Login' must not be empty. a verification error message is displayed.
  • Enter an invalid email address: 'Login' is not a valid email address. a verification error message is displayed.

And finally, the HTML for the text field is created here:

 <input data-val="true" data-val-regex="&amp;#39;Login&amp;#39; is not a valid email address." data-val-regex-pattern="^(?:[\w\!\#\$\%\&amp;\&#39;\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&amp;\&#39;\*\+\-\/\=\?\^\`\{\|\}\~] +@ (?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$" data-val-required="&amp;#39;Login&amp;#39; must not be empty." id="Login" name="Login" type="text" value="" /> 
+3
source

Source: https://habr.com/ru/post/1337750/


All Articles