we can do validation with Model in cakephp by array variable i.e $validate.
here you can see working validation example which you have to write in model
public $validate = array(
'username' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => "Please enter username",
),
),
'password' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'on' => 'create',
'message' => "Please enter password",
),
),
'email' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => "Please enter email",
),
'email' => array(
'rule' => array('email'),
'message' => "Please enter valid email",
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'Email already registered'
),
)
);
you can also validate in controller see below code snippet
if ($this->User->validates()) {
// do whatever you want after all fields beings validated
}else{
$this->set('data', $this->request->data);
$this->validateErrors($this->Sendmail);
$this->render();
}
suppose you have two submit button in one form like Btn1 and Btn2
On click of Btn1 you want to validate some fields and on click of Btn2 you want to validate all fields
Then you can have more than one validates array and select which one you want to use before you save
see below code snippet for model
public $validate = array(
// default validation rules
);
public $validateforallfields = array(
// validation rules including password validation
);
Now, in your controller action you can select to validate with the all fields with:
$this->User->validate = $this->User->validateforallfields;
Thanks to watch :)
here you can see working validation example which you have to write in model
public $validate = array(
'username' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => "Please enter username",
),
),
'password' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'on' => 'create',
'message' => "Please enter password",
),
),
'email' => array(
'notBlank' => array(
'rule' => array('notBlank'),
'message' => "Please enter email",
),
'email' => array(
'rule' => array('email'),
'message' => "Please enter valid email",
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'Email already registered'
),
)
);
you can also validate in controller see below code snippet
if ($this->User->validates()) {
// do whatever you want after all fields beings validated
}else{
$this->set('data', $this->request->data);
$this->validateErrors($this->Sendmail);
$this->render();
}
suppose you have two submit button in one form like Btn1 and Btn2
On click of Btn1 you want to validate some fields and on click of Btn2 you want to validate all fields
Then you can have more than one validates array and select which one you want to use before you save
see below code snippet for model
public $validate = array(
// default validation rules
);
public $validateforallfields = array(
// validation rules including password validation
);
Now, in your controller action you can select to validate with the all fields with:
$this->User->validate = $this->User->validateforallfields;
Thanks to watch :)
No comments:
Post a Comment