Justin
Justin

Reputation: 171

CakePHP Element Error Handling Question

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).

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?

Upvotes: 4

Views: 567

Answers (1)

Daniel Wright
Daniel Wright

Reputation: 4604

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.

Upvotes: 3

Related Questions