规则触发时显示验证消息

时间:2013-08-16 12:14:31

标签: php cakephp

所以如果我的Forward模型中有以下验证规则

    public $validate = array(
    'url' =>array(
        'rule' => 'url',
        'message' => 'Please supply a valid Url.'
    )
);

我想在flash中显示消息我该如何实现?

我尝试了以下内容:

        $new_forward = $this->request->data;
        $this->Forward->create();
        $this->Session->setFlash($this->Forward->save($new_forward));

还尝试了这个没有结果

$this->Session->setFlash($this->ModelName->validationErrors);

1 个答案:

答案 0 :(得分:1)

您可以使用以下示例代码

执行此操作
$this->Forward->create();
if ($this->Forward->save($this->request->data))
{
    $this->Session->setFlash(__('The Forward has been saved', true),'flash_success');
    $this->redirect(array('controller' => 'forwards','action' => 'index'));
}
else
{
    $this->Session->setFlash(__('The Forward could not be saved. Please, try again.', true),'flash_error');
}