CakePHP preserving validation errors after redirecting

匿名 (未验证) 提交于 2019-12-03 08:59:04

问题:

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

回答1:

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



回答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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!