DevAnimal
DevAnimal

Reputation: 379

ActiveForm, custom id for field broke validation

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.

Upvotes: 2

Views: 1175

Answers (1)

topher
topher

Reputation: 14860

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' => ...

Upvotes: 2

Related Questions