Yii2: Active Form readonly

Created on 29 Jul 2014  路  15Comments  路  Source: yiisoft/yii2

Is there a way to make an activeform item readonly? Make it have the same presentation as when it's normal, but have the input disable?

Something like this: http://demos.krajee.com/widget-details/select2 the disabled one.

Most helpful comment

first options are for the ActiveField class. Do it this way:

<?= $form->field($model, 'username')->textInput(['readonly' => true]) ?>

All 15 comments

Sure. In HTML it's <input type="text" readonly> so you need to pass this option:

<?= $form->field($model, 'username', ['readonly' => true]) ?>

I had tried that, I get an exception:

Setting unknown property: yii\widgets\ActiveField::readonly

field($company, 'dot_com_name', ['readonly' => true]) ?>

first options are for the ActiveField class. Do it this way:

<?= $form->field($model, 'username')->textInput(['readonly' => true]) ?>

Thanks, that was it, I had tried the 1st option, didn't occur to me that way. :smile:

The last solution was helpful.

why is it not working in my case? I put readonly but it is STILL editable.

    echo $form->field($model, 'user_id')->widget(Select2::classname(), [
        'data' => $listData,
        'readonly' => true,
        'options' => ['placeholder' => 'Select a Staff', 'readonly',],
        'pluginOptions' => [
            'readonly' => true,
            'allowClear' => true,
        ],
    ])->label('Employee Name');

I think because it's widget (not standard field), it should have special option for that.

kaynewilder, may be because "readonly" has to be a key in options. Like this:

'options' => ['placeholder' => 'Select a Staff', 'readonly'=>true,],

Try put disabled :
'pluginOptions' => [
'allowClear' => true,
],
'disabled' => 'disabled'

Disabled and readonly are differing things.
Disabled element will not return on submit.

Opss..I try submit form and my element return perfectly when i use disabled!!

solution below:
<?= $form->field($model, 'yourtext')->textInput(['readonly'=>true]) ?>

If I have a dropdown list, how to make it readonly?
<?= $form->field($model, 'featured_listings')->dropDownList([1=>'Yes',0=>'No']; ?>

@PaheerathanSelvarasa the readonly attribute don't works on a select element. It's a HTML problem.

To solve problems like this, I use one of the following approach on my projects:

  • Pass a array with only the selected key/item in the $items argument;
  • You can disable the non selected options;
  • Alternatively, since user cannot change the input, you can simply display the value as text.

@berosoboy Thanks, it was helpful!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

njasm picture njasm  路  44Comments

schmunk42 picture schmunk42  路  47Comments

SamMousa picture SamMousa  路  55Comments

samdark picture samdark  路  52Comments

cebe picture cebe  路  53Comments