我对 Yii2 还很陌生,并且遇到了下一个不愉快的问题。
我在同一页面创建了两个表单,例如
<?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 验证都可以完美地工作。 typeahed 小部件仅适用于第一组字段,因为它通过 id 绑定。
如果我尝试通过小部件选项指定来覆盖元素 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);
?>
预输入工作完美,但在这种情况下验证失败,我的意思是它停止工作。
所以问题是,如何调整验证脚本并使用唯一的表单 ID。