How To Make Active Form Fields Readonly In Update Form

hi i m saving form into db wat when i m again updating same records i want to keep some fields readonly so user cant change them ,i know how to handle from _form but how to handle from model or controller part

or easy way to handle in _form

i m justing checking before any field with if condition is it new record or not

how to do this yii2 in models rules section

array(‘date’, ‘readOnly’=>true, ‘on’=>‘update’),

I’m not sure but I think if can remove all rules for that attribute from rules and it’s doesn’t update.

also you can save value in temp variable in proper event, and write your own rule to reset attribute value.

is there any way to make field readonly in update like in yii 1 was some thing like


echo &#036;form-&gt;textField(



    array('readonly'=&gt;(&#036;model-&gt;scenario == 'update')? true : false)



it will make (tetxfiled) readonly when form is creating ,i want readonly when user try to update field .

when user inserting record first time then it should not be readonly ,but when record saved then user try to update then field should be readonly

i got solution

so just check inside textinput field ,is it newrecord

below is my code

<?= $form->field($model, ‘name’)->textInput([‘readonly’ => !$model->isNewRecord]) ?>

Nice solution.

but when i inspect element, and i deleted readonly.
i still can edit the value

this is wrong and wll turn the input readonly on the create and update both