There is nothing stopping you from naming your fields as:
$form->input('Model.1.fieldname');

As a matter of fact, if you read about saveAll() in the manual, you'll
see an example that uses that exact format.

Regarding the 'fieldname.1' notation as in the form helper example, it
clearly states that is only useful for looping through the fields, it
doesn't say anything about saveAll(). That being said, it probably
should be updated, but again form helper works just fine either way.

On Nov 6, 1:23 am, Joel <[EMAIL PROTECTED]> wrote:
> 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-conv...
>
> 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/4981is 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 [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to