Shaun Mccran

My digital playground

17
J
A
N
2010

Using Coldfusion to generate JQuery validation scripts

One of the ideas that we have been throwing around the office is creating a platform that will create the form-validation-database cycle automatically. Something akin to ORM, but not. ORM looks good, but we want more control over what is happening.

In this article I am exploring the idea of automatically creating JQuery validation from a simple Coldfusion input. In this case a list of required fields. I'll say up front Ray Camden's blog entry on Jquery Validation (http://www.coldfusionjedi.com/index.cfm/2009/2/10/An-Introduction-to-jQuery-and-Form-Validation-2) has been an invaluable help.

The principle behind this is that you can create a generic validation object routine, and simple provide it with a set data object (list or struct, haven't decided yet) and have it match against a form and validate it. So with that in mind we will create a simple form.

view plain print about
1<form name="form" id="form" method="post" action="index.cfm?inline=#url.inline#">
2
3<label for="name">Name</label><br/>
4<input type="text" name="name" id="name" class="form-field"><p/>
5
6<label for="telephone">Telephone</label><br/>
7<input type="text" name="telephone" id="telephone" class="form-field"><p/>
8
9<label for="email">Email</label><br/>
10<input type="text" name="email" id="email" class="form-field"><p/>
11
12<label for="favouriteSandwhich">Favourite Sandwhich</label><br/>
13<input type="text" name="favouriteSandwhich" id="favouriteSandwhich" class="form-field"><p/>
14
15<input type="submit" name="action" value="Submit">
16
17</form>
As you can see from above, this is a simple form. Next we will include references to the Google code repository for the AJAX library, and out JQuery validation plugin.
view plain print about
1<scr/ipt type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
2<scr/ipt type="text/javascript" src="jquery.validate.pack.js"></script>
Next we have the JQuery script to validate our fields. Firstly we create a list of the fields that are required. By doing this it is incredibly easy to add another required field to the model.
view plain print about
1<cfset variables.requiredList = "name,email">
The next phase of this will probably accept a structure rather than a list, in that way I can combine field lengths and any other custom criteria, rather than merely the fact that it is required. The JQuery validation starts off in exactly the same way as usual, we create a function to catch the validation request, and use the id of the form as the reference. We declare the error container style of "#error". Next we loop over our list inside the "rules:" element. This creates the required rules for each field. I've also added a basic check for the value 'email'. This will create an email validation rule. Next we create custom messages in the "message:" element. Again this catches the email value and adds an appropriate message.
view plain print about
1<scri/pt>
2        $(document).ready(function(){
3            $("#form").validate({
4            
5            errorContainer: "#error",
6            errorLabelContainer: "#error ul",
7            wrapper: "li",
8
9            rules: {
10                <cfoutput>
11                    <cfloop list="#variables.requiredList#" index="variables.index">
12                    #variables.index#: {required: true <cfif findNoCase('email', variables.index, '1')>, email: true</cfif>, minlength: 5},
13                    </cfloop>
14                </cfoutput>
15                    },
16
17            messages: {
18                
19                <cfoutput>
20                    <cfloop list="#variables.requiredList#" index="variables.index">
21                    #variables.index#: {required: "The #variables.index# field is required",
22                    <cfif findNoCase('email', variables.index, '1')> email: "Email addresses are of the form user@host. Please enter a valid email address.",</cfif>
23                     minlength: jQuery.format("You need to use at least {0} characters for your name.")
24                    },
25                    
26                    </cfloop>
27                </cfoutput>
28            
29                     }
30                                }
31            );
32        });
33        </script>
Lastly we can create a style for the #error container we declared above.
view plain print about
1<style>
2/* Error handling styles */
3#error {-moz-background-clip:border;
4        -moz-background-inline-policy:continuous;
5        -moz-background-origin:padding;
6        background:#FFE7DF;
7        background-position: 5px 8px;
8        border: #FF3F00 solid 1px;
9        color:#440000;
10        margin:10px 0 1em;
11        padding:0px 7px 7px 7px;
12        display:none;
13        width: 90%;}
14
15/* padding for the list */
16#error ul {list-style-type:none; padding: 0px 0px 0px 0px;}
17
18/* padding for the list items */
19#error ul li {padding: 4px 0 2px 16px;}
20
21.error {color: red; list-style-type:none; padding: 0px 0px 0px 0px; width: 198px; color:#440000; background:#FFE7DF;}
22li {list-style-type:none;}    
23.form-field {width: 200px; padding: 0px 0px 0px 0px;}
24</style>

This creates a totally dynamic validation routine - all fed from a list. I think it forms a good basis to build a more dynamic rules driven model, where you can set field lengths as well.

There is an example of the complete code here, along with a variation on the inline or external placing of the validation messages.

Follow this link for a demo of the JQuery validation model.

15
J
A
N
2010

Top 5 remote working applications

With the UK currently stumbling to a halt due to snow I've been working from home. I like working from home, it has many advantages like the time saved in commuting, and the fact that you usually know your own hardware/software setup better than works.

One thing it has highlighted is that as a software developer your actual location is now totally irrelevant. Even for team based projects you do not all have to be in the same physical location. Our physical and online worlds have become so enmeshed that is it now common place to be able to work productively from anywhere.

So with that in mind I thought I'd do a quick round up of my favourite remote working applications. Some are more "best practices" with recommendations if I've been using a specific product. Warning: all recommendations may contain nuts.

1. Online Code / Asset Source Control

Having your code in an online repository is an absolute life saver. Not only does this contribute towards a "Business continuity" plan for regular working, but it means you can get to it from anywhere. We should all be using source control anyway, but having it online means that it's much more easily maintained and you are not moving code around on USB keys.

I use an application called Beanstalk. www.beanstalkapp.com. They have worked well for me so far, and the basic account is free.

2. Virtualisation

The Virtual PC software market has moved on considerably in the last few years. To the point now where there are free versions of virtualisation software that allows you to create virtual pc's inside your actual physical pc. I use this at work and at home for a few reasons. Firstly I don't even have a physical development server anymore, it is a virtual windows 2003 server, and secondly this is great for setting up specific testing environments. Need a windows XP running Internet Explorer 6? Create a new virtual image and install XP, it is far more reliable than multiple IE's or other Internet Explorer emulation software.

I use VM ware server 2.0, its totally free (Virtual pc's operating system licences aren't so be careful) you can get it here: http://www.vmware.com/products/server/

3. Instant messaging / Skype

Communication in any team is important, especially so if you are working remotely. If no-one can contact you then appearances may be that you aren't working at all, which is bad news. I use Skype http://www.skype.com/intl/en-gb/ which can be used for chat and messaging.

The obvious benefits are that it is free, and means you can have team discussions online. More recently I've encountered several companies that actually use Skype as their main communications tool. They don't even have desktop phones anymore.

4. FTP clients / File synchronisation utilities

Ideally you can still provide any of your in-office functionality at your remote location. So being able to implement change releases is essential. I use a combination of Filezilla http://filezilla-project.org/ which is a free FTP client, and Beyond Compare http://www.scootersoftware.com/ which is a file comparison tool. Ideally I'd like to learn ANT, but just haven't got round to it yet.

5. Online file storage

Services like DropBox https://www.dropbox.com/ are invaluable for the online storage of files. We always have online briefs, and Photoshop designs flying around the place, so storing them in one central online location is an ideal way of ensuring that you always have the latest version. It also avoids the problem of not having the project spec, and not being able to work.

I personally use Google Docs for this http://www.google.com/docs I've found it to be a very flexible easily maintained way of storing pretty much any format of document.

08
J
A
N
2010

Creating a CSS only cross browser drop down menu

I'll say this up front, I'm not a CSS guru. I like CSS, but it can be incredibly frustrating. One of the clients I am currently producing work for requires a high level of Accessibility. High enough that we need to avoid using JavaScript unless any script has a "Non" JavaScript equivalent.

The site navigation is through a top level horizontal menu, but what if we also want a drop down menu? Can we do it in CSS only, and it still be Firefox, Internet Explorer 6,7 and 8 compatible?

Yes, we can, but it's a lot of code, and it's not pretty. The basic concept behind most CSS menus is to use a list, and transform each list item to suit your styling. So we will create a div, with a class of menu. Then create a list inside it.

view plain print about
1<div class="menu">
2        <ul>
3        <li><a href="">Top menu option
4        <!--[if IE 7]><!--></a><!--<![endif]-->
5        <!--[if lte IE 6]><table><tr><td><![endif]-->
6    
7        <ul>
8            <li><a href="">Link 1</a></li>
9            <li><a href="">Link 2</a></li>
10            <li><a href="">Link 3</a></li>
11            <li><a href="">Link 4</a></li>
12            <li><a href="">Link 5</a></li>
13            <li><a href="">Link 6</a></li>
14        </ul>
15
16            <!--[if lte IE 6]></td></tr></table></a><![endif]-->
17        </li>
18        
19        </ul>
20    
21    </div>

Notice that there are some specific if statements relating to different versions of Internet Explorer. these basically make the functionality the same for each version, they are compensating for the differences in code handling between IE versions.

Next create a set of CSS styles to alter the appearance of the list. I am not going to go into the CSS line by line, as it is commented, but I'll explain the methodology behind it.

The top level menu item is always displayed, but the list is hidden using "visibility:hidden". When the user mouse's over the menu div the CSS applies a:hover ul{visibility:visible;. This makes the list visible.

view plain print about
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
3.menu {width:149px; height:32px; position:relative; z-index:100;border-right:1px solid #000; font-family:arial, sans-serif;}
4
5/* hack to correct IE5.5 faulty box model */
6* html .menu {width:149px; w\idth:149px;}
7
8/* remove all the bullets, borders and padding from the default list styling */
9.menu ul {padding:0;margin:0;list-style-type:none;}
10.menu ul ul {width:149px;}
11
12/* float the list to make it horizontal and a relative position so that you can control the dropdown menu positon */
13.menu li {float:left;width:149px;position:relative;}
14
15/* style the links for the top level */
16.menu a, .menu a:visited {display:block;font-size:12px;text-decoration:none; color:#fff; width:138px; height:30px; border:1px solid #000; border-width:1px 0 1px 1px; background:#c0c0c0; padding-left:10px; line-height:29px; font-weight:bold;}
17
18/* a hack so that IE5.5 faulty box model is corrected */
19* html .menu a, * html .menu a:visited {width:149px; w\idth:138px;}
20
21/* style the second level background */
22.menu ul ul a.drop, .menu ul ul a.drop:visited {background:#c0c0cc url(arrow.gif) no-repeat 130px center;}
23
24/* style the second level hover */
25.menu ul ul a.drop:hover{background:#c0c0cc url(arrow.gif) no-repeat 130px center;}
26.menu ul ul :hover >
a.drop {background:#c0c0cc url(arrow.gif) no-repeat 130px center;}
27
28/* hide the sub levels and give them a positon absolute so that they take up no room */
29.menu ul ul {visibility:hidden;position:absolute;height:0;top:31px;left:0; width:149px;border-top:1px solid #000;}
30
31/* another hack for IE5.5 */
32* html .menu ul ul {top:30px;t\op:31px;}
33
34/* style the table so that it takes no part in the layout - required for IE to work */
35.menu table {position:absolute; top:0; left:0; border-collapse:collapse;}
36
37/* style the second level links */
38.menu ul ul a, .menu ul ul a:visited {background: ghostwhite; color:#000; height:auto; line-height:1em; padding:5px 10px; width:128px;border-width:0 1px 1px 1px;}
39
40/* yet another hack for IE5.5 */
41* html .menu ul ul a, * html .menu ul ul a:visited {width:150px;w\idth:128px;}
42
43/* style the top level hover */
44.menu a:hover, .menu ul ul a:hover{color:#000; background:#c0c0cc;}
45.menu :hover > a, .menu ul ul :hover > a {color:#000; background:#c0c0cc;}
46
47/* make the second level visible when hover on first level list OR link */
48.menu ul li:hover ul,
49.menu ul a:hover ul{visibility:visible;}

There are also a lot of other browser specific hacks present, as the aim was to get the menu working in every Internet Explorer version, and be Accessible.

There is a demo of this here: CSS menu demo

08
J
A
N
2010

Windows 2003 Server does not stream F4V videos - adding mime types

With the introduction of Adobe's new .F4v flash wrapper you may find that your server does not respond well to files of the .f4v extension. Mine has been returning a 404 error, despite the file being in the right location.

This appears to be a Mime type error, rather than any coding error. Your server simply does not know what type of file the .f4v is, so we need to tell it.

  • On the Windows 2003 server, open the Internet Information Services Manager.
  • Expand the Local Computer Server.
  • Right-click the local computer server and select Properties.
  • Select the MIME Types tab.
  • Click New and enter the following information:

    view plain print about
    1Associated Extension box: .F4V
    2MIME Type box: video/mp4

  • Click OK

You must then restart the World Wide Web Publishing service, as changes do not take effect until the service is refreshed. At this point you should have working .f4v flash movies.

_UNKNOWNTRANSLATION_ /