I am using the each validator to validate each item of an array, in the docs is explained how to declare a validation rule that would be applied for each array item like this:
[
// checks if every category ID is an integer
['categoryIDs', 'each', 'rule' => ['integer']],
]
My question is: can I declare a custom validation function such as:
Thank you for your quick response, I was looking for something that could validate an array whereas in your example is a unique validation performed on a scalar value not a set of values. I believe there is not such feature on Yii2 but I can validate an array as well just doing like this:
...
public function rules() {
return [
....
['categoryIDs','validateCategory'],
....
];
}
...
public function validateCategory($attribute, $params, $validator){
if(!is_array($this->$attribute){
$this->addError($attribute, 'Not an array');
return;
}
foreach($this->$attribute as $item){
if(something wrong with the item){
$this->addError($attribute, "Something wrong with item");
break;
}
}
}