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->refe开发者_C百科rer() 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
Try this http://bakery.cakephp.org/articles/binarycrafts/2010/01/20/persistentvalidation-keeping-your-validation-data-after-redirects-2
Just remove $this->redirect($this->referer()); altogether.
Also, if you have your field validations set up properly, how is the user even getting to the redirect? the save() function should fail if invalid data is entered and the user will never see the redirect and the db will never accept any data.
精彩评论