开始我的第一个CakePHP'项目'。我在添加$ this-> request->是('post')时,让CakePHP(2.3)在提交时正确显示验证消息时遇到问题。如果我删除它我得到验证消息(好!),但它验证并在页面加载时显示它们。(不好!)。
修改以澄清:问题是它没有自动显示验证错误。
/app/view/Feedback/add.ctp
echo $this->Form->create('Feedback');
echo $this->Form->input('Feedback.browser', array('type' => 'hidden', 'value' => $_SERVER['HTTP_USER_AGENT']));
echo $this->Form->input('Feedback.ip', array('type' => 'hidden', 'value' => $_SERVER['REMOTE_ADDR']));
echo $this->Form->input('Feedback.suggestie', array('rows' => '3', 'label' => false));
echo $this->Form->end('Send');
/app/Controller/FeedbackController.php
public function add($site = null, $page = null)
{
if( $this->request->is('post') )
{
if( $this->Feedback->save( $this->request->data ) )
{
$subject = "Feedback ontvangen voor ".$this->data['Feedback']['site'];
$mailBody = "Er is feedback ontvangen voor ".$this->data['Feedback']['site'] .":<br/>
<br/>
Pagina: ".$this->data['Feedback']['page']."<br/>
IP: ".$this->data['Feedback']['ip']."<br/>
Browser: ".$this->data['Feedback']['browser']."<br/>
<br/>
Pagina beoordeling: ".$this->data['Feedback']['rating']."<br/>
<br/>";
if( $this->data['Feedback']['suggestie'] ) {
$mailBody .= "Suggestie:<br/>"
.$this->data['Feedback']['suggestie']."<br/>";
}
if( $this->data['Feedback']['foutmelding'] ) {
$mailBody .= "Foutmelding:<br/>"
.$this->data['Feedback']['foutmelding']."<br/>
<br/>
Contact voor foutmelding: ".$this->data['Feedback']['foutmeldingctc'];
}
$email = new CakeEmail();
$email->emailFormat( 'html' );
$email->from( array( 'xxxx' => 'High Profile Locaties Feedback' ) );
$email->to( 'xxxx' );
$email->subject( $subject );
$email->send( $mailBody );
//naar bedank pagina
$this->redirect( '/pages/bedankt' );
}
}
}
答案 0 :(得分:-1)
使用空($ this-&gt; request-&gt;数据)来测试表单是否已发布:
if (!empty($this->request->data)) {
// form posted
if ($this->Feedback->save($this->request->data)) {
// save successful
}
else {
// errors happened
}
}
else {
// form not posted yet
}