Customizing error messages using jQuery Validate Plugin for Twitter Bootstrap

jQuery Validate (http://bassistance.de/jquery-plugins/jquery-plugin-validation/) is very versatile plugin to validate forms. Although this plugin lacks detailed documentation, a quick look into the ‘jquery.validate.js‘ file gives you a lot of ways to customize various aspects of validation. I recently used this plugin with Twitter Bootstrap (http://twitter.github.com/bootstrap/). I quickly coded up a sample form and played around with the options.
The functions which were not mentioned quite briefly were ‘unhighlight‘ and ‘highlight‘. These toggle the CSS class for the element being validated from ‘error‘ to ‘success/valid‘ and vice versa. These functions are response to the ‘error’ and ‘success’ fired events. Below is a sample implementation which customizes the error messages by overriding these functions.

 $("#registerForm").validate({
     debug:true,
     errorElement:"span",
     errorClass:"error",
     validClass:"success",
     unhighlight: function(element, errorClass, validClass) {
			if (element.type === 'radio') {
				this.findByName(element.name).parent("div").parent("div").removeClass(errorClass).addClass(validClass);
			} else {
				$(element).parent("div").parent("div").removeClass(errorClass).addClass(validClass);
                $(element).next("span").addClass("okIcon");
			}
    },
    highlight: function(element, errorClass, validClass) {
			if (element.type === 'radio') {
				this.findByName(element.name).parent("div").parent("div").addClass(errorClass).removeClass(validClass);
			} else {

                $(element).next("span").removeClass("okIcon");
				$(element).parent("div").parent("div").addClass(errorClass).removeClass(validClass);
                $(element).parent("div").parent("div").css("margin-left","0px")
                                                      .css("padding-left","0px")
                                                      .css("padding-top","0px")
                                                      .css("padding-bottom","0px")
                                                      .css("margin-bottom","9px");
			}
		}

Note: The sample is missing the ‘rules’ section required for validation.

Posted in Web | Tagged , , , , , | 5 Comments

Fancybox 2 with NextGen Gallery Plugin in WordPress

I was just updating the blog to WordPress 3.3 and forgot to backup my custom edits required to integrate Fancybox with NextGen Gallery plugin. It was good in a way since I got a chance to upgrade FancyBox 1.3.4 to FancyBox 2.0.4. I have noted down all the edits required and decided to write a rough tutorial about the same .The edits are simple and can be added to any theme for that matter.

Lets start with the requirements:

  1. jQuery – This can be installed by either editing the header.php of the theme or adding a plugin (WP jQuery CDN) to add jQuery support to the entire site. I recommend adding a plugin. More cleaner and efficient to update with the advantage of loading the JavaScript library from a CDN. Gotta save that bandwidth!
  2. NextGen Gallery Plugin – This plugin will manage all the required pictures and galleries in WordPress.
  3. FancyBox 2 – Rather than finding a plugin, I added the library files to the theme directory itself.

Note: Please backup your ‘header.php’ just in case.

Step 1 : Create a sample gallery in NextGen Gallery and upload a few photos.

Step 2 : In the Admin section, Under Gallery->Options->Effects->Link Code Link, Add the following lines

class="myfancybox" rel="%GALLERY_NAME%"

Step 3 : Download the latest FancyBox 2 library and unzip it to the directory of the theme. In my case, it was ‘wp-content/themes/twentyten/fancybox’. For the version 2.0.4, you should have the ‘lib’ & ‘source’ directories under ‘fancybox’.

Step 4 : Edit the ‘header.php’. Add the following lines just above the </head> tag.

<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/fancybox/source/jquery.fancybox.css?v=2.0.4" type="text/css" media="screen" />
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/fancybox/lib/jquery.mousewheel-3.0.6.pack.js"></script>
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/fancybox/source/jquery.fancybox.pack.js?v=2.0.4"></script>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/fancybox/source/helpers/jquery.fancybox-buttons.css?v=2.0.4" type="text/css" media="screen" />
<script type="text/javascript" src="<?php bloginfo('template_url'); ?>/fancybox/source/helpers/jquery.fancybox-buttons.js?v=2.0.4"></script>

<script type="text/javascript">
  jQuery(document).ready(function() { 
    jQuery("a.myfancybox").fancybox(); 
}); >
</script>

Step 5 : Check the script and link tags for the right version numbers. The latest version of library is posted on http://fancyapps.com/fancybox/.

Step 6: Create a post using NextGen short codes to have an entire gallery displayed.

You should be able to have custom changes for FancyBox by passing options as key/value object to fancybox() method.

Posted in Web, WordPress | Tagged , , , , , | 1 Comment

Encounter with F1 Red Bull Racing Team

A few weeks ago, some friends and I took a trip to the stretch of Leh-Ladakh. Apart from the fabulous scenery and cold climate, we were lucky to witness Red Bull Racing Team capturing for their promotional video for Formula One in India. The race is to take place tomorrow in Delhi. The machine was amazing. We did not see it live in action since we just encountered it on the roads to Hemis Monastary when they were just about to pack it up. I checked up videos online to see the car in action later. I guess we were god damn lucky.




We also managed to take up shots up close.

 

Posted in Travel | Leave a comment