2015-02-11 30 views
2

我很新與Yii2並有下一個令人不快的問題。ActiveForm,自定義ID爲現場打破驗證

我創建了兩種形式在同一頁面像

<?php $form = ActiveForm::begin([ 
     // make sure you set the id of the form 
     'id' => 'create', 
     'action' => $action, 
     'options' => ['enctype' => 'multipart/form-data'] 
    ]); ?> 

    <?php $form = ActiveForm::begin([ 
     // make sure you set the id of the form 
     'id' => 'update', 
     'action' => $action, 
     'options' => ['enctype' => 'multipart/form-data'] 
    ]); ?> 

我用的是同一型號兩種形式的領域,如

<?= $form->field($action_model, 'action_name',[ 
       'addon' => ['prepend' => ['content'=>$action_model->getAttributeLabel('action_mobile')]] 
      ])->widget(Typeahead::classname(), [ 
       'options' => ['placeholder' => $action_model->getAttributeLabel('action_placeholder')], 
        'pluginOptions' => ['highlight'=>true], 
        'dataset' => [ 
         [ 
         'local' => Json::encode($totalLookUp['action_lookUp']), 
         'limit' => 10 
         ] 
        ] 
       ])->label(false); 
      ?> 

這裏是問題。在這種情況下,我有兩個具有相同字段範圍的表單,具有相同的名稱和相同的ID。 W3C肯定無效。另一個問題,儘管客戶端預先提交這兩種形式的JavaScript驗證的事實工作完美。由於按id綁定,所以typeahed小部件僅適用於第一組字段。

,如果我嘗試重寫元素ID由小工具選項,如

<?= $form->field($action_model, 'action_name',[ 
       'addon' => ['prepend' => ['content'=>$action_model->getAttributeLabel('action_mobile')]] 
      ])->widget(Typeahead::classname(), [ 
       'options' => ['id'=> $form_id.'_action_name', 
'placeholder' => $action_model->getAttributeLabel('action_placeholder')], 
        'pluginOptions' => ['highlight'=>true], 
        'dataset' => [ 
         [ 
         'local' => Json::encode($totalLookUp['action_lookUp']), 
         'limit' => 10 
         ] 
        ] 
       ])->label(false); 
      ?> 

的預輸入完美的作品,但在這種情況下,驗證失敗,我的意思是它只是停止工作指定。

因此,questuion是,如何使可能的調整驗證腳本和使用獨特的形式ID的。

回答

2

docs

如果設置爲輸入元素的自定義id,您可能需要相應地調整$selectors

對於您的情況:

$form->field($action_model, 'action_name', [ 
    'selectors' => ['input' => '#'.$form_id.'_action_name'], 
    'addon' => ... 
+0

謝謝!完美的作品! – DevAnimal 2015-02-12 16:21:16