CakePHP 2.2.1如何将validationErrors传递给SetFlash?

时间:2012-08-01 16:59:16

标签: php cakephp

我正在为我的网站构建一个简单的联系表单。

在我的模特中,我有:

class Contact extends AppModel {
public $useTable = false;
public $validate = array(
    'name' => array(
        'rule' => 'notEmpty',
        'message' => 'Field name must be not empty'
    ),
    'email' => 'email',
    'message' => array(
        'rule' => 'notEmpty',
        'message' => 'Field message must be not empty'
    )
);
}

控制器:

class ContactsController extends AppController {
public $helpers = array('Html', 'Form');

public function index() {

}

public function send() {
$this->Contact->set($this->request->data);
if ($this->Contact->validates()) {
//sending mail logic
        };
                                } else {
// didn't validate logic
$errors = $this->Contact->validationErrors;

}
}
}
}

并在index.ctp视图中我有:

echo $this->Form->create('Contact', array('action' => 'send'));
echo $this->Form->input('name', array('label' => 'Your name:'));
echo $this->Form->input('email', array('label' => 'Your e-mail:'));
echo $this->Form->input('message', array('rows' => '6', 'label' => 'Your message:'));
echo $this->Form->end('Send button');

echo $this->Session->flash();

因为我使用来自Controller的Validating Data我遇到了将$ errors传递给Session-> flash的问题。这样做的正确方法是什么?我不想为"发送"在我的控制器中的方法。

1 个答案:

答案 0 :(得分:0)

我的问题是我没有在控制器中使用第二个视图作为send方法。 当我添加$ this-> render('index');它解决了所有问题,现在我的表单自动显示验证消息。