Reputation: 1933
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
Upvotes: 3
Views: 2608
Reputation: 4517
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.
Upvotes: 0
Reputation: 17516
Upvotes: 1