Reputation: 3015
Does any body can know how to validate multiple entries for same field. I need to validate and save the data to DB table. I tried with validation rule method that we normally use to validate data, but validation function returns 1 i.e. True instead of error. Also I enclosed validation rules in an extra array steal it is not working.
Please guide me to validate the data for multiple entries.
Controller code
$this->MyModel->set($this->request->data);
if($this->MyModel->validates()){
//Some code
}else{
pr($this->MyModel->validationErrors);
}
output of submitted form :
Array
(
[MyModel] => Array
(
[0] => Array
(
[qualification_id] =>
[stream] =>
[pass_year] =>
[description] =>
[resume_id] => 1
)
[1] => Array
(
[qualification_id] =>
[stream] =>
[pass_year] =>
[description] =>
[resume_id] => 1
)
)
)
MyModel validation rules:
public $validate = array(
array(
'resume_id' => array(
'notEmpty'=>array(
'rule' => 'notEmpty',
'message' => 'Invalid resume reference.'
),
'numeric' => array(
'rule' => 'numeric',
'message' => 'Invalid resume reference.'
)
),
'qualification_id' => array(
'notEmpty'=>array(
'rule' => 'notEmpty',
'message' => 'Please enter qualification.'
),
'numeric' => array(
'rule' => 'numeric',
'message' => 'Invalide qualification selected'
)
)
)
);
Thanks,
Parag C.
Upvotes: 1
Views: 2177
Reputation: 3015
I Found the solution as Inigo Flores said Model::validateMany()
is help full, but in my case Model::validateMany()
is not working. So I have overridden the method in my model
Model code:
public function validateMany(&$data, $options = array()) {
$validationErrors = array();
foreach ($data[$this->alias] as $key => $values) {
$this->set($values);
if (!$this->validates($values)) {
$validationErrors[$this->alias][$key] = $this->validationErrors;
foreach ($this->validationErrors as $field => $msg) {
$validationErrors[$this->alias][$key][$field]['message'] = $msg[0];
}
}
}
if (!empty($validationErrors)) {
unset($this->validationErrors);
$this->validationErrors = $validationErrors;
return false;
} else {
return true;
}
}
I used cakephp's $this->validates()
method to validate each array
CTP code for each individual form field:
<?php
$isError = false;
if (!empty($errors['MyModel'][$index]['qualification_id']['message'])) {
$isError = true;
}
?>
<div class="col-lg-4<?php echo ($isError) ? ' error' : ''; ?>">
<?php
echo $this->Form->label(_('Qualification'));
echo $this->Form->select('MyModel.' . $index . '.qualification_id', $qualifications, array('div' => false, 'required' => false, 'empty' => 'Please select', 'class' => 'form-control', 'maxlength' => 80));
echo ($isError) ? '<span>' . $errors['MyModel'][$index]['qualification_id']['message'] . '</span>' : '';
?>
</div>
This is a bit long process, but it resolved my validation problem.
Thanks, Parag C.
Upvotes: 0
Reputation: 4469
Model::validates()
can only handle a single record, that has previously been set with Model::set()
.
If you wish to validate multiple records, you have to use Model::validateMany()
.
It could be implemented as:
$data = $this->request->data; //to prevent $this->request->data from being altered.
if($validationErrors=$this->MyModel->validateMany($data)){
//Some code
}else{
pr($validationErrors);
}
See Model::validateMany()
in the CakePHP 2.x API.
Upvotes: 3