I agree with Ryan. I do all my fancy validation on the client. My server side returns a generic error message and a numeric code, which I can cross reference with my private error documentation.
For what it's worth, it's been years since I kicked it old school and did an html form submit. Ajax is a far superior user experience. 100% of my pages are like this: 1) put all my fields on the page, 2) assign a javascript function to a button 3) in the submit function I create an array of the fields I wanna post, including any validation 4) stringify the object and 5) post it using jquery ajax.
Google is drowning in examples of this, but here are some code snips for reference.
(these examples use jQuery)
1) html has various input elements, like
<input type="text" id="username" name="uid" /><input type="password" id="pwd" name="pwd" /> (NOTE: the 'name' attribute is critical - otherwise you'll bang your head on the desk wondering why seralizeArray is skipping your fields.)
2) on the submit button action, either manually reconcile your values, such as:
- myobj = {};
- myobj.user = $("#uid").val();
- myobj.pass = $("#pwd").val();
- OR- use jquery's serializeArray to just grab the whole form.
- myobj = $(":input").serializeArray()
I do it manuallly when I have fields with complex data, or I want to do additional validation.
3) in your ajax call, stringify the data (this will go to the server as a JSON object)
$.ajax({
async : false,
type : "POST",
url : "/myWebPyMethod",
data : '{"args":' + JSON.stringify(myobj) + '}', contentType : "application/json; charset=utf-8",
dataType : "text",
success : function(response) {
alert("woot!");
},
error : function(response) {
alert("wah wah wah....");
}
});
4) in web.py get hold of your args
def myWebPyMethod(self):
try:
import json
data = web.data()
foo = json.loads(data)
print foo
print foo["user"]
except Exception, ex:
raise ex
Have fun!