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.
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'); ?>
By Default the id for he field is $model-$attribute
i.e
if you are using User
model and username
filed in the form i.e
<?= $form->field($model, 'username')->textInput(['maxlength' => true]) ?>
The id will be user-username
You can also specify the id manually for the field i.e.
<?= $form->field($model, 'username')->textInput(['maxlength' => true, 'id' => 'my_id']) ?>
in this case the id for the input will be my_id
Edit as per latest comment: (I have not tried this, but explained as per documentation)
as textInput
function is return $this
http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html#textInput()-detail
So getInputId
(http://www.yiiframework.com/doc-2.0/yii-widgets-activefield.html#getInputId()-detail)must be call able as
<?php
$form->field($model, 'username')->textInput(['maxlength' => true])->getInputId(); // this will not work
?>
BUT this is protected method as per documentation so will not be available to call outside class
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With