M
M
Michael2021-08-27 18:24:04
Yii
Michael, 2021-08-27 18:24:04

How to block input for one of the views?

Hello! Can you please tell me how it would be correct to write a condition in ActiveForm so that in the form of create the input field has the disabled status, and for the update types the field has already been unlocked?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vilinyh, 2021-08-27
@Bally

If you are editing an ActiveRecord, then it has a "property" isNewRecord:

echo $form->field($model, 'attributeName')->input('text', ['disabled' => $model->isNewRecord]);

If you are editing some of your Model, you will have to distinguish the new model from the existing one yourself by adding the necessary methods to it:
class MyModel extends Model {
    // ...
    public function getIsNew(): bool
    {
        // some logic behind it
    }
}

echo $form->field($model, 'attributeName')->input('text', ['disabled' => $model->isNew]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question