ActiveForm, custom id for field broke validation

我的梦境 提交于 2019-12-22 00:17:11

问题


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

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.

If i try to override elements id by specify it by widgets options like

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

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


回答1:


From the docs:

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

For your case:

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


来源:https://stackoverflow.com/questions/28456775/activeform-custom-id-for-field-broke-validation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!