2015-10-18 7 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']], 
    ]); 

ध्यान में रखते हुए मैं: enter image description here

:

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

समस्या है, जब मैं के साथ "डी" पहला इनपुट में फ़ॉर्म सबमिट करेंगे, मैं हर "श्रेणी" इनपुट पर त्रुटियाँ हैं मैं क्या गलत करता हूँ?

+0

प्रत्येक वैधकर्ता विशिष्ट मॉडल विशेषता से जुड़े सरणी मानों को मान्य करता है। इसलिए यदि सरणी मानों में से कोई एक अमान्य मानी गई संपूर्ण विशेषता को मान्य नहीं करता है। दूसरे शब्दों में आपके पास ऐसी जानकारी नहीं है जो सरणी तत्व ने सत्यापन त्रुटि उत्पन्न की है। – aalgogiver

+0

@ एल्गोगिवर निश्चित रूप से वह जानकारी प्राप्त कर सकता है कि किस सरणी तत्व ने सत्यापन त्रुटि उत्पन्न की है। यह आसानी से संभव है: '$ view_model-> getErrors()'। –

+0

@ एल्गोगिवर तो यह ढांचे का सही व्यवहार है? –

उत्तर

2

आप प्रत्येक वैधता जानकारी का उपयोग कर सकते हैं जानकारी: यह सत्यापनकर्ता संस्करण 2.0.4 के बाद उपलब्ध है।

[ 
    // checks if every category ID is an integer 
    ['categoryIDs', 'each', 'rule' => ['integer']], 
] 

यह सत्यापनकर्ता केवल सरणी विशेषता के साथ काम करता है। यह मान्य करता है कि सरणी के प्रत्येक तत्व को एक निर्दिष्ट सत्यापन नियम द्वारा सफलतापूर्वक सत्यापित किया जा सकता है। उपरोक्त उदाहरण में, श्रेणी आईडी गुणों को एक सरणी मान लेना चाहिए और प्रत्येक सरणी तत्व पूर्णांक सत्यापन नियम द्वारा मान्य किया जाएगा।

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