Module Validation under certain condition yii2
-
I want to test the field for uniqueness only if it's available, how am I supposed to put it in the rules? I'm doing this.
public function rules() { return [ ['checked_box', 'unique', 'on' => 'Только при наличии этого поля в модели' ], ]; }
How am I supposed to write a simple condition?
-
In default if the field is empty, the rule does not apply ( http://www.yiiframework.com/doc-2.0/yii-validators-validator.html#$skipOnEmpty-detail other question as he checks, empty field or not. You can redesign the method of checking the void. http://www.yiiframework.com/doc-2.0/yii-validators-validator.html#$isEmpty-detail And watch https://github.com/yiisoft/yii2/blob/master/framework/validators/Validator.php#L372
public function rules() { return [ [ 'checked_box', 'unique', 'isEmpty'=>function($value){ return isset($value); }, 'on' => 'Только при наличии этого поля в модели' ], ]; }