我具有以下數據: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」,我對每個 「類別」 輸入錯誤:
我做錯了什麼?
每個驗證程序驗證與特定模型屬性關聯的數組值。 所以如果其中一個數組值沒有驗證整個屬性被認爲是無效的。 換句話說,你沒有信息哪個數組元素導致了驗證錯誤。 – aalgogiver
@aalgogiver當然他可以獲得有關哪個數組元素導致驗證錯誤的信息。這很容易通過:'$ view_model-> getErrors()'。 –
@aalgogiver所以這是框架的正確行爲? –