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 に答える