Sorry, but you need to login to access this location.

How to validate an Ajax-enabled form?

I've got a form that pulls info through an ajax request out of my controller. Now I'd like to validate the form fields when the ajax request is initiated. I do this by calling the validates() method in the function that executes the ajax request like so:


function doAjaxRequest() {
    $this->Order->Set($this->data);
    if($this->Order->validates()) {
        //do ajax stuff here
    }
}

Now, when the form doesn't validate, I want to display the usual error messages next to the form fields that weren't filled in right. Yet, using above code it still executes the ajax request (the view of doAjaxRequest() is called). So I probably need to abort the ajax request and return the errors to display next to the form. What would be the right way to do this? Thanks

Asked by watchout, on 1/6/10

1 Answer

Use $javascript->object($this->validationErrors) to echo json object of the errors and the messages. Your javascript should handle json gracefully and show the errors the way you want it to.

Answered by mmhanon 3/6/10

Thanks for your answer.

So to my understanding, I'd need to validate prior to executing the ajax request right? Instead of doing the validation in the controller, I'd need to do echo the json object from the view. Right?

watchout - on 3/6/10

<< comments | 1 | 2
<< previous next >>

Your Answer

You can use Creole Wiki Syntax to format your text.

Tagged with

Rating

0

Viewed

218 times

Last Activity

on 3/6/10