2

Here is what I would like to be able to do:

  1. Display a dialog box containing a form to create a new record (for example a user)
  2. Form is submitted via AJAX.
  3. Get response that can be either the same form with validation errors or the new record in JSON format.

Some additional information:

  • I am using partials to render the form. So when the dialog is first displayed I make an ajax call to get the empty form markup and when the form is submitted I will use the same partial if the form has validation errors.
  • If the response comes back and there are validation errors then I just want to replace the existing form markup with the returned markup.
  • If the response comes back and there are not validation errors then I would close the dialog and take the JSON formatted object and make some other changes to the page.

I some parts of this working fine. The problem is that if I were to pass back a JSON formatted object on success instead of HTML I can by definition never be sure what the return type will be. My first though was to always return JSON from AJAX calls. If the form had validation errors the returned JSON would have a property called "html" that would contain the updated form markup. If there are not validation errors then the returned JSON would have a property called "data" which contains the newly created object.

I think my solution will work, but I wonder if anyone else has come up with something more elegant. I would imagine this basic flow must be common in other applications, so I can't be the only one looking for a solution to this problem.

2
  • 2
    Why don't you use client side validation? Commented Feb 18, 2011 at 19:10
  • 2
    I would like to add client side validation just to improve the user experience, but I still need to do validation on the server side to ensure that the data coming in is valid and that the client side validation hasn't been bypassed in any way. Commented Feb 18, 2011 at 19:16

1 Answer 1

4

You mentioned you'd like to add client-side validation... then you will need a way to display validation errors in the client-code. If you have that, then instead of returning HTML from your AJAX call, you can instead return a list of validation errors in JSON, and plug those into the same error-display methods you have for the client validation.

Sign up to request clarification or add additional context in comments.

1 Comment

I guess I need to investigate what options I have for client side validation. My initial thought was that it would be much easier to re-render the form on the server side and include things like descriptive error messages and invalid field highlighting, but maybe the frameworks that are available will make this easy to do on the client side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.