Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP preserving validation errors after redirecting

I have an element which displays a comment form, the action is comments/add. If the form doesn't validate, I don't want users to go to comments/add (the view for which does not exist), but I want them to remain on the same page and see the validation errors there.

However, redirecting to $this->referer() doesn't work - the validation errors disappear and just the flash message remains.

public function add(){
    if (!empty($this->data)){
        $this->Comment->create();
        if ($this->Comment->save($this->data)){
            $this->Session->setFlash('Comment added.','success');
            $this->redirect($this->referer());
        }else{
            $this->Session->setFlash('There was a problem adding your comment.  Please try again.','failure');
        }
    }
}

How can I either retain the validation errors and form data on redirect? If this isn't possible, how else can I solve the problem?

Thanks a lot,

Will

like image 994
Will Avatar asked Nov 29 '25 03:11

Will


1 Answers

Try this http://bakery.cakephp.org/articles/binarycrafts/2010/01/20/persistentvalidation-keeping-your-validation-data-after-redirects-2

like image 118
RSK Avatar answered Dec 01 '25 04:12

RSK



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!