ActiveForm,字段中断验证的自定义 ID [英] ActiveForm, custom id for field broke validation

查看:21
本文介绍了ActiveForm,字段中断验证的自定义 ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Yii2 还很陌生,但遇到了下一个令人不快的问题.

I'm pretty new with Yii2 and have the next unpleasant issue.

我在同一页面上创建了两个表单,如

I created two forms at same page like

<?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']
    ]); ?>

我对两个表单字段使用相同的模型,例如

I use the same model for both form fields like

<?=  $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 绑定.

And here is the issue. In that case I have two forms with same scope of fields, with same names and same id. What for sure will be not valid for W3C. The another one issue that inspite of that fact that client side presubmit javascript validation for both forms work perfect. The typeahed widget works only for first set of fields since it bindid by 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);
            ?>

预输入功能完美,但在这种情况下验证失败,我的意思是它只是停止工作.

The typeahead works perfect, but in that case validation fails, I mean it just stop working.

所以问题是,如何调整验证脚本并使用唯一的表单 ID.

So the questuion is, how to make possible adjust validation script and use unique form id's.

推荐答案

来自 文档:

如果为 input 元素设置了自定义 id,则可能需要调整 $selectors 相应地.

If you set a custom id for the input element, you may need to adjust the $selectors accordingly.

对于您的情况:

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

这篇关于ActiveForm,字段中断验证的自定义 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆