Revision #4 has been created by mrun on Dec 2, 2016, 7:40:35 AM with the memo:
Fixed three typos.
« previous (#3)
Changes
Title
unchanged
How to "masquerade" a non-required form element as 'required' one (in the view only)
Category
unchanged
How-tos
Yii version
unchanged
Tags
unchanged
required field, validation rules
Content
changed
[...]
When is it needed?
----------------------
Consider the following use case:
- You have a model with two phone numbers attributes: _phone_ and _fax_. Phone is qa required field while fax is not. Yet, if submitted, both should be validated as 'phone numbers', hence use a custom validator class, [PcSimplePhoneValidator](http://www.yiiframework.com/extension/pcsimplephonevalidator/)).
- So, you
'rer rules() method will need to look like this:[...]
PcSimplePhoneValidator that is configured to disallow empty phone numbers (only empty fax is allowed).
- So, you take off the 'required' line in the rules() method above. The line after it will take care that phone is always submitted, and when submitted its validated with our needed validator.
- But, once 'phone' is no more a 'required' attribute in the rules() method, it's no longer rendered as a required field, taking off the usual red asterisk that followed the label.
The solution
--------
We'll change the label rendering method. Change the phone number rendering in _form.php from:[...]