Quantcast
Channel: Fluent Validation for .NET
Viewing all articles
Browse latest Browse all 1917

New Post: FluentValidation not working on form submit when JavaScrip is disabled

$
0
0
Model
publicclass UserViewModel {
    public String Email { get; set; }
    public String PhoneNumber { get; set; }
    public String FullName { get; set; }
    public String CompanyName { get; set; }
}
Validation Class
publicclass UserViewModelValidation: AbstractValidator<UserViewModel> {
    public UserViewModelValidation() {
        RuleFor(x => x.Email).NotNull().WithLocalizedMessage(() => ValidationResources.EmailRequired);
        RuleFor(x => x.Email).EmailAddress().WithLocalizedMessage(() => ValidationResources.EmailValid);
        RuleFor(x => x.PhoneNumber).NotNull().WithLocalizedMessage(() => ValidationResources.PhoneNumberRequired);
        RuleFor(x => x.FullName).NotNull().WithLocalizedMessage(() => ValidationResources.FullNameRequired);
        RuleFor(x => x.CompanyName).NotNull().WithLocalizedMessage(() => ValidationResources.CompanyNameRequired);
    }
}
Registration in Global.asax
FluentValidationModelValidatorProvider.Configure();
Controller
[HttpPost]
public ActionResult UpdateUser(UserViewModel vm) {
    if (!ModelState.IsValid)
        return View(vm);

    var service = DiConfig.Resolve<IUserService>();
    service.Update(vm);
    return RedirectToAction("Index");
}
View
<formaction="@Url.Action("UpdateUser", "User")">
    @Html.ValidationSummary(true)
    <divclass="form-input">
        @Html.LabelFor(m => m.Email, FormResources.Email)
        @Html.TextBoxFor(m => m.Email)
        <divclass="error-message">@Html.ValidationMessageFor(m => m.Email)</div></div><divclass="form-input">
        @Html.LabelFor(m => m.PhoneNumber, FormResources.PhoneNumber)
        @Html.TextBoxFor(m => m.PhoneNumber)
        <divclass="error-message">@Html.ValidationMessageFor(m => m.PhoneNumber)</div></div><divclass="form-input">
        @Html.LabelFor(m => m.FullName, FormResources.FullName)
        @Html.TextBoxFor(m => m.FullName)
        <divclass="error-message">@Html.ValidationMessageFor(m => m.FullName)</div></div><divclass="form-input">
        @Html.LabelFor(m => m.CompanyName, FormResources.CompanyName)
        @Html.TextBoxFor(m => m.CompanyName)
        <divclass="error-message">@Html.ValidationMessageFor(m => m.CompanyName)</div></div><divclass="form-input"><inputtype="submit"value="@FormResources.UpdateButton"></div></form>
Problem
Client side validation works perfectly. No issue here. However, I've tried to make sure that the validation will work even when the JS is disabled on the browser. What I've expected was to see the error messages after page reload when I submit the form, without client side validations.

However, the result is - no validation messages at all, but only query string params in the URL with submitted values.


Question
How can I make validation work when JavaScript disabled? What I need is (when validation fails) showing the validation messages after the post and page reload.

Viewing all articles
Browse latest Browse all 1917

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>