我正在为我的网站构建一个简单的联系表格。
在我的模型中,我有:
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();
因此,因为我使用来自控制器的验证数据,所以将 $errors 传递给 Session->flash 时遇到了问题。这样做的正确方法是什么?我不想在我的控制器中为“发送”方法制作额外的视图。