0
votes

Working on my first CakePHP 'project'. Im having problems getting CakePHP (2.3) to show validation messages correctly on submit when i add $this->request->is('post'). If i remove it i get the validation messages (good!), but it validates and shows them on page load.. (bad!).

Edit: to clarify: The problem is it isnt showing validation errors automagicly.

/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' );
        }
    }
}
1
What's the problem? When you submit and check is('post') it skips validation? Or it doesn't show the message? Showing the rest of your action may let us know what's going on. - jeremyharris
@jeremyharris it doesnt show any messages. - xAndyx
Hmmmm I see nothing wrong with your code, other than you should be using $this->request->data['Feedback']['..'] instead of $this->data['Feedback']['..']. I don't think that would make a difference with your validation errors though. - Hoff
So you're saying when you POST the same data and it only fails validation if you leave off the is('post') check? That doesn't make much sense :/ - jeremyharris
omg, jQuery was removing my radio buttons to make a 5 start rating bar, that probably is causing issues with cake display code. I added another test field to validation and cake just skips the radios and goes to this new field. I feel stupid, sorry for not checking this out first :-( - xAndyx

1 Answers

-1
votes

Use empty($this->request->data) to test if the form has been posted:

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

    // form posted

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

}
else {

    // form not posted yet

}