Validate attribute for type array

In Yii 1.1. we could use the following validation rule to ensure an attribute is of type array:


['category_ids', 'type', 'type' => 'array', 'message' => '{attribute} contains an invalid value.'],

I use this to ensure checkboxes are sent as an array, e.g:


<input type="checkbox" name="Form[category_ids][]" value="1">

However in Yii2 I can not find an equivalent validator. Does anyone know how to do this?

There isn’t an array validator, however, there is a validator to ensure each item of the array is valid.

Each Validator

you can write a custom validator here is an example of your array validator




// custom rule

['category_ids', 'validateArray'],




// and your custom validator

public function validateArray($attribute, $params, $validator)

{

    if (!is_array($this->$attribute)) {

        $this->addError($attribute, 'The attribute must be array.');

    }

}