This post originated from an RSS feed registered with PHP Buzz
by Alan Knowles.
Original Post: Validating javascript and flexy
Feed Title: Smoking toooo much PHP
Feed URL: http://www.akbkhome.com/blog.php/RSS.xml
Feed Description: More than just a blog :)
I've been working on a typical web application, and given the fact that deadlines are not a big issue for a change, a little focus has gone into thinking 'is there a better way to do this?'
The challenge is really, how to do client side javascript validation and at the same time add the ability for flexy to pick up the calls and perform the matching serverside validation..
Like alot of my client side javascript, i'm going with a model that basically involves adding these lines to the end of the file:
The onload method adds a onsubmit hander to all the forms, and which in turn looks for all the input elements and does the required validation.
I've tended to avoid onchange handlers on the form, partly as I feel it is a bit confusing for an end user to be continually be reminded that they have got something wrong, even while they are filling in the form (and perhaps havent finished entering the data)
the onload'er method is also quite nice, as it doesnt involve to many changes to the form.
Now comes the crunch.. where to add the checks for validation, A long while ago I tried something like this:
Using javascript, it should be easy to extract the rule, split it on the ');' bit, and eval each of the expressions, using the input's value.
On the Flexy side, it does involve a little messing around to convert the tests to pear's Validate class, but the principle is quite nice in the fact you can test the validation just by viewing the template, without actually installing it in the application and running it through the template engine.
Javascript allows me nicely to change the background colour if there is an error (either directy or setting it's style), but again another issue raises its head: Where do I put the message about what is wrong.
On the simple version you really want it to stick add a div layer, and put a message under the box.
On a more complex version, you want it to pull the phrase from a hidden element on the page, and display a translated version if applicable.
The other thing to consider is how to re-integrate the error messaging with stuff coming back after a post (on the extremely unlikely situation these days that someone has turned of javascript )
But at least it did make me think that I should javascript enable the comment boxes on my blog, only making the comment box appear if you are using javascript.. - at least it should stop a few blog spammers.