Yii2 How do I implement a custom standalone validator? This paper mainly introduces the method of implementing custom independent validator in Yii2, and analyzes the implementation and usage of YII2 custom independent validator with instance form. Need friends can refer to, hope to help you.
The examples in this article describe how YII2 implements a custom independent validator. Share to everyone for your reference, as follows:
Create a new file:
<?phpnamespace common\helps;use yii\validators\validator;class Arrayvalidator extends Validator{public function ValidateAttribute ($model, $attribute) { if (!is_array ($model, $attribute)) { $this Adderror ($model, $attribute, $attribute. ' must be an array ');}}}
When used:
Public Function Rules () { return [ ... ] [' Kind_ids ', ' common\helps\arrayvalidator '],//custom validation ... ];}
Related recommendations:
Solve the problem of yii CFileCache getting no value
YII2 integrated Fast Search for efficient Chinese word search
How Yii Filters Bad code