Besides using the core validators included in the Yii releases, you may also create your own validators.You may create inline validators or standalone validators.Similar to input data coming from end users, you should never trust client-side validation.For this reason, you should always perform server-side validation by calling yii\base\Model::validate(), as described in the previous subsections.An inline validator is one defined in terms of a model method or an anonymous function.The signature of the method/function is: If an attribute fails the validation, the method/function should call yii\base\Model::add Error() to save the error message in the model so that it can be retrieved back later to present to end users.For example, really work, you should declare validation rules for the attributes you plan to validate.This should be done by overriding the yii\base\Model::rules() method.
You may also access the dynamic attributes defined through the model instance, e.g., .
For example, If you also need to support client-side conditional validation, you should configure the when Client property which takes a string representing a Java Script function whose return value determines whether to apply the rule or not. As you can see, these validation rules do not really validate the inputs.
Instead, they will process the values and save them back to the attributes being validated.
You may implement its validation logic by overriding the yii\validators\Validator::validate Attribute() method.
If an attribute fails the validation, call yii\base\Model::add Error() to save the error message in the model, like you do with inline validators.Behind the scene, yii\widgets\Active Form will read the validation rules declared in the model and generate appropriate Java Script code for validators that support client-side validation.