Question

I have a User model with a lot of attributes and validation rules:

class User extends AppModel {
    public $hasMany = 'Serviceorder';

    public $validate = array(
        'name' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A name is required'
            )
        ),
        ...
        'password' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'A password is required'
            )
        ),
        ...
    );
}

This makes sense to me because when creating a user a password is always required. In some cases, like when editing a user, I don't want the password to be required. When there's no new password, the password is not changed. How can I do that?

Was it helpful?

Solution

change the password validation as below :-

   'password' => array(
        'required' => array(
            'rule' => array('notEmpty'),
            'message' => 'A password is required',
            'on' => 'create'
        )
    ),
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top