I'm trying to validate multiple models on saveAll. The validations in the first model are being triggered but when it comes to the related model, nothing seems to be happening. I even tried to check if the beforeValidate() and beforeSave() methods are being called by putting an exit; in them but the code continues to execute as normal.
ContactDetails Model:
<?php
class ContactDetails extends ContactAppModel {
public $actsAs = array("MapValidate");
public $hasMany = array(
'ProjectLocation' => array(
'className' => 'ProjectLocation',
'foreignKey' => 'project_id'
)
);
public $validate = array(
'name' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Contact name is required'
)
),
'address1' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Contact address 1 is required'
)
),
'email' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Contact email is required'
),
'email' => array(
'rule' => array('email'),
'message' => 'Contact email format is not valid'
)
)
);
}
ProjectLocation Model:
<?php
class ProjectLocation extends ContactAppModel {
public $actsAs = array("MapValidate");
public $belongsTo = array(
"ContactDetails" => array(
"className" => "ContactDetails",
"foreignKey" => "project_id"
);
);
public $validate = array(
'lat' => array(
'checkLocation' => array(
'rule' => array('checkMap', 'lat'),
'message' => 'One or more positions on the map are invalid.'
)
)
);
}
This is the $this->request->data that I'm trying to save:
Array
(
[ContactDetails] => Array
(
[id] => 1
[name] => PreeoStudios
[address1] => 4, Stivala Street
[address2] => Mosta, MST 3205
[address3] => Malta
[telephone] => 34562737
[email] => [email protected]
[fax] => N/A
[skype] => N/A
)
[ProjectLocation] => Array
(
[0] => Array
(
[lat] => 35.886277456343024
[lon] => 14.428907312499973
)
[1] => Array
(
[lat] => 35.886277456343024
[lon] => 14.528907312499973
)
)
)
The saveAll call:
$this->ContactDetails->saveAll($this->request->data, array('validate' => 'first'))
EDIT
I also tried to remove the validation rules from the associated model and put an exit in the beforeSave function... the code just keeps executing
<?php
class ProjectLocation extends ContactAppModel {
public $actsAs = array("MapValidate");
public $belongsTo = array(
"ContactDetails" => array(
"className" => "ContactDetails",
"foreignKey" => "project_id"
);
);
public function beforeSave(){
exit;
}
public $validate = array(
);
}
Try adding the option deep:
$this->ContactDetails->saveAll($this->request->data, array('validate' => 'first', 'deep' => true))
From: http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-saveassociated-array-data-null-array-options-array
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With