本文介绍了CakePHP元素错误处理问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的登录和注册表单在引用users控制器和login()和register()操作的元素。例如,当我在模式或页面控制器操作中使用此元素时,当用户具有验证错误(即模型验证)时,它会重定向到登录/注册操作页面(而不是更新元素)。

I have my login and registration forms in elements that reference the users controller and login() and register() actions. When I use this element in a modal or on a page controller action, for example, it redirects to the login/registration action page (rather than update the element), when the user has validation errors (model validation, that is).

我意识到我可以使用Javascript / JQuery验证,但是有什么方法让CakePHP的模型验证更新元素,而不是在错误发生时重定向到原始操作的页面?

I realize that I could use Javascript/JQuery validation, but is there any way to have CakePHP's model validation update the element rather than redirect to the original action's page when errors occur?

推荐答案

我已经通过。设置它的方法是在 UsersController 中包含 PersistentValidation 组件,以及任何其他控制器计划包括登录/注册元素。

I've solved this issue in the past with this PersistentValidation component. The way you set it up is by including the PersistentValidation component in your UsersController, and any other controller where you're planning to include the login/register elements.

您的登录/注册表单将提交到他们各自的控制器操作。如果验证失败,操作将重定向回引用页面。例如:

Your login/registration forms would submit to their respective controller actions. If validation fails, the action would redirect back to the referring page. For example:

if (!$validated) {
    $this->redirect($this->referer());
}

由重定向到的操作调用的视图将自动播种

The view invoked by the action to which you redirect will be automagically seeded with the validation errors from the previous request.

通过将验证错误存储在会话变量中,并在重定向后检索这些错误,这在幕后工作,它们可用于当前请求的视图/元素。你可以自己做,但组件使它很容易使用。

This works, behind-the-scenes, by storing the validation errors in a session variable, and retrieving these errors after the redirect, making them available for the current request's views/elements. You could do this yourself, but the component makes it very painless to use.

这篇关于CakePHP元素错误处理问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 04:42