CakePHP Element Error Handling Question

cakephp, php, validation

Solution

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.

Problem

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?

Original source