Saturday, April 21, 2012

jQuery validation plugin per-field AJAX

UPDATE: sigh. after looking for solutions, not finding any, making this one... I stumble across:

Now I'm gonna read up on those..

Well after tinkering around a bunch with jQuery validation plugin, I managed success doing AJAX validation per field, vs. over the whole form.  There's lots of buggy example code out there, they suck.  This one works - albeit ugly:

 $(function() {   
     var response;  
     var retrigger = 0;  
   $.validator.addMethod("uniqueUserName", function(value, element) {  
     if(retrigger == 1)  
       return true;  
       type: "POST",  
       url: "/TestHandler.ajax.axd",  
       data: "ajaxPost="+element.ID + "&value="+value,  
       success: function(msg)  
         response = (msg == 'True') ? true : false;  
         retrigger = 1;  
         retrigger = 0;  
     return true;  
   }, "Username is Already Taken");  
   $.validator.addMethod("uniqueUserName2", function(value, element) {  
     return response;  
   }, "Username is ALready Taken2");  
   var test2 = $("#Middle");   
   //var test2 = $("#contact-form#Email");   
   test2.rules("add", {  
     uniqueUserName: true,  
     uniqueUserName2: true  

So the basic concept is to allow jQuery validator plugin to manage all the focus/keyup etc. events and call us when we're really interested in validating something.  Most code examples out there do the "uniqueUserName" but not the "uniqueUserName2".  Long story short, AJAX is a 2-step process - the callback comes much later, so "uniqueUserName2" is our placeholder for that.

Flow goes as follows:

  • You type something
  • uniqueUserName validator runs and kicks off AJAX request.  It always succeeds in validation, so it's kind of a dummy just to kick off the request
  • at some point, the AJAX success function runs.  We set our retrigger to 1 and brute force validation again on the same field.  
  • uniqueUserName validation runs again but this time we abort out before making the AJAX call - still returning success.  Without this retrigger check, you'll keep sending AJAX requests forever.
  • finally, uniqueUserName2 is evaluated and it is patiently waiting around with the results of our last AJAX response.  
There are some good jQuery validation frameworks out there, this one in particular actually appears to be the least 'developed' featurewise, but its extensibility is hard to beat.  Some other good ones I found are:


Validation Engine looks really powerful, but I felt constrained by how to present the errors - although his error reporting is perfectly adequate.

Valid8 also looks powerful, but I wasn't confident it was being maintained 

Worth mentioning are both of these actually tout AJAX support, where jQuery validation plugin does not.  But as you can see it's a little clunky but not too difficult to get it working.  

Oh Javascript.  It's like writing untyped C code all over again.... and not in a good way.

Tuesday, April 17, 2012

Apprentice Open Source Edition now available!

After much deliberation, I've decided to piecemeal Apprentice out into the open source community. Certain elements will remain top secret and/or closed - but some juicy goodies are now free and available.  Starting small, but I will be revealing more features on a fairly consistent basis.

Check it out here:

Included presently are:
  • IoC Cache Facade
  • Logger & Logger facade
  • Initializer 
Coming soon:
  • IoC Cache Facade implementations for ASP.NET, Memcached and MSEL

Friday, April 13, 2012

jQuery, validation & clueTip

The sordid world of validation continues to turn. Validation is the red headed step child of development. Ignored, neglected and even scorned it rarely garners the attention it deserves. After you're done twiddling those javascript tricks that please your brainless marketing trendoids, it's time to turn your attention to that sneaky little detail of data integrity. After the fanfare has died down, the tiki torches dark we're left with business web forms gathering data. And are they going to gather the data well? Is the backend+frontend going to be a frightful mess of spaghetti? Frequently this answer is yes. I for one say no, this is not what we should accept. It's been this way for years and needs to end. jQuery has made a move in the right direction with a cohesive and sensible client validation component, which itself has extensive plugin points and ajax capability. The last hurdle, and it is not insignificant, is marrying this to a back end which can feed the hungry baby and keep it from descending into the blob of unmaintainable code that javascript loves to be. Oh Silverlight, why could you not have become more popular? So this became a rant instead of a useful discussion on the title. Sorry about that. Next time I'll do better.

Thursday, April 12, 2012

.NET Apprentice library, where next?

For years, literally, I've debated taking my prized C# closed-source library to the open source community.  It's served me well in its various forms for numerous clients.  However, it (or I) am not ready to give away the keys to the kingdom just yet.

On one hand, I want to.  It's far more work than a single man can complete alone.  Furthermore, I'd love for it to be able to help others.

On the other, there are really some special and unique things in there that just don't feel right to plainly give away, after so many years of work.

While checking some old data on my drive, I found its first iteration going all the way back to July of 2007.  That's closing in on 5 solid years of development.  And it's aged beautifully.  Lacking some refinement sure, but containing features onhand we wish we'd seen in WCF, WF and others.  I just wish I had a few folks to help me with it.

I've opened up just a page or two of the wiki, to give you a taste: