how to disable a field on update in Yii
Solution 1
That validation rule is meaningless.
The error message tells you that the validator name is missing:
array('username', 'ValidatorNameGoesHere', 'readOnly'=>true, 'on'=>'update'),
But even if you fill in something for the validator name it still won't work because there is no validator in Yii that has a readOnly
attribute; this role is played by the safe
attribute.
Making certain fields read-only when updating in a secure manner (i.e. one that the user cannot override) means that you will have to look at the submitted data, determine independently if the data includes the PK of an existing model (which tells you if you are adding or updating) and set the model's scenario based on that. If you don't do this, your users can easily manipulate the HTTP requests sent to the server and bypass the read-only logic.
After the scenario is set you can easily enforce the read-only logic with a couple of rules:
array('username', 'safe', 'except'=>'update'),
array('username', 'unsafe', 'on'=>'update'),
Solution 2
The HTML code you generate is not correct.
This is incorrect: <input id='username' readonly='true'>
This is correct: <input id='username' readonly='readonly'>
Therefore, change your code to
echo $form->textFieldRow($model,'username',array(
'class'=>'span5',
'maxlength'=>45,
'readOnly'=>($model->scenario == 'update')? "readonly" : ""
));
References : http://www.w3.org/TR/html-markup/input.text.html#input.text.attrs.readonly
References : What is the correct readonly attribute syntax for input text elements?
Solution 3
This way is even better!
echo $form->textFieldRow($model,'username',array(
'class'=>'span5',
'maxlength'=>45,
'readOnly'=>($model->scenario == 'update')? "readonly" : false
));
StreetCoder
Web application Developer: expert on python, php, nodejs, angular
Updated on June 29, 2022Comments
-
StreetCoder almost 2 years
I want to disable or make readOnly a field when user will update, i.e.
username
. When registered user update their information, they will see that username disable. I tried based on this answer but it does not work for me rather giving an errorUser 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 do not understand why this shows error.
-
Wirone over 7 yearsNoteworthy:
safe
validator works only for mass assignment and will not deny updating model's attribute with individual assignment ($model->someAttribute
). -
bakavic over 6 yearsThere is no
unsafe
validator - the 2nd line is redundant. -
Waihon Yew over 6 years@bakavic there is an
unsafe
validator in Yii 1 (e.g. see here), which this question is about. Yii 2 had not even been released when I wrote this answer. =) -
bakavic over 6 years@Jon apologies - the syntax for rules are pretty similar between the 2 frameworks, so I assumed this question was about yii2.