Client and server side validation in an ASP.NET MVC application

We are launching a new ASP.NET 3.5 MVC application. The following are requirements for verification:

  • Verification on the client and server side.
  • Verification rules in one place.
  • Common scenarios such as Password and Password Confirmation are considered.

Options:

xVal and 'Validation Library' can use the DataAnnotation plugin and jQuery validation.

If the form has a field that is required for Create but not required for Refresh, which of these frameworks can handle this script?

Please advise which one would be the best choice for checking MVC clients and server?

Thanks.

+4
source share
1 answer

I can answer the question of others :)

FluentValidation looks interesting. They provide free syntax, for example:

public class CustomerValidator: AbstractValidator<Customer> { public CustomerValidator() { RuleFor(customer => customer.Surname).NotEmpty(); RuleFor(customer => customer.Forename).NotEmpty().WithMessage("Missing first name"); } } 

It also has a little integration with ASP.NET MVC, where you can add the test result to ModelState, as shown below:

 public ActionResult Save(Customer customer) { var validator = new CustomerValidator(); var results = validator.Validate(customer); results.AddToModelState(ModelState, "customer"); } 
+4
source

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


All Articles