[mochikit] Form validation example

2007-08-28 Thread [EMAIL PROTECTED]

Since responding to the other topic was reposting old messages of
mine, I've decided to try a new topic with the example and code, so
that it's less confusing.

I'l start with the basics.  The premise of my form validation script
is that the validation occurs server side, and I use javascript to
post the form and interpret results from the server.  These are the
basic steps:

1. User fills in form and submits.
2. Server validates field values and responds with a none if there
are no validation errors or json data describing the errors.
3. Javascipt in the browser confirms the submittal or displays error
information, depending on what the server sent back.
4. If necessary, user corrects form and resubmits.

The form isn't a true form.  There isn't a form tag.  For every
field that will be validated there is a corresponding span to
display any validation errors, if they are needed.  The naming system
is pretty straight forward.  For a form field with an id lastName
the error message span has an id lastName_error.

We'll start with a really simple form with one field, like so:

input type=text id=lastName value= /span
id=lastName_error/span

You'll want to activate the submit with a connect like this:

connect(some_submit_button, onclick, submit_handler);

We submit it with an Ajax post using this javascript code:

submit_handler = function() {
var url = /server_script_url/;
var field_list = ['lastName'];
var error_span_list = ['lastName_error'];
i = 0;
while(i  error_span_list.length)
{
$(error_span_list[i]).innerHTML = ;
i++;
}
var lookup = function(field_list){
return $(field_list).value;
}
var field_values = imap(lookup,field_list);
var d_querystring = queryString(field_list,list(field_values));
var d_setup = getXMLHttpRequest();
d_setup.open( POST, url, true);
d_setup.setRequestHeader('Content-Type', 'application/x-www-form-
urlencoded');
d_setup.setRequestHeader('Cache-Control', 'no-cache');
var d = sendXMLHttpRequest( d_setup, d_querystring);
d.addCallbacks(handleServerFeedback,handleServerError);
}

This code also clears any error messages the page was displaying when
the form was submitted.

Next the server script goes through validation.  In this example we'll
assume the field lastName isn't allowed to be blank.  If the field has
some text in it the server will send back the string none.  If the
field is blank the server will send back a json formatted string like
this:

{error:[{field:lastName,error_message:lastName can not be
blank.}]}

Now we need the javascript that handles the response from the server.
These will be the functions handleServerFeedback and
handleServerError.  First, handleServerFeedback:

function handleServerFeedback(d_setup) {
if ((strip(d_setup.responseText)) == none)
{
// code for a successful submit
}
else
{
error_obj = evalJSONRequest(d_setup);
error_array = error_obj['error'];
i = 0;
while(i  error_array.length)
{
error_id = error_array[i].field + _error;
$(error_id).innerHTML = error_array[i].error_message;
i++;
}
}
}

The else portion of this function goes through the json string and
puts the error messages where they belong.  Next is handleServerError:

function handleServerError() {
// code for a server error
}

The nice thing about this is that to submit more fields you just
expand the list.  Here's the field and error list with an additional
field:

var field_list = ['lastName','firstName'];
var error_span_list = ['lastName_error','firstName_error'];

To get the code I show above, I've cut out large portions of my own
scripts.  For example, I pass the values to my submit handler, I don't
hardcode them in the function.  I also display feedback to the user
about what's going on.  I use ColdFusion to do my validation,
primarily the IsValid function.  Between that and string functions I
can validate any data type I want.  If the validation is successful
the script can go right into a database update or insert.

I hope this helps, please let me know if I fat fingered any of the
code above.  Also, feel free to post questions or criticisms.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: Form Validation

2007-08-28 Thread [EMAIL PROTECTED]

Geez, now my 1st post shows up a day late.  Anywho, ignore the first
long post, plz.


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: Form Validation

2007-08-28 Thread Leo Soto M.

Coming late to the thread, but anyway, this could be useful:
http://ui4w.sourceforge.net/UI4W/packed/doc/html/Form.html

Examples: 
http://ui4w.sourceforge.net/UI4W/packed/samples/Custom_Form_Validator.html
http://ui4w.sourceforge.net/UI4W/packed/samples/Simple_Manual-Layout_Form.html

It's focused on client-side validation, so doesn't directly supports
asynchronous validation.

-- 
Leo Soto M.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Comparing dates

2007-08-28 Thread grum

Hi everyone,

I'm having problems comparing the following:

isoTimestamp(2007-08-13T04:00:00+00:00)

which gives me:

Mon Aug 13 2007 00:00:00 GMT-0400 (EDT)

and:

Date()

which (right now) gives me:

Tue Aug 28 2007 16:14:12 GMT-0400 (EDT)

ie. isoTimestamp(2007-08-13T04:00:00+00:00)  Date() is returning
false.

I think it's something to do with the lack of quotes around the result
of isoTimestamp - though quite what that means i'm not sure.  Please
someone help me out, i really need to be able to compare these dates.

Thanks,

Graham Hagger
MochiKit Noob


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---



[mochikit] Re: Comparing dates

2007-08-28 Thread Bob Ippolito

On 8/28/07, grum [EMAIL PROTECTED] wrote:

 Hi everyone,

 I'm having problems comparing the following:

 isoTimestamp(2007-08-13T04:00:00+00:00)

 which gives me:

 Mon Aug 13 2007 00:00:00 GMT-0400 (EDT)

 and:

 Date()

 which (right now) gives me:

 Tue Aug 28 2007 16:14:12 GMT-0400 (EDT)

 ie. isoTimestamp(2007-08-13T04:00:00+00:00)  Date() is returning
 false.

 I think it's something to do with the lack of quotes around the result
 of isoTimestamp - though quite what that means i'm not sure.  Please
 someone help me out, i really need to be able to compare these dates.

JavaScript's built-in comparison operators are no good for comparing
objects, only strings and numbers. This is why MochiKit.Base.compare
exists.

-bob

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
MochiKit group.
To post to this group, send email to mochikit@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/mochikit?hl=en
-~--~~~~--~~--~--~---