Step Form Validation

I’m creating a form that is split up into 5 steps.

By the way this is just one form - and not 5 separate forms. I have a continue button on each step, which is just an image button - when you click on the image button it hides the current step and displays the next step.


$('#step-2-continue-button').click(function(){

    $('#step-1').hide();

    $('#step-2').fadeIn();

});

Now I need to perform AJAX validation on each step. I have read that I can use


$model->validate();

and specify the attributes but how do I do this on each step? As I am not actually submitting the form on each step, how do I make the image button call the validation function?

Hi GStar,

why not split your form to five subforms? you could set enableAjaxValidation to true for each form

and in last step you could calls an action that save all of models.

In any case you could make five actions (or one with parameter) which accept ajax requests and returns message "success" or specified form with errors (by renderpartial). Then according to the data ("success" or html form) client server (javascript) allows the user to fill the next form or not

No because the last step needs to do a normal post request and save all fields to the database. That’s why it needs to be one form.

So, the solution could be the second option I mentioned. Make five ajax-mode action (or one with paramater) in the same controller. If you have one model you should use scenarios in your model that settle the specific fields for each state as required. In the last scenario you have to settle all the fields as required.

How do I make it return / display validation messages if I use my own ajax methods?

Basically I want to use the built-in functionality as much as I can rather than duplicating it.

You can set validation on fields according to your requirement on the form that render… so on applying scenario on particular action the validation will be applied.

And displaying validation message you can you setFlash() and getFlash() method for displaying your custom message.