I have 2 models on the same page:
Person (always required)
User (optional)
Person fields id|firstname|lastname|gender
User fields: id|username|password|password_confirm
These fields have notEmpty validation rules, password and password_confirm have a custom validation rule (matchpassword).
I want that if I don't fill any of the User fields in the view, the empty models mean valid. But if any of the fields are not empty the User validation rules are works as normally.
So save in the following cases:
- Person all of the fields are filled and all of the User fields are empty
or
- Person all if the fields filled and User fields are also filled
Any Other case i want to see the exact reason why i was not able to save
The main task is in the add function,but i am totally confused now, don't exactly know how to.
Update1: Sorry i make a fault the validation rule is not empty for both models
Update2:i was try to check User validation before save
public function add() {
unset($this->Group->User->validate['username']);//i switched off the notempty rule here
if ($this->request->is('post')) {
$this->Group->create();
if ($this->Group->save($this->request->data))
{
$this->Session->setFlash(__('The group has been saved.'));
//try to check validation before saving
$this->Group->User->set( $this->data );
if ($this->Group->User->validates()) {
//group saved ok, now add people
$group_id=$this->Group->getInsertId();
if(!empty($this->request->data['User']))
foreach($this->request->data['User'] as $user) {
//loop for each person added
$user['group_id']=$group_id;
$this->Group->User->create();
if($this->Group->User->save($user))
{
debug($this->Group->User->validates());
$this->Session->setFlash(__('The Userhas been saved.'));}
}//end foreach
return $this->redirect(array('action' => 'index'));
}else{
$this->Session->setFlash(__('The User could not been saved.'));
}
}
else {
$this->Session->setFlash(__('The group could not be saved. Please, try again.'));
}
}
}
There are following errors:The User is saved even with empty username field,even if totally empty fields saved with group_id and the rest fields are empty.
Update3: i implemented as cwbit second solution was(also modified the behavior to be compatible with 2.x),but this does not works for me
public $validate = array(
'password'=>array('username'=>array(
'rule'=>'notEmpty',
'if'=>array('username')//if the username exist, the password is notEmpty if i good understand
)
);
You could check if
$this->request->data['User']
is empty prior to saving/validating usingHash::filter()
(see docs on Hash utility):-