What is the best way to repopulate a form and display errors with no-script?

Go To StackoverFlow.com

1

I'm building an application which uses ajax calls for form validations and some others, problem is that I don't like it when those validation errors shows up on a new page with no css at all and form completely gets reset without the javascripts.

I'm talking about a dozen forms so, it will be quite annoying for the user to go through forms over and over again.

I've read some answers on this topic, but is there a specific way, a best-practice for a situation like this (more along the lines of form validation)?

Apologies if there is an answer already.

Edit:

All validations are done on the server-side, currently the errors are being displayed with the help of ajax. What I'd like is a convenient method for repopulating the form and display errors without javascript if an error occurs while processing the form.

2009-06-16 18:21
by saint
I'm not sure I'm following what the problem is... what do you mean by 'no html, css' - Paolo Bergantino 2009-06-16 18:39
i mean form submission event is handled by ajax so the errors while processing forms gets echoed to a certain div, without the script obviously errors get displayed directly, and that's really not the question, i've edited the question now..sorry for the confusio - saint 2009-06-16 18:57


4

I'm not sure if you're implying by your question that you don't validate on the server - this would be considered a huge security hole as quite obviously forms could be bypassed by simply disabling Javascript. You have to validate in the server every time, while client-side validation is simply nice to do.

Furthermore, the "problem" of having the forms reset themselves when the page is reloaded becomes moot when you use a decent framework - In Python, Django auto-repopulates forms in the case of errors. In PHP, CakePHP and CodeIgniter do as well, and I'm sure most good frameworks do this, as it falls under the category of "common tasks" that frameworks are supposed to abstract away.

Whenever I'm not using a framework, I write up my own little Form class that fills the need.

EDIT:

I am not aware of anything you could plug-in to what you have to be able to repopulate the form. Your best bet is writing your own little class for it, which should take no more than 1 hour.

2009-06-16 18:25
by Paolo Bergantino
I wish that was the case, I like CI myself, thing is I'm just extending something that's already there..so no framework for me. - saint 2009-06-16 18:33


1

What I normally do is store all the error strings in an array in a session variable (I use PHP). If there's an error on the server-side, redirect the user back to the form page, check for the session var, and display the errors as appropriate. Make sure you unset the session var to free up memory after you display it. Same goes for repopulating the form -- values in a session var.

2009-07-14 20:55
by Calvin
Ok..so to repopulate you mean to say we have to save the values in a session and then while redirecting the user to the form page, display the session. I have to do this, so i will use sessions as you mentioned. Thank - noobcode 2011-05-25 05:26


0

Form validation should be written on the server first, on the client later. You should be validating on the server in any case. Don't rely on Ajax for something as simple and as crucial to the operation of your site as form validation. Using unobtrusive scripting, you can prevent the default form submittal and then process the form using Ajax. Otherwise, allow the submit to go through and respond with a server-generated validation page.

2009-06-16 18:25
by Andrew Noyes
all validations are done on the server side,its not quite the problem..problem is its raw strings no html no css, and i'm trying not to include any if that's possibl - saint 2009-06-16 18:30
I don't follow. What do you mean raw strings? You mean exceptions being thrown by a PHP script, or something like that - Andrew Noyes 2009-06-16 18:46


0

Perform your validations on the server-side and repopulate the forms.

Validation via AJAX is nice, but cannot be enforced. Thus, it is more effective to make the validations be performed whether or not the AJAX happens. I would suggest a second route that can handle the validations and pass back an array of errors in JSON for those who have javascript. But for those without javascript, the only way your validations will work is through the server-side.

You don't really get a choice in this, unfortunately. Any halfway decent programmer or hacker with firebug can edit your javascript, detach hooks, and get around everything you're trying to do on his side: be paranoid and enforce validation on the server side.

2009-06-16 18:27
by Robert K
as i mentioned it uses ajax calls for validations, all validations are done on the server side. It just outputs the errors with the help of ajax - saint 2009-06-16 18:36
You missed my point: make all validation occur on the server side, and intercept the onSubmit event and use AJAX there to handle the dynamic validation. If they have no script they proceed to the post page, which validates the form anyway - Robert K 2009-06-16 19:36
I totally got your point, u just described the current scenario. if they have no script they will proceed to the post page as u mentioned. Problem is that form will get reset once that happens - saint 2009-06-17 03:18
Ads