Brett
Brett

Reputation: 20049

Passing multi-dimensional array within Yii2 validation rules?

When you are using Yii2's validation rules within a model, for example:

[['foo','bar'], 'integer],

Obviously ['foo','bar'] is an array, which I know you can use.

But can do pass a multi-dimensional array like this:

$this->numbers = [1,2,3];

[['foo','bar','numbers'], 'integer]

Will Yii2 accept this and check the correct data or will it test the value and return an error because numbers is an array?

Upvotes: 1

Views: 1471

Answers (1)

Onedev_Link
Onedev_Link

Reputation: 2012

You need merge arrays for work rules

[ArrayHelper::merge(['foo','bar'], $this->getNumberFields()), 'integer']

Update:

Use each rule. See EachValidator.

public function rules()
{
    return [
        ['numbers', 'each', 'rule' => ['integer']],
    ]
}

Upvotes: 3

Related Questions