I've written some code to allow easy asynchronous validation of form
contents and thought it might be the kind of thing mochikit would
consider including. Basically, calling
setupValidation("myformid", "validation_url");
replaces the form's onsubmit handler with one that does a json request
to the given url (with the form contents provided via formContents and
queryString), and then sets the contents of the error message elements
according to the error messages returned by the server in a simple json
format. For example, the json response
{'email': 'Please enter an email address'}
would indicate that the input with name "email" was invalid, and an
element with id "error_email" would have its innerHTML replaced with
the error message (fields without errors get their error messages
cleared, of course). An options argument, like the new Draggable
functions use, lets the "error_" prefix be customized and provides
hooks for custom logic upon validation success or failure.
This doesn't replace server validation upon actual submission, of
course... it just provides a way to also provide validation without a
page refresh, and without having to write your validation logic twice
(on the server and in javascript). It should integrate simply with any
server framework that provides validation and json. Anyway, I don't
know if this is the kind of thing that belongs in mochikit or not...
its reasonably generic, but higher level than most of mochikit. I've
pasted the code below.
Greg
-----------------------
// a slightly lower level interface that allows greater customization
// returns a deferred that, after processing the error messages
// returns a deferred that will callback with the json from the server
function validate(formname, address, options) {
logDebug("validating");
form = getElement(formname);
contents = formContents(form);
fields = contents[0];
values = contents[1];
if (options.errors_prefix == null)
options.errors_prefix = "error_";
query_string = queryString(fields, values);
deferred = loadJSONDoc(address + "?" + query_string);
deferred.addCallback(function(json) {
for(i in fields) {
field = fields[i];
// the error element doesn't necessarily have
to be a span
span = getElement(options.errors_prefix + field);
if (span != null) { // if an error element for the
field exists
if(json[field] == undefined)
error = " ";
else
error = json[field];
span.innerHTML = error;
}
}
return json;
});
return deferred;
}
// the default, simpler interface
// if validation fails, the error messages get added
// if validation succeeds, the form gets submitted for real
function setupValidation(formname, address, options) {
form = getElement(formname);
if (options == undefined)
options = {};
// the default onvalid handler submits the form
if (options.onvalid == undefined)
options.onvalid = partial(
function(f, json) { log("default
onvalid");getElement(f).submit()},
formname);
if (options.oninvalid == undefined)
options.oninvalid = noop;
form.onsubmit = function() {
d = validate(formname, address, options);
d.addCallback(function(json) {
if (json == true) {
options.onvalid();
}
else {
options.oninvalid();
}
});
return false;
}
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"MochiKit" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/mochikit
-~----------~----~----~----~------~----~------~--~---