Method 1:
Use saveMany For validating the multiple records at once
In your controller just use saveMany/saveAll
method of model instead of validates
, since you're having too many records to validated.
if ($this->UserInformation->saveMany($this->request->data)) {
// do your stuff
} else {
// check the validation errors here.
pr($this->UserInformation->validationErrors);exit;
}
and your $this->request->data
should be like this
Array(
'Userinformation' => array(
0 => array(
'schoolname' => 'school 0'
),
1 => array(
'schoolname' => 'school 1'
),
2 => array(
'schoolname' => 'school 2'
),
)
)
Method 2:
Here is an another way of validating multiple records at once:
If you want to validate the records, and don't want to perform save operation it is useful..
$this->ModelName->validateMany($data['ModelName']);
Here $data
should look like as below
$data = array(
'ModelName' => array(
'field1' => 'value1'
'field2' => 'value2'
'field3' => 'value3'
)
);
And you can access the validation errors (on unsuccessful validation) Using $this->ModelName->validationErrors
In Your case
if ($this->UserInformation->validateMany($this->request->data['Userinformation'])) {
// do your stuff
} else {
// check the validation errors here.
pr($this->UserInformation->validationErrors);exit;
}
and your $this->request->data
should be like this
Array(
'Userinformation' => array(
0 => array(
'schoolname' => 'school 0'
),
1 => array(
'schoolname' => 'school 1'
),
2 => array(
'schoolname' => 'school 2'
),
)
)