在没有数据库的多个字段同名的表单验证之后,cakephp为空表单

时间:2012-09-28 20:34:25

标签: php cakephp

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

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

所以数据就在那里,但是蛋糕不会把它放在字段中。但我无法理解为什么......

2 个答案:

答案 0 :(得分:1)

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

所以我的解决方案:

    $invoiceData = $this->request->data['Invoice'];
    if ($this->Invoice->validateMany($this->request->data['Invoice']))
    {
    ...
    }
    $this->request->data['Invoice'] = $invoiceData;

答案 1 :(得分: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');