Cakephp – bypassing data validation in models
This is very short and simple yet it might take you few minutes to Google and find out how to do this. As we know data validation in models are excellent way to get rid of unnecessary if -else conditions in controllers and keep code simple. Now consider an example you have a user table where fields are id, username, email, password, country etc. Now we can make sure that during registration username, email and password fields are entered correctly, this is done inside user model as you know
var $validate = array(
'username' => array('rule' => 'notEmpty', 'required' => true, 'message' => 'Enter username'),
'email' => array('rule' => 'notEmpty', 'required' => true, 'message' => 'Email address is incorrect'),
'password' => array('rule' => 'email', 'required' => true, 'message' => 'Enter password'));
Need arises
There are cases when we don’t want the cakephp’s model validation to function as usual for example in above case if user wants to simply update one field like email or country name the model would still block the updating, of one fields because remaining fields are not set. thus we need to bypass validation rules this can be done by adding following lines just above Model’s save method in respective controller action.
unset($this->User->validate['username']); unset($this->User->validate['email']); unset($this->User->validate['password']); $this->User->save($this->data);