For this reason, you should always perform server-side validation by calling yii\base\Model::validate(), as described in the previous subsections.
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.
Below are some examples: Note: By default, inline validators will not be applied if their associated attributes receive empty inputs or if they have already failed some validation rules.
If you want to make sure a rule is always applied, you may configure the skip On Empty and/or skip On Error properties to be A standalone validator is a class extending yii\validators\Validator or its child class.
You may write the validation rule like the following: Some validations can only be done on the server-side, because only the server has the necessary information.
For example, to validate if a username is unique or not, it is necessary to check the user table on the server-side. It will trigger an AJAX request in the background to validate the input while keeping the same user experience as the regular client-side validation.
You may configure these error messages like configuring other properties of validators in a validation rule.
When yii\base\Model::validate() is called, it will call two methods that you may override to customize the validation process: To validate attributes only when certain conditions apply, e.g.
validating email addresses), you may call the validate() method of the desired validator, like the following: .This should be done by overriding the yii\base\Model::rules() method.The following example shows how the validation rules for the Most validators have default error messages that will be added to the model being validated when its attributes fail the validation.Instead, they will process the values and save them back to the attributes being validated.
A complete processing of user input is shown in the following example code, which will ensure only integer values are stored in an attribute: for values that are not bound to any model.
When a user changes the value of an input field or submit the form, the client-side validation Java Script will be triggered.