user3476345
user3476345

Reputation: 331

Changing validation on the fly in CakePHP

I'm using CakePHP 2.3.8 and I'm trying to figure out if there's a way to set certain validation rules to required on the fly.

For example, my User model has phone_number, username, email, and password validation. If a user wants to change their username, their phone number isn't required to do so. That means I can't set it to required, because then when changing a username, the phone_number will be expected to be present in the data.

 public $validate = array(

   'username' => array(
        'minLength' => array(
             'rule' => array('minLength', '3'),
             'message' => 'A username with a minimum length of 3 characters is required'
          ),
          'unique' => array(
                'rule'   => 'isUnique',
                'message' => 'This username has already been taken.'
           )
    ),
    'email' => array(
        'email' => array(
            'rule'    => array('email'),
            'message' => 'Please enter a valid email address.',
        ),
        'unique' => array(
            'rule'    => 'isUnique',
            'message' => 'This email address is already in use'
        )
    ),
    'password' => array(
         'rule' => array('minLength', '8'),
         'message' => 'A password with a minimum length of 8 characters is required'
    ),
    'phone_number' => array(
        'rule' => array('valid_phone'),
        'message' => 'Invalid phone number',
    )
);

To get around this problem, in my controller for the corresponding action what I've been doing is checking to make sure the expected inputs have been posted. If not, set that index to null so that it is validated...such as

public function change_username(){

    if(!isset($this->request->data['username'])){
        $this->request->data['username'] = null;
    }

    $this->ExampleModel->set($this->request->data);

    //if it wasn't posted, the username index will be created but set to null. This is my workaround for setting something to "required"
    if($this->ExampleModel->validates() == true){
        //do something
    }
    else{
        //do something
    }
}

While this works, I feel like it makes for a lot of extra coding, especially if I have a form that has a lot of inputs.

I've also tried to validate only the inputs that I need, but unless the data was posted, it ignores them. For example

if($this->ExampleModel->validates(array('fieldList' => array('phone')) == true){
    .....
}

If "phone" wasn't posted, it doesn't validate it.

Is there any way to set required for a given input's validation to true on the fly? I found this article on using multiple validation rulesets and while it would accomplish what I want, there would be a lot of re-coding involved.

Before validation, can I set an input to required?

Upvotes: 1

Views: 943

Answers (1)

Diomedes Andreou
Diomedes Andreou

Reputation: 385

Firstly, in your Model validation rules you have phone_number but yet trying to validate phone, there aren't validation rules for phone. It would be ideal request->data[] to match model fields, you can rebuild an array etc.

From book.cakephp:

This will add a single rule to the password field in the model. You can chain multiple calls to add to create as many rules as you like:

$this->validator()
    ->add('password', 'required', array(
        'rule' => 'notEmpty',
        'required' => 'create'
    ))
    ->add('password', 'size', array(
        'rule' => array('between', 8, 20),
        'message' => 'Password should be at least 8 chars long'
    ));

Upvotes: 1

Related Questions