How to disable update field in Yii

I want to disable or make the readOnly field when the user will update, i.e. username. When the registered user updates their information, they will see that the username is disabled. I tried based on this answer, but this does not work for me, but gives an error. User has an invalid validation rule. The rule must specify attributes to be validated and the validator name. I wrote in the rules:

array('username', 'readOnly'=>true, 'on'=>'update'),

and in the form:

echo $form->textFieldRow($model,'username',array(
         'class'=>'span5',
         'maxlength'=>45,
         'readOnly'=>($model->scenario == 'update')? true : false
     ));

But I do not understand why this shows an error.

+4
source share
5 answers

This validation rule does not make sense.

The error message states that the validator name is missing:

 array('username', 'ValidatorNameGoesHere', 'readOnly'=>true, 'on'=>'update'),

- , , Yii , readOnly; safe.

( , ) , , , ( , ) . , HTTP-, , .

:

 array('username', 'safe', 'except'=>'update'),
 array('username', 'unsafe', 'on'=>'update'),
+7

HTML-, , .

 This is incorrect: <input id='username' readonly='true'>
 This is correct: <input id='username' readonly='readonly'>

echo $form->textFieldRow($model,'username',array(
         'class'=>'span5',
         'maxlength'=>45,
         'readOnly'=>($model->scenario == 'update')? "readonly" : ""
     ));

: http://www.w3.org/TR/html-markup/input.text.html#input.text.attrs.readonly

: readonly ?

+3

This method is even better!

echo $form->textFieldRow($model,'username',array(
     'class'=>'span5',
     'maxlength'=>45,
     'readOnly'=>($model->scenario == 'update')? "readonly" : false
 ));
+1
source

We can use the code below, since it will work on creating and updating both:

<?php echo $form->textField($model,'promo_code', ($model->scenario == 'update') ? array('size'=>60,'maxlength'=>1000,   'readOnly'=>'readOnly') :array('size'=>60,'maxlength'=>1000)); ?>
0
source
<?PHP echo $form->textFieldRow($model,'username', ($model->isNewRecord)?array('class' => 'form-control','span'=>5,'maxlength'=>255):array('class' => 'form-control','span'=>5,'maxlength'=>255,'readOnly'=>'readOnly'));?>
0
source

Source: https://habr.com/ru/post/1534997/


All Articles