Passing multi-dimensional array within Yii2 validation rules?

徘徊边缘 提交于 2019-12-14 02:35:12

问题


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?


回答1:


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']],
    ]
}


来源:https://stackoverflow.com/questions/32808190/passing-multi-dimensional-array-within-yii2-validation-rules

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!