0

我正在使用具有多个同名字段的表单。在对表单进行错误验证后,我将使用验证消息返回给它。这一切都有效。虽然字段的验证消息显示正确,但字段中输入的数据却消失了。

它的 cakephp 2.2 应用程序没有数据库。(将数据保存到 3 页的会话中,然后将其写入 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));

控制器:

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.'));
                }
            }
        }
    }
}

step2.ctp 中 ($this->Form) 的调试:

    .....
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' => ''
                )
            )
        )
    )
....

所以数据在那里,但蛋糕没有把它放在字段中。但我想不通为什么...

4

2 回答 2

1

我在提示后找到了解决方案。验证确实改变了数据。我错过了模型中的警告:“* 警告:此方法可能会更改传递的参数 $data,* 如果您不希望发生这种情况,请在将 $data 传递给此方法之前复制它*”

所以我的解决方案:

    $invoiceData = $this->request->data['Invoice'];
    if ($this->Invoice->validateMany($this->request->data['Invoice']))
    {
    ...
    }
    $this->request->data['Invoice'] = $invoiceData;
于 2012-09-29T13:19:22.573 回答
0

尝试这个:

$this->Form->create('Invoice');
$this->Form->input('0.OrderNumber');
$this->Form->input('0.NetAmount');
...
$this->Form->input('1.OrderNumber');
$this->Form->input('1.NetAmount');
于 2012-09-29T11:21:23.463 回答