I’m almost embarassed to be asking this question because I suspect the answer is staring me in the face. I’m writing my first yii application, learning as I go along. I’ve set-up the following rule for one of my models -
array('password', 'required', 'on'=>'create'),
However, when testing, it appears not to work as I expected - ‘password’ seems to be optional on either creation or update while I want it to be required for creation but optional for update. If I change the rule to read ‘on’=>‘update’ it works fine, although the wrong way round for my purposes.
That is because probably you don’t set any rule for ‘password’ on update scenario, therefore, it is ‘not safe’ to set on update so you can’t assign a value to it
Try to add other rule like array(‘password’,‘length’,‘max’=>64) or some other rule
Thanks Gustavo. I think I’m already doing something along those lines. Here’s the full set of rules I’ve got, including one restricting the password length -
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('name, address1, city, postcode, admin_email', 'required'),
array('password', 'required', 'on'=>'create'),
array('admin_email', 'email'),
array('admin_email', 'unique', 'attributeName'=>'admin_email'),
array('name, website', 'length', 'max'=>255),
array('address1, address2, address3, city, admin_email', 'length', 'max'=>100),
array('postcode, sic', 'length', 'max'=>10),
array('phone', 'length', 'max'=>20),
array('password', 'length', 'max'=>32),
array('id, admin_id, name, description, address1, address2, address3, city, postcode, website, phone, sic, admin_email, password', 'safe', 'on'=>'search'),
);
}