I'm looking for a way to validate various forms which each require
different information, but discrete chunks of information could be
easily validated by simple rules (e.g. checks for valid email address,
url, zip code, telephone, etc…)

In the past, it was satisfactory to make self-submitting PHP pages
that include some validation code, and if errors were found, the form
would be redisplayed (with data intact), errors displayed, invalid
fields highlighted with a CSS error class, etc… I was using a small
PHP include to validate the most common data, and any corner-case
rules, I'd write into the page itself. As a finishing touch, I'd add
jquery validation (I've used about 3 or 4 different validation
plugins), writing matching rules.

Now I'm getting more requests for forms, and this duplication of rules
is getting absurd. I don't think that what I'm looking for is magic;
it seems like I can see all the pieces scattered about, but my own
efforts to do this as a novice are stumbling and excruciating.I've
been searching the web and the newsgroups with dozens of different
combinations of terms hoping to find a solution that will work the way
I'd really like it to.

==== JS disabled ====
- user fills out form, clicks submit
- form is submitted to server-side PHP script
- script switches to PHP-friendly output since the submission doesn't
appear to be via ajax*
- script uses hidden form values (placed inside a noscript tag on the
form) to determine required or conditional fields
- if there's no errors, script passes the data on to a success handler
(cgi form, generated email, etc…) and displays a success message
- if there are errors, the original data is returned to the form along
with an error array
- form redisplays, with entries intact and error messages displayed,
invalid inputs highlighted with a css class, etc…

==== JS enabled ====
- user enters data, changed fields
- jQuery submits the input to the server-side PHP script, along with
any specific rules it should be validated through (required, numeric,
email, etc…)
- script switches to json or other jQuery-friendly output since the
submission was made via ajax*
- if there's no error, script returns success back via ajax, and
jQuery runs the associated success action (add a success icon, show/
enable dependent fields, allow full submission (which would
effectively be the noscript version listed above), etc…)
- if there's an error, script returns error message, and jQuery runs
the associated success action (display returned error, add error
class, send user to 4chan, etc…)

* I've seen this used to switch/case the server-side of things:
return ((!empty($_SERVER['HTTP_X_REQUESTED_WITH'])) && $_SERVER
['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest');

As far as which error is displayed when there are multiple conditions,
a general progression of severity seems reasonable - i.e. required >
must be x > noncritical (what, your name has no capital letters?) >
optional

I found these sibling scripts (whose rules are effectively
interchangable), but rulesets are *still* defined twice, once server-
side, and once in the javascript:
http://www.benjaminkeen.com/software/php_validation/
http://www.benjaminkeen.com/software/rsv/jquery/

I also stumbled into this, but I can't find much info on it (probably
dead/stalled - anyone know?); I think it might work out if I can
figure out how to make a mashup with the server-side version of the
above:
http://deadguy.reliccommunity.com/stuffbox/js/jquery-jvalidate.js

Jörn's plugin seems to prefer its internal methods for the usual
suspects, reserving remote() for the more esoteric validation
conditions (I know extensions are in the works). I'm just boggled that
I can't seem to find validation-lite.js or something; server-side
validation that only gets sweeter when a dab of jQuery gives it that
jewel-like shine.

Anyway, I'm still a million miles away from reaching even novice in
either js or php, and could really use some good advice/solutions.

Reply via email to