You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Zachary Bedell <za...@thebedells.org> on 2012/02/15 16:34:40 UTC

Wicket jQuery Validator integration

Good morning,

Reading a recent thread about accessing jQuery Validation from Wicket reminded me that I've developed some code that might be of use.  I'm not sure if this is something anyone else would be interested in or if it's something that might eventually be integrated into Wicket core or if it would be more appropriate for one of the existing jQuery/Wicket integration libraries.  I wanted to describe what I've cooked up so far.  If this is anything that would be useful, I'd be willing to clean the code up a bit to extract a few bits that are specific to our environment and post the code somewhere.

My intent was to get client-side validation using Wicket's existing validation classes without requiring AJAX calls to make them work and preferably without requiring Page's to include lots of unsightly JavaScript.  Also, not duplicating validation logic on the client & server tiers was desirable.  The code was originally developed for a site that was expected to receive a high amount of traffic in a short period of time, and avoiding unnecessary server calls was a priority.  

I created a subclass of Form (ClientSideValidatingForm) which examines each FormComponent (and sub-Form) added to it and extracts information about the standard Wicket validations.  It generates JavaScript which uses jQuery's Validator library to apply client-side checks equivalent to the Wicket server side checks.  The nice thing about this is that you get client-side validation for "free" just by adding the normal Wicket validations plus you still get all your validations backed up in the server side in case JavaScript is unavailable or disabled.

The implementation of the class does lack a bit in terms of elegance unfortunately.  As the Wicket validation interface doesn't currently know anything about JavaScript, it was necessary to run a chain of instanceof checks against all the known Wicket validations and emit JavaScript to mirror their logic.  I also created an extension of the Wicket IValidator interface which can provide JavaScript functions to perform validation equivalent to the server-side Java code.  The extraction code in ClientSideValidatingForm preferentially checks for this interface and uses provided JavaScript if available.  Otherwise, it's a bunch of instanceof's to check for known validations or log an error if an unknown instance of IValidator is found.

Long term, it would be helpful if the stock Wicket IValidator interface could include a method to get JavaScript validations for all of the stock validations.

The code is pretty tightly bound to jQuery Validator at this point, but it's likely the methodology could be abstracted to other validation frameworks.  It's also built using WiQuery, though that's mostly as a convenience to get the same version of jQuery that we use elsewhere.  The same could easily be implemented without WiQuery provided a version of jQuery was contributed to the response via some other mechanism.

Is this something anyone would be interested in?  

Best regards,
Zac Bedell
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Wicket jQuery Validator integration

Posted by kbhuvi07 <kb...@gmail.com>.
Hi,

I am more interested to know about 'Wicket jQuery Validator integration' .
Could you please post some more information with example. That would be
really great!

Thanks in advance! :)




--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Wicket-jQuery-Validator-integration-tp4390824p4650721.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org