2015-10-18 47 views
6

我具有以下數據:yii2驗證的輸入陣列

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 
     ) 
) 

我創建DynamicModel爲它的規則「各值應爲整數」(在2.0.4添加)。

$view_model = DynamicModel::validateData(compact('category', 'volume', 'urgency', 'importance'), [ 
     [['category', 'volume', 'urgency', 'importance'], 'each', 'rule' => ['integer']], 
    ]); 

鑑於我有:

<?= $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」,我對每個 「類別」 輸入錯誤: enter image description here

我做錯了什麼?

+0

每個驗證程序驗證與特定模型屬性關聯的數組值。 所以如果其中一個數組值沒有驗證整個屬性被認爲是無效的。 換句話說,你沒有信息哪個數組元素導致了驗證錯誤。 – aalgogiver

+0

@aalgogiver當然他可以獲得有關哪個數組元素導致驗證錯誤的信息。這很容易通過:'$ view_model-> getErrors()'。 –

+0

@aalgogiver所以這是框架的正確行爲? –

回答

2

您可以使用每個驗證器 信息:該驗證器自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. 
+0

我已經使用它了,如果你仔細閱讀我的問題。 –

0

這個答案是適用於Ajax請求的情況

在你的控制器

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(); ?>