如何使用cakeHP显示错误表单


How to display an error form with cakePHP?

我想知道是否有一种方法可以用cakeHP显示错误表单,但在另一个视图中它不是默认的。

这是我将数据添加到数据库的默认视图:

添加数据的默认视图

但我有另一种观点,我使用相同的表格,只是风格不同:

我的另一个视图,我可以添加相同的数据太

我在我的模型中进行了验证:

var $validate = array(
    'cantidad' => array(
            'rule' => 'notEmpty',
            'message' => 'Rellena este campo'),
    'articulo_id' => array(
            'rule' => array('checkArticulo', 1),
            'on' => 'create',
            'message' => 'El artículo seleccionado ya fue agregado, intenta con otro.'
        )
);      
function checkArticulo($data, $limit){
    $now = new DateTime();
    $articulo_agregado = $this->find( 'count', array('conditions' => array('AperturaArticulo.articulo_id' => $data, 'AperturaArticulo.created >=' => $now->format('Y-m-d 00:00:00'))));
    return $articulo_agregado < $limit;
} 

我的字段"cantidad"不能为空,字段"articulo_id"每天不能为两次。

如果你在第二张图片中看到,我已经添加了名为"TORTILLA 900GR"的文章。如果我试图在"我的其他视图中添加相同的文章,在那里我也可以添加相同的数据"我成功地获得了验证,但错误显示在'添加数据的默认视图中

我只想在我的"我的其他视图中也可以添加相同的数据"中显示该错误

谢谢你!

您的意思是使用自定义表单/不使用formhelper进行验证。我们可以在控制器中进行这些验证。从控制器验证数据中获取代码示例。

方法$this-><控制器中的MODEL>->validates()将只返回true或false。要手动处理验证错误,您需要以下内容:

if ($this-><MODEL>->validates()) {
    // it validated logic
} else {
    // didn't validate logic
    $errors = $this-><MODEL>->validationErrors;
}