Yii2. Form get Input Id

Eduardo picture Eduardo · Jul 11, 2016 · Viewed 12.4k times · Source

I am trying to the ID of a field in order to use it in js. I have read Yii2 documentation and found ActiveField has a method "getInputId", but how do I call it?

<?php $form = ActiveForm::begin(); ?>
        <?= $form->field($model, 'name')->textInput(['maxlength' => true]) ?>
<?php $form->end(); ?>

I can save the result from $form->field in a var, but it is a string so not possible to use it that way.

I have been checking ActiveForm code and I see it exists 2 methods: beginField and endField, maybe soemthing to do with that? any ideas will be appreciated.

Answer

Eduardo picture Eduardo · Jul 11, 2016

I have found a good solution. There is a method "getInputId" from Html helper. My original question is pending yet. How to use the method "getInputId" from activeField?

<?= Html::getInputId($model, 'phone'); ?>