我一整天都在做这个,这是我第一次在这里发帖,所以我希望有人可以帮助我。
我有一个表单,其中一个表单的多个记录是相同的模型。结果,我手动设置了name和id标签。我将以下代码放在foreach循环中,并在视图中生成标记。
// updatemember.php view has these declarations
use kartik\widgets\ActiveForm;
...
$form = ActiveForm::begin([
'id' => 'tab-horizontal',
'type' => ActiveForm::TYPE_HORIZONTAL,
'formConfig' => ['labelSpan' => 4 ],
'options' => ['enctype' => 'multipart/form-data'],
]
);
...
// updatemember calls this subform _claimcontact.php
use kartik\builder\Form;
...
echo Form::widget([
'model'=>$claimcontact,
'form'=>$form,
'columns'=>2,
'attributes'=>[
'phone_container'=>[
'label'=>'Phone',
'attributes'=>[
'phone'=>[
'type'=>Form::INPUT_TEXT,
'options'=>['name'=>"ClaimContact[$index][phone]", 'id'=>"claimcontact-phone_$index", ],
'container' => ['class'=>$col_width],
],
],
],
'email_container'=>[
'label'=>'Email',
'attributes'=>[
'email'=>[
'type'=>Form::INPUT_TEXT,
'options'=>['name'=>"ClaimContact[$index][email]", 'id'=>"claimcontact-email_$index", ],
'container' => ['class'=>$col_width],
],
],
],
],
]);
?>
</div>
</div>
</div>
</div>
我在ClaimContact模型中设置了一个规则,当我离开字段时,验证正确触发了错误消息和css,它正确地记录了记录3而不是记录2,所以这很好,因为电子邮件3是空白的和电子邮件2不是空白。
[['first_name', 'last_name', 'position', 'phone', 'email' ] , 'required',
不幸的是,当我单击“提交”按钮时,文件名验证将触发所有记录,包括填充的文件名字段。我附上了一张示例图片。
Invalid validation 似乎在源html页面底部生成的Yii验证脚本具有重复的标签ID和名称,这可能导致问题。
{"id":"claimcontact-email","name":"email","container":".field-claimcontact-email_0","input":"#claimcontact-email_0","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages,
{"message":"Email cannot be blank."});}},
...
{"id":"claimcontact-email","name":"email","container":".field-claimcontact-email_1","input":"#claimcontact-email_1","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages,
{"message":"Email cannot be blank."});}},
...
{"id":"claimcontact-email","name":"email","container":".field-claimcontact-email_2","input":"#claimcontact-email_2","validate":function (attribute, value, messages, deferred, $form) {yii.validation.required(value, messages,
{"message":"Email cannot be blank."});}},
我认为如果“id”标记分别设置为claimcontact-email_0,claimcontact-email_1和claimcontact-email_2,我打赌验证会有效。
我尝试过设置inputOptions和jquery选择器选项,但这不起作用。
我已经阅读并使用了多个模型文章(http://www.yiiframew...ple-models.html)并且它达到了一定程度,但我从另一个项目中遇到了很多问题所以我决定按照上述方法进行更多控制但是在我需要帮助的验证中有一点障碍,拜托。
感谢您抽出宝贵时间阅读我的问题。
答案 0 :(得分:0)
我找到了解决方案。需要使用$ index设置名称,然后创建的验证是唯一的。
例如
'email_container'=>[
'label'=>'Email',
'attributes'=>[
"[$index]email"=>[
'type'=>Form::INPUT_TEXT,
'options'=>['name'=>"ClaimContact[$index][email]", 'id'=>"claimcontact-email_$index", ],
'container' => ['class'=>$col_width],
],
],
],
注意:“[$ index] email”
生成此
{
"id":"claimcontact-0-email",
"name":"[0]email",
"container":".field-claimcontact-email_0",
"input":"#claimcontact-email_0","validate":function (attribute, value, messages, deferred, $form) {
...
(attribute, value)) { yii.validation.required(value, messages, {"message":"Email cannot be blank."});
}
{
"id":"claimcontact-1-email",
"name":"[1]email","container":
".field-claimcontact-email_1",
"input":"#claimcontact-email_1","validate":function (attribute, value, messages, deferred, $form) {
...
(attribute, value)) { yii.validation.required(value, messages, {"message":"Email cannot be blank."});
}
然后只在一个字段上产生我想要的验证。