6

i have two forms holds the same model attributes, since Yii2 generate the field id to be ModelName-fieldName so the field generated will be as follow:

<select name="Channel[channel]" class="form-control" id="channel-description">

i have tried to use fieldConfig in Activeform but it doesn't add the id to the field itself.

3 Answers3

6

You should simply use the third parameter of ActiveForm::field() :

$options : The additional configurations for the field object.

e.g. :

$form->field($model, 'channel', ['inputOptions' => ['id' => 'channel-description']])

Read more about ActiveForm::field().

But if you really want to add a prefix to all your fields ids, you should override ActiveForm.

soju
  • 25,111
  • 3
  • 68
  • 70
1

If you want save input id structure "{model}-{attribute}".

Use yii\helpers\Html::getInputId() for generate "{model}-{attribute}" input id and complete it with your custom prefix.

$form->field($model, 'name')->textInput(['id' => 'custom-' . Html::getInputId($model, 'name')])
0

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

<?= $form->field($searchModel, 'regId',[
            'selectors' => ['input' => '#company-vacancy-regId'],
            'inputOptions' => ['id' => 'company-vacancy-regId'],
        ])->widget()?>
David Buck
  • 3,752
  • 35
  • 31
  • 35