#5715: Validation errors don't display when validating with an array of the same
model using model.saveAll()
--------------------------------------+-------------------------------------
Reporter: pearj | Type: Bug
Status: new | Priority: Medium
Milestone: 1.2.x.x | Component: Helpers
Version: RC3 | Severity: Normal
Keywords: formhelper validation | Php_version: PHP 5
Cake_version: |
--------------------------------------+-------------------------------------
When trying to validate an array of the same type of model, the validation
errors don't appear.
I have used the name convention as suggested here:
[http://manual.cakephp.org/complete/181/Core-Helpers#Field-naming-
convention-547]
eg:
{{{
<?php
echo $form->input('fieldname.1');
echo $form->input('fieldname.2');
?>
<input type="text" id="ModelnameFieldname1" name="data[Modelname]
[fieldname][1]">
<input type="text" id="ModelnameFieldname2" name="data[Modelname]
[fieldname][2]">
}}}
The problem is when I do model.saveAll() it screws up because it
expects the data in this format:
{{{
data[1][Modelname][fieldname]
data[2][Modelname][fieldname]
}}}
I know that is the expected model because of the comment above the
saveAll function:
{{{
@param array $data Record data to save. This can be either a
numerically-indexed array (for saving multiple records of the same
type), or an array indexed by association name.
}}}
But validation errors won't display in the saveAll format.
Because validationErrors array ends up looking like:
{{{
[ModelName][1][field] = "error message"
[ModelName][2][field] = "error message"
}}}
But for the FormHelper to display the errors it needs to look like
this:
{{{
[ModelName][field][1] = "error message"
[ModelName][field][2] = "error message"
}}}
So I have managed to work around it by using this in the view:
echo $form->input('Model.fieldname.1');
echo $form->input('Model.fieldname.2');
And then I have a function that converts the $this->data for saveAll:
{{{
<?php
function _prepareDataForValidation($data) {
$fixedData = array();
foreach ($data as $model => $fields) {
foreach ($fields as $field => $values) {
foreach ($values as $id => $value) {
$fixedData[$id][$model][$field] = $value;
}
}
}
return $fixedData;
}
?>
}}}
And another function that puts the validationErrors in the right
format:
{{{
<?php
function _fixValidationErrorsArray($validationErrors) {
$newValidationErrors = array();
foreach($validationErrors as $key => $fields) {
foreach ($fields as $field => $value) {
$newValidationErrors[$field][$key] = $value;
}
}
return $newValidationErrors;
}
?>
}}}
I really think FormHelper should be changed to be in this format:
{{{
<?php
echo $form->input('Model.1.fieldname');
echo $form->input('Model.2.fieldname');
?>
}}}
instead of
{{{
<?php
echo $form->input('Model.fieldname.1');
echo $form->input('Model.fieldname.2');
?>
}}}
[https://trac.cakephp.org/ticket/4981] is similar to this, except I don't
have the belongs to association.
--
Ticket URL: <https://trac.cakephp.org/ticket/5715>
CakePHP : The Rapid Development Framework for PHP <https://trac.cakephp.org/>
Cake is a rapid development framework for PHP which uses commonly known design
patterns like ActiveRecord, Association Data Mapping, Front Controller and MVC.
Our primary goal is to provide a structured framework that enables PHP users at
all levels to rapidly develop robust web applications, without any loss to
flexibility.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"tickets cakephp" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/tickets-cakephp?hl=en
-~----------~----~----~----~------~----~------~--~---