0

我正在构建一个 CakePHP 2 应用程序。我一直在尝试锻炼如何最好地在页面顶部显示我的表单错误。我发现了其他一些帮助我解决错误的帖子,但我正在努力循环浏览它们以正确显示它们。

我的表格由多行字段组成 - 每行都是一个单独的数据库行(作为费用报销的一部分的个人费用)。

我在控制器中设置它以传递错误:

$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 插入行,所以不能依赖表单助手。

提前致谢。

4

1 回答 1

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 } ?>
于 2012-08-13T11:53:18.017 回答