Wednesday, November 5, 2008

Inconsistency between displaying validation errors and validating with model.saveAll()

Hi,

I've run into a frustrating problem, I'm trying to validate an array
of the same type of model, but the

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:

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:

function _fixValidationErrorsArray($validationErrors) {
$newValidationErrors = array();

foreach($validationErrors as $key => $fields) {
foreach ($fields as $field => $value) {
$newValidationErrors[$field][$key] = $value;
}
}

return $newValidationErrors;
}

Should I log this as a bug in trac?

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.

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

No comments: