2012-08-13 124 views
0

我正在構建一個CakePHP 2應用程序。我一直在努力鍛鍊如何最好地在頁面頂部顯示我的表單錯誤。我發現了其他一些幫助我獲得錯誤的文章,但我正在努力通過循環來正確顯示它們。CakePHP在頁面頂部顯示錯誤

我的表格由多行字段組成 - 每行都是一個單獨的數據庫行(作爲費用聲明一部分的單獨費用)。

我在控制器中設置此傳遞錯誤:

$this->set('errors', $this->ExpenseClaim->validationErrors); 

這是經過該陣列的錯誤:

array(
    (int) 0 => array(
     'date' => array(
      (int) 0 => 'The expense date cannot be blank' 
     ), 
     'sitename' => array(
      (int) 0 => 'The expense sitename cannot be blank' 
     ), 
     'detail' => array(
      (int) 0 => 'The expense details cannot be blank' 
     ), 
     'amount' => array(
      (int) 0 => 'The expense amount cannot be blank' 
     ), 
     'total' => array(
      (int) 0 => 'The expense total should not be blank' 
     ) 
    ), 
    (int) 1 => array(
     'date' => array(
      (int) 0 => 'The expense date cannot be blank' 
     ), 
     'sitename' => array(
      (int) 0 => 'The expense sitename cannot be blank' 
     ), 
     'detail' => array(
      (int) 0 => 'The expense details cannot be blank' 
     ), 
     'amount' => array(
      (int) 0 => 'The expense amount cannot be blank' 
     ), 
     'total' => array(
      (int) 0 => 'The expense total should not be blank' 
     ) 
    ) 
) 

我試過很多不同的foreach循環次數來嘗試並找出錯誤,但由於嵌套級別而失敗。有人能指出我正確的方向嗎?

我只想要一個錯誤 - 「費用日期不能在第1行中爲空」。

我正在使用jQuery來插入行,所以不能依賴於表單助手。

在此先感謝。

回答

0

我已經結束了複雜的事情!希望這可以幫助別人:

foreach ($errors['Expense'] as $key => $error) { 
     foreach ($error as $value) { 
      foreach ($value as $val) { ?> 
       <li><?php echo $val; ?> in row <?php echo $key; ?></li>  
      <?php } ?> 
     <?php } ?> 
    <?php } ?>