Data Validation
Data validation is the process of ensuring that a program operates on clean and correct 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. To add validation for a model in nopCommerce, you need to do the following:
Create a class derived from the AbstractValidator
class and put all required logic there.
public partial class AddressValidator : BaseNopValidator<AddressModel>
{
public AddressValidator(ILocalizationService localizationService)
{
RuleFor(x => x.FirstName)
.NotEmpty()
.WithMessageAwait(localizationService.GetResourceAsync("AddressFields.FirstName.Required"));
}
}
ASP.NET Core will execute the appropriate validator when a view model is posted to a controller.