<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>SpinOneSolutions &#187; Cold Fusion</title>
	<atom:link href="http://www.spinonesolutions.com/tag/cold-fusion/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.spinonesolutions.com</link>
	<description>I waste my time so that you don't have to...</description>
	<lastBuildDate>Fri, 16 Apr 2010 21:39:11 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>CommonSpot Custom Field Validation</title>
		<link>http://www.spinonesolutions.com/2009/01/commonspot-custom-field-validation/</link>
		<comments>http://www.spinonesolutions.com/2009/01/commonspot-custom-field-validation/#comments</comments>
		<pubDate>Tue, 13 Jan 2009 22:40:34 +0000</pubDate>
		<dc:creator>Will Wright</dc:creator>
				<category><![CDATA[Cold Fusion]]></category>
		<category><![CDATA[CommonSpot]]></category>

		<guid isPermaLink="false">http://www.spinonesolutions.com/?p=93</guid>
		<description><![CDATA[The Problem You&#8217;re making a form and the out-of-the-box field types just aren&#8217;t cutting it.  Most web developers are familiar with this scenario.  The biggest culprits are phone numbers, addresses, and credit cards. Let&#8217;s look at how you can quickly inject you&#8217;re own JavaScript validation into a CommonSpot form to handle any kind of validation [...]]]></description>
			<content:encoded><![CDATA[<h3>The Problem</h3>
<p>You&#8217;re making a form and the out-of-the-box field types just aren&#8217;t cutting it.  Most web developers are familiar with this scenario.  The biggest culprits are phone numbers, addresses, and credit cards.</p>
<p>Let&#8217;s look at how you can quickly inject you&#8217;re own JavaScript validation into a CommonSpot form to handle any kind of validation that you want.</p>
<h3>The Solution</h3>
<p>We&#8217;ll make use of custom field types in order to accomplish our goal.  First, we&#8217;ll make a custom render handler for our custom field type.  You may be thinking we should jump straight into the Site Admin, but that dialog is going to ask us to chose a custom render handler from a drop down list, so first we need to make the render handler so there&#8217;s something to choose from!</p>
<p>Fire up the FTP program of your choice, and navigate to the &#8220;customfields&#8221; folder in root.  I&#8217;ll be making a US Zip Code validator for this example so I&#8217;ll call my file &#8220;zip_render.cfm&#8221;.  If you want to register custom properties to your custom field type go ahead and create a &#8220;zip_props.cfm&#8221; in this folder now too.</p>
<p>Now that there&#8217;s something to choose; open Site Admin -&gt; Field Masks &amp; Custom Field Types -&gt; Register New Field Type.  You should see something like this:</p>
<p><img class="aligncenter size-medium wp-image-94" title="Add Custom Field Type Dialog" src="http://www.spinonesolutions.com/wp-content/uploads/2009/01/customfieldtype-483x600.jpg" alt="Add Custom Field Type Dialog" width="483" height="600" /></p>
<p>Type: The type of the field you&#8217;re creating.  It&#8217;s also what is displayed when choseing custom fields from the list.  I&#8217;ll call mine &#8220;Zip&#8221;.</p>
<p>Description: A human friendly description.  I&#8217;ll use &#8220;A valid US Zip code&#8221; for now. It can be a good idea to put what will and won&#8217;t match in here if you have the energy.</p>
<p>Property Module: Choose &#8220;zip_props.cfm&#8221; (whatever you called your file) if you made a Property Module or choose from the list of Standard Property Modules.  &#8220;Text&#8221; will work fine for me.</p>
<p>Render Module: Choose &#8220;zip_render.cfm&#8221; (whatever you called your file) from the list of Custom Render Modules.</p>
<p>Hit OK!</p>
<p>Now when you go to create/edit your form you&#8217;ll have a new field type available:</p>
<p><img class="aligncenter size-full wp-image-95" title="Edit Form Field - New custom field selected" src="http://www.spinonesolutions.com/wp-content/uploads/2009/01/editformfield.jpg" alt="Edit Form Field - New custom field selected" width="540" height="559" /></p>
<p>Now all we have to do is add the custom validation. Here&#8217;s my custom render handler (zip_render.cfm):</p>
<pre name="code" class="html">&lt;cfscript&gt;
	// the fields current value
	currentValue = attributes.currentValues[fqFieldName];
	// the param structure which will hold all of the fields from the props dialog
	xparams = parameters[fieldQuery.inputID];

	request.youproject.utils.loadJQueryHeaders();
&lt;/cfscript&gt;

&lt;cfoutput&gt;
	&lt;script Language="JavaScript"  type="text/javascript"&gt;
		// javascript validation to make sure they have text to be converted
		#fqFieldName#=new Object();
		#fqFieldName#.id='#fqFieldName#';
		#fqFieldName#.tid=#rendertabindex#;
		#fqFieldName#.msg="Please enter a valid zip code.";
		#fqFieldName#.validator="#fqFieldName#_validater()";
		// push on to validation array
		&lt;cfif xparams["REQ"]&gt;
			vobjects_#attributes.formname#.push(#fqFieldName#);
			$(document).ready(function(){
				$("###fqFieldName#_label").wrap("<strong></strong>");
			});
		&lt;/cfif&gt;

		function #fqFieldName#_validater() {
			regex = new RegExp("(^\\d{5}$)|(^\d{5}-\\d{4}$)");
			return regex.test($("###fqFieldName#").val());
		}
	&lt;/script&gt;
	&lt;tr&gt;
		&lt;td nowrap="nowrap" align="left" width="25%" valign="baseline"&gt;
			&lt;font face="Verdana,Arial" color="##000000" size="2">
                        &lt;label for="#fqFieldName#">#xparams["LABEL"]#:&lt;/label>
                &lt;/font>
		&lt;/td&gt;
		&lt;td align="left" width="75%" valign="baseline"&gt;
			&lt;input type="text" id="#fqFieldName#" name="#fqFieldName#" value="#currentValue#"&gt;
		&lt;/td&gt;
	&lt;/tr&gt;
&lt;/cfoutput&gt;</pre>
<p>Not too complicated but, let&#8217;s go over it real quick so you can expand and adapt this simple case to suit your needs.</p>
<p>&#8220;fqFieldName&#8221; is holding the CommonSpot generated name of your form field.  If you don&#8217;t include an input field for it, it <strong>won&#8217;t</strong> get saved when the user hits submit.</p>
<p>The JavaScript object of the same name (fqFieldName) is holding the specifics for our valdiation.  There&#8217;s the message to display on fail (msg) and the function to call on validation (validator).</p>
<p>I&#8217;ve wrapped the last bit in a conditional so that my custom validation is only added to the form&#8217;s validation array if I mark the field as required in the element.</p>
<p>The HTML for the display for the form field is left to you, the developer.</p>
<p>As always, Questions and Comments are welcome!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.spinonesolutions.com/2009/01/commonspot-custom-field-validation/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
