0

I'm using a form with multiplefields with the same name. After a false validation of the form i return to it with the validation messages. That all works. While the validation messages of the fields are shown correct, the entered data in the fields are gone.

Its cakephp 2.2 application without a database. (saving the data to a session for 3 pages then write it to XML)

step2.ctp:

debug($this->Form);
echo $this->Form->input('Invoice.0.InvoiceOrderNumber', array('label' => false));
echo $this->Form->input('Invoice.0.NetAmount', array('label' => false));
echo $this->Form->input('Invoice.0.CostBearer', array('label' => false));
echo $this->Form->input('Invoice.0.CostCenter', array('label' => false));
echo $this->Form->input('Invoice.0.LedgerAccount', array('label' => false));

Controller:

public function step2() {
    $invoice = $this->Session->read('Invoice');
    if (!isset($invoice) && is_array($invoice))
        $this->redirect(array('action' => 'step1'));
    else
    {       
        $this->set('title_for_layout', 'Nieuwe interne doorbelasting');
        if ($this->request->is('post')) {

            debug($this->request->data);
            if ($this->Invoice->validateMany($this->request->data['Invoice']))
            {
                if ($this->Session->write('Invoice', $this->request->data['Invoice'])) {
                    $this->Session->setFlash(__('The invoice has been saved'));
                    $this->redirect(array('action' => 'step3'));
                } else {
                    $this->Session->setFlash(__('The invoice could not be saved. Please, try again.'));
                }
            }
        }
    }
}

debug of ($this->Form) in step2.ctp:

    .....
request => object(CakeRequest) {
    params => array(
        'plugin' => null,
        'controller' => 'invoices',
        'action' => 'step2',
        'named' => array(),
        'pass' => array(),
        'models' => array(
            'Invoice' => array(
                'plugin' => null,
                'className' => 'Invoice'
            )
        )
    )
    data => array(
        'Invoice' => array(
            (int) 0 => array(
                'Invoice' => array(
                    'InvoiceOrderNumber' => 'adfas',
                    'NetAmount' => '2',
                    'CostBearer' => '',
                    'CostCenter' => '',
                    'LedgerAccount' => ''
                )
            ),
            (int) 1 => array(
                'Invoice' => array(
                    'InvoiceOrderNumber' => 'adaaaaaaaaa',
                    'NetAmount' => 'bbbbbbbbb',
                    'CostBearer' => '',
                    'CostCenter' => '',
                    'LedgerAccount' => ''
                )
            )
        )
    )
....

So the data is there, but cake doesn't put it in the fields. But i can't figger out why...

3
  • Seems your data have an extra "Invoice" index, like this ['Invoice'][0]['Invoice']['NetAmount]. How are you generating that array? Commented Sep 28, 2012 at 21:16
  • Well, i don't, cake does. Cake saves the data to $this->request->data after false validation it uses his own data again. I just checked and it seems the validation function alters the index of the request data to the structure u just described. Maybe a bug in validateMany? Commented Sep 29, 2012 at 7:25
  • I found the solution after the hint. Validation indeed changes the data. Theres a warning in the model i missed: "* Warning: This method could potentially change the passed argument $data, If you do not want this to happen, make a copy of $data before passing it to this method" Commented Sep 29, 2012 at 13:14

2 Answers 2

1

I found the solution after the hint. Validation indeed changes the data. Theres a warning in the model i missed: "* Warning: This method could potentially change the passed argument $data, * If you do not want this to happen, make a copy of $data before passing it * to this method"

So my solution:

    $invoiceData = $this->request->data['Invoice'];
    if ($this->Invoice->validateMany($this->request->data['Invoice']))
    {
    ...
    }
    $this->request->data['Invoice'] = $invoiceData;
Sign up to request clarification or add additional context in comments.

Comments

0

Try this:

$this->Form->create('Invoice');
$this->Form->input('0.OrderNumber');
$this->Form->input('0.NetAmount');
...
$this->Form->input('1.OrderNumber');
$this->Form->input('1.NetAmount');

1 Comment

That doenst fix it, but tnx for the suggestion. But like stated its fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.