yii2 Validate array


Example

Since Yii2 version 2.0.4 there is the EachValidator used to validate each item in an array.

[
    // ... other rules
    ['userIDs', 'each', 'rule' => ['integer']],
]

The ['integer'] part can be every other validator object that Yii2 offers and can hold the specific arguments for the validator. Like: ['integer', 'min' => 1337]. If the userIDs doesn't contain an array the rule validation will fail.

If you just want to see if an attribute contains an array without validating the contents you can write your own validator.

[
    ['myAttr', function($attribute, $params) {
        if (!is_array($this->$attribute)) {
            $this->addError($attribute, "$attribute isn't an array!");
        }
    }]
]