yii2验证输入数组

yii2验证输入数组,yii2,Yii2,我有以下资料: Array ( [category] => Array ( [0] => d [1] => 100 [2] => 100 [3] => 100 ) [volume] => Array ( [0] => 100 [1] => 1

我有以下资料:

Array
(
    [category] => Array
        (
            [0] => d
            [1] => 100
            [2] => 100
            [3] => 100
        )

    [volume] => Array
        (
            [0] => 100
            [1] => 100
            [2] => 100
        )

    [urgency] => Array
        (
            [0] => 100
            [1] => 100
            [2] => 100
        )

    [importance] => Array
        (
            [0] => 100
            [1] => 100
            [2] => 100
        )
)
我用规则“每个值都应该是整数”(在2.0.4中添加)为它创建了DynamicModel

我认为:

    <?= $form->field($model, 'category[0]')->textInput() ?>
    <?= $form->field($model, 'category[1]')->textInput() ?>
    <?= $form->field($model, 'category[2]')->textInput() ?>
    ...
    <?= $form->field($model, 'importance[2]')->textInput() ?>

...
问题是,当我提交第一次输入为“d”的表格时,我在每个“类别”输入上都有错误:

我做错了什么?

您可以使用每个验证器 信息:此验证器从2.0.4版开始可用

[
    // checks if every category ID is an integer
    ['categoryIDs', 'each', 'rule' => ['integer']],
]
此验证器仅适用于数组属性。它验证数组的每个元素是否可以通过指定的验证规则成功验证。在上面的示例中,categoryIDs属性必须采用数组值,并且每个数组元素都将通过整数验证规则进行验证

rule: an array specifying a validation rule. The first element in the array specifies the class name or the alias of the validator. The rest of the name-value pairs in the array are used to configure the validator object.
allowMessageFromRule: whether to use the error message returned by the embedded validation rule. Defaults to true. If false, it will use message as the error message.

Note: If the attribute value is not an array, it is considered validation fails and the message will be returned as the error message.

此答案适用于ajax请求场景

在控制器中

在你看来



每个验证器都验证与特定模型属性关联的数组值。因此,如果其中一个数组值未验证整个属性,则认为该属性无效。换句话说,您没有关于哪个数组元素导致了验证错误的信息。@aalgogiver当然可以获得关于哪个数组元素导致了验证错误的信息。通过:
$view\u model->getErrors()
@aalgogiver可以很容易地实现这一点,所以这是框架的正确行为?是的,错误属于属性。例如,如果您有一个模型数组,则每个模型在id字段上都可能有自己的错误。@aalgogiver您的意思是:?
rule: an array specifying a validation rule. The first element in the array specifies the class name or the alias of the validator. The rest of the name-value pairs in the array are used to configure the validator object.
allowMessageFromRule: whether to use the error message returned by the embedded validation rule. Defaults to true. If false, it will use message as the error message.

Note: If the attribute value is not an array, it is considered validation fails and the message will be returned as the error message.
use yii\base\Model;
use yii\widgets\ActiveForm;
use yii\web\Response;

public function actionCreate()
{
    $models = [
        'model1' => new Category, 
        'model2' => new Category, 
    ];

    if (Yii::$app->request->isAjax && Model::loadMultiple($models, Yii::$app->request->post())) {
        Yii::$app->response->format = Response::FORMAT_JSON;

        $validate = [];
        $validate = array_merge(ActiveForm::validateMultiple($models), $validate);
        // If you need to validate another models, put below.
        // $validate = array_merge(ActiveForm::validate($anotherModel), $validate);

        return $validate;
    }

    if (Model::loadMultiple($models, Yii::$app->request->post())) {
        foreach($models as $key => $model) {
            $model->save();
        }
    }

    return $this->render('create', [
        'models' => $models,
    ]);
}
<?php 
    $form = ActiveForm::begin([
        'enableClientValidation' => false, 
        'enableAjaxValidation' => true, 
    ]);
?>

<?= $form->field($models['model1'], '[model1]name')->textInput(); ?>
<?= $form->field($models['model2'], '[model2]name')->textInput(); ?>

<?= Html::submitButton('Create') ?>

<?php ActiveForm::end(); ?>