Array validation

Hi there!

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:

['categoryIDs', 'each', 'rule' => ['validateCategory']],

and if so, which signature the validation function should have?

Thanks in advance

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 [







public function validateCategory($attribute, $params, $validator){


      $this->addError($attribute, 'Not an array');



   foreach($this->$attribute as $item){

      if(something wrong with the item){

         $this->addError($attribute, "Something wrong with item");