Original link: http://docs.nopcommerce.com/display/nc/Data+Validation
Translation By:yersans, if there is inappropriate, please correct me, bo main mailbox: [email protected]. Reprint please indicate the source.
Data validation refers to the processing of data in a program's operations to ensure that it is clean, correct, and useful. Most. NET developers use data annotation validation. But Nopcommerce is using Fluent Validation. It's a small one. NET validation libraries, using the fluent interface and lambda expressions to generate validation rules for business objects. To add a validation to the Nopcommerce model, you need to complete the following two steps:
1. Create a Abstractvalidator derived class that contains all the validation logic. As shown below:
public class addressvalidator:abstractvalidator<addressmodel> {public addressvalidator ( Ilocalizationservice localizationservice) { rulefor (x = x.firstname) . Notempty () . Withmessage (Localizationservice.getresource ("Address.Fields.FirstName.Required")); } }
2. Mark your model class with a validation attribute. As shown below:
[Validator (typeof (Addressvalidator))] public class Addressmodel:basenopentitymodel {
When the view model is passed to the controller, ASP. NET will be properly validated.
Nopcommerce Developer's Guide (vi): Data validation