CakePHP在页面顶部显示错误

时间:2012-08-13 11:19:02

标签: cakephp

我正在构建一个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插入行,因此无法依赖表单助手。

提前致谢。

1 个答案:

答案 0 :(得分: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 } ?>