CakePHP Element Error Handling Question

前端 未结 1 1424
星月不相逢
星月不相逢 2021-01-14 03:01

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 co

相关标签:
1条回答
  • 2021-01-14 03:38

    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.

    0 讨论(0)
提交回复
热议问题