Skip to end of metadata
Go to start of metadata

Redirection Notice

Icon
Data validation is the process of ensuring that a program operates on clean, correct and useful data. Most .NET developers use Data Annotation Validators. But nopCommerce uses Fluent Validation. It's a small validation library for .NET that uses a fluent interface and lambda expressions for building validation rules for your business objects. You have to complete two steps in order to add a validation to some models in nopCommerce: 
1. Create a class derived from AbstractValidator class and put all required logic there. See the image below to get an idea:

   public class AddressValidator : AbstractValidator<AddressModel>
    {
        public AddressValidator(ILocalizationService localizationService)
        {
            RuleFor(x => x.FirstName)
                .NotEmpty()
                .WithMessage(localizationService.GetResource("Address.Fields.FirstName.Required"));
        }
    }


2. Annotate your model class with the ValidatorAttribute. Refer to the example below for guidance.

    [Validator(typeof(AddressValidator))]
    public class AddressModel : BaseNopEntityModel
    {


ASP.NET will execute the appropriate validator when a view model is posted to a controller.

  • No labels