【问题标题】:CakePHP - Validation with save no messagesCakePHP - 不保存消息的验证
【发布时间】:2013-02-13 14:55:23
【问题描述】:

从事我的第一个 CakePHP '项目'。当我添加 $this->request->is('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' );
        }
    }
}

【问题讨论】:

  • 有什么问题?当您提交并检查is('post') 时,它会跳过验证吗?或者它不显示消息?显示您的其余操作可能会让我们知道发生了什么。
  • @jeremyharris 它没有显示任何消息。
  • 嗯,我认为您的代码没有任何问题,只是您应该使用$this-&gt;request-&gt;data['Feedback']['..'] 而不是$this-&gt;data['Feedback']['..']。不过,我认为这不会对您的验证错误产生影响。
  • 所以你是说当你发布 same 数据时,如果你离开is('post') 检查,它只会失败验证?这没有多大意义:/
  • omg,jQuery 正在删除我的单选按钮以创建一个 5 开始的评分栏,这可能导致蛋糕显示代码出现问题。我在验证中添加了另一个测试字段,而 cake 只是跳过收音机并进入这个新字段。我觉得很愚蠢,很抱歉没有先检查一下:-(

标签: php validation cakephp controller


【解决方案1】:

使用 empty($this->request->data) 测试表单是否已发布:

if (!empty($this->request->data)) {

    // form posted

    if ($this->Feedback->save($this->request->data)) {
        // save successful
    }
    else {
        // errors happened
    }

}
else {

    // form not posted yet

}

【讨论】:

  • 这并不完全正确,因为 $this-&gt;request-&gt;data 可能已在其他地方填充。使用$this-&gt;request-&gt;is('post') 是检查表单是否真正为POSTed 的最佳方式。
  • 我认为鉴于示例不太可能将其填充到其他地方,但我承认你的观点。
猜你喜欢
  • 2017-03-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
相关资源
最近更新 更多