Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add id preix for field in activeForm?

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.

like image 988
git push origin master Avatar asked May 02 '16 07:05

git push origin master


Video Answer


2 Answers

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.

like image 82
soju Avatar answered Oct 11 '22 20:10

soju


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')])
like image 43
saimon47908 Avatar answered Oct 11 '22 20:10

saimon47908