views:

108

answers:

2

I'm struggling to find the best way to display validation messages after a form which has been generated via ajax is submitted. Let me explain better: I've got a page that starts with a few options. Based on what the user selects from dropdowns and by adding new fields, the form is created and at the end the form is submitted.

The problem is that, even if I can do validation checks on the server, and display a general validation message on the page, I should pre-fill the form fields with the values that the user entered before pushing the submit button.

Have you ever had such a problem? How did you address this?

A: 

There's a couple ways I could see handling this.

1) Do another AJAX call upon submission that does the validations and returns w/o a full post. It's not an elegant solution since it involves two round trips and depending on the validations would lead to performance issues.

2) When you return the json that is used for extending the form, have it instead turn 2 sets of json objects. The first set of objects would represent the data you're currently using. The second set in the array would represent the validation logic which you can then apply however you need upon the form's creation.

Neither way is truly elegant; however, the latter provides a way minimize round trips to do the validation.

JamesEggers
Yes, I was just thinking of just doing an Ajax POST instead of a normal POST and have it return possible validation errors... The second approach is not possible the validation logic is not possible on the client side
CodeClimber
+1  A: 

Three options:

  1. normal POST -> the server render the form AS it was submitted (you should be able to rebuild the form server side)

  2. ajax POST -> just redirect the user if validation succeeded

  3. check before post -> synchronous ajax validation calls

Andrea Balducci
I'm going for option 2Option 1 is too much complicate.. I'd have to send the whole model back to the page just for the sake of showing the validation errors.Option 2 has his problems as well: I've to manually display validation messages
CodeClimber