<?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/"
	
	xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Rough Book &#187; annotations</title>
	<atom:link href="http://vivin.net/tag/annotations/feed/" rel="self" type="application/rss+xml" />
	<link>http://vivin.net</link>
	<description>random musings of just another computer nerd</description>
	<lastBuildDate>Wed, 02 May 2012 18:20:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Integrating Regula with Spring 3.0.x MVC</title>
		<link>http://vivin.net/2011/02/21/integrating-regula-with-spring-3-0-x-mvc/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2011/02/21/integrating-regula-with-spring-3-0-x-mvc/#comments</comments>
		<pubDate>Tue, 22 Feb 2011 01:50:20 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[annotation]]></category>
		<category><![CDATA[annotation-based form-validation]]></category>
		<category><![CDATA[annotation-based validation]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[bean-validation]]></category>
		<category><![CDATA[form-validation]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript form-validation]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[regula]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[spring mvc]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1680</guid>
		<description><![CDATA[A little less than a year ago, I released Regula, an annotation-based form-validation written in Javascript. The source and documentation are available on GitHub. I started working on the integration on and off throughout most of last year. At the end of the year, I had a pretty good integration going, where you could annotate [...]]]></description>
			<content:encoded><![CDATA[<p>A little less than a year ago, I <a href="http://vivin.net/2011/03/30/regula-an-annotation-based-form-validator-written-in-javascript/">released</a> Regula, an annotation-based form-validation written in Javascript. The <a href="https://github.com/vivin/regula">source</a> and <a href="https://github.com/vivin/regula/wiki">documentation</a> are available on GitHub. I started working on the integration on and off throughout most of last year. At the end of the year, I had a pretty good integration going, where you could annotate fields with Hibernate Validator annotations, and the corresponding Regula validation-code would be generated on the client side. Of course, I wasn&#8217;t done yet because what I had was simply a demo project and I had to figure out a good way to distribute the whole thing; I was able to finish up the packaging and distribution today. With minimal setup, you should be able to get started with Regula and Spring. You don&#8217;t need to go through this post to figure out how to use the integration. This post is mostly about <i>how</i> I accomplished the integration (I don&#8217;t go into all the details; just the important bits). As far as actually using it, I will make a blog post about it later.</p>
<p>The <a href="https://github.com/vivin/regula-spring">source</a> for the integration is also hosted on GitHub. My approach towards translating validation constraints from the server-side to the client-side was two-fold: gather validation constraints from the object and represent it in a canonical form. Using the canonical form, generate Javascript code that uses Regula for validation. To do this, I created a service that examines a domain object and gathers all information regarding its properties and validation constraints. The service returns this information in a canonical form, that I then inserted into the model. On the client-side, I had a tag that used the canonical form and outputted Javascript that uses the Regula framework. Initially, I was calling the service explicitly from an action in the controller. Later, in an effort to make the integration less-invasive and more seamless, I used an aspect-oriented approach with interceptors. In fact, that&#8217;s where I&#8217;d like to start.<br />
<span id="more-1680"></span><br />
I created an annotation called <span style="font-family:courier">@ValidateClientSide</span> that marks a domain object as requiring client-side validation. Then, I created an interceptor called <span style="font-family:courier">ClientSideValidationInterceptor</span> that will call the validation service and gather validation information:</p>
<br /><a href="http://vivin.net/?p=1680#comments" title="Comments on &quot;Integrating Regula with Spring 3.0.x MVC&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1680" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1680&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2011/02/21/integrating-regula-with-spring-3-0-x-mvc/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1680" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1680" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1680&#38;type=feed" medium="image" />
	</item>
		<item>
		<title>Regula: An annotation-based form-validator written in Javascript</title>
		<link>http://vivin.net/2010/03/30/regula-an-annotation-based-form-validator-written-in-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rss</link>
		<comments>http://vivin.net/2010/03/30/regula-an-annotation-based-form-validator-written-in-javascript/#comments</comments>
		<pubDate>Tue, 30 Mar 2010 15:16:27 +0000</pubDate>
		<dc:creator>vivin</dc:creator>
				<category><![CDATA[Programming and Development]]></category>
		<category><![CDATA[Projects]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[annotation]]></category>
		<category><![CDATA[annotation-based form-validation]]></category>
		<category><![CDATA[annotation-based validation]]></category>
		<category><![CDATA[annotations]]></category>
		<category><![CDATA[bean-validation]]></category>
		<category><![CDATA[form-validation]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[hibernate]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[javascript form-validation]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[regula]]></category>

		<guid isPermaLink="false">http://vivin.net/?p=1477</guid>
		<description><![CDATA[Regula is an annotation-based form-validation framework written in Javascript. There already exist a few frameworks that address form-validation in Javascript, but I have found them to be somewhat lacking. I have thought about writing one of my own for some time, but I honestly had no idea what form it would or should take. I [...]]]></description>
			<content:encoded><![CDATA[<p><strong><em>Regula</em></strong> is an annotation-based form-validation framework written in Javascript. There already exist a few frameworks that address form-validation in Javascript, but I have found them to be somewhat lacking. I have thought about writing one of my own for some time, but I honestly had no idea what form it would or should take. I knew that I wanted to make one that was easy to use, flexible, and easily extensible (custom validation rules). I finally got an idea as to the form my framework should take, when I was looking at <a href="http://www.hibernate.org/">Hibernate</a> <a href="http://docs.jboss.org/hibernate/stable/validator/reference/en/html_single/">bean-validation</a>. I like the fact that you can set constraints by using annotations like <em>@NotNull</em> or  <em>@NotEmpty</em>. That way, when you look at the bean, you are immediately aware of the constraints attached to it. I wanted to do something similar in HTML.<br />
<span id="more-1477"></span><br />
Enter HTML5. HTML5 supports and endorses <a href="http://dev.w3.org/html5/spec/dom.html#embedding-custom-non-visible-data">custom attributes</a> in HTML tags. Purists may scream &#8220;No!&#8221;, but I think it is a useful feature. However, like all features it has the potential for abuse. But that&#8217;s another topic entirely. Anyway, I started thinking &#8211; what if I could use a custom attribute to specify the constraints? Then I could use Javascript to identify those constraints and then enforce them during validation. What I was thinking of, was to do something like this:</p>
<pre class="brush: html">
&lt;input id = &quot;myInput&quot;
         type = &quot;text&quot;
         data-constraints = &#039;@NotEmpty @IsNumeric @Between(min=1, max=5)&#039;&gt;
</pre>
<p>That bit of code describes a text box which cannot be empty, and which expects a numeric value between 1 and 5. With this basic design in mind, I started working on my framework. It took me about a week of on-and-off work (maybe 3 days actual work) but I&#8217;ve come up with something that is, in my humble opinion, a flexible and easy to use framework. I plan to document the use of this library/framework more thoroughly, but this post should serve as a gentle introduction to the framework and its features.</p>
<p>In <em><strong>Regula</strong></em> (which, by the way, means &#8220;rule&#8221; in Latin) form elements (input elements or even the form elements) can have constraints attached to them in a similar fashion to the example I described earlier. There is a small difference, however:</p>
<pre class="brush: html">
&lt;input id = &quot;myInput&quot;
         class = &quot;regula-validation&quot;
         type = &quot;text&quot;
         data-constraints = &#039;@NotEmpty @IsNumeric @Between(min=1, max=5)&#039;&gt;
</pre>
<p>As you can see, I use a class name of <em>regula-validation</em> which tells the framework that this input element has constraints attached to it. The reason I did this was for efficiency reasons. It&#8217;s much more efficient to grab all elements that have a class name of <em>regula-validation</em> than walking the whole document tree to search for nodes that have a <em>data-constraints</em> attribute.</p>
<p>After you annotate the input elements with their constraints, you&#8217;re halfway there already! All you have to do after that is add a script tag for the framework:</p>
<pre class="brush: html">
  &lt;script type = &quot;text/javascript&quot; src=&quot;regula.js&quot;&gt;&lt;/script&gt;
</pre>
<p>And then add the necessary Javascript to validate the form (example uses jQuery):</p>
<pre class="brush: javascript">
jQuery(document).ready(function() {
    // must call this first. The best place would be in an
    // onload handler. This function looks for elements with
    // a class name of &quot;regula-validation&quot; and binds the
    // appropriate constraints to the elements
    regula.bind(); 

    jQuery(&quot;#myForm&quot;).submit(function() {
        // this functions performs the actual validation
        var validationResults = regula.validate();

        for(var index in validationResults) {
             var validationResult = validationResults[index];
             alert(validationResult.message);
        }
    });
});
</pre>
<p>That&#8217;s pretty much all there is to it. More advanced uses of the framework don&#8217;t deviate that much from this pattern. Compared to other form-validation frameworks (that I&#8217;ve seen), validation will not stop on the first failing element. The <em>validate</em> function runs through every input element and validates it against the constraints bound to that element, and returns an array of &#8220;validation results&#8221;. Each &#8220;validation result&#8221; element in the array has the following properties:</p>
<ul>
<li><em><strong>constraintName</strong></em> &#8211; the name of the failing constraint</li>
<li><em><strong>custom</strong></em> &#8211; a flag that says whether this is a custom constraint or not</li>
<li><em><strong>constraintParameters</strong></em> &#8211; An array of objects that represents the parameters passed to this constraint (defined in the HTML. For example <em>@Max(max=5)</em>, where you have a parameter who&#8217;s name is <em>max</em> and who&#8217;s value is <em>5</em>. There&#8217;s a little more to this, but like I said, this is supposed to be a gentle introduction <img src='http://vivin.net/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> )</li>
<li><em><strong>receivedParameters</strong></em> &#8211; A hash (organized such that the name of the parameter is the key, and the value is well, the value) of parameters that the validator function received (helpful when you have a custom validator).
<li><em><strong>failingElements</strong></em> &#8211; An array containing references to the actual input element or elements (in the case of form-specific constraints) that failed the constraint.</li>
<li><em><strong>message</strong></em> &#8211; The error message.</li>
</ul>
<br /><a href="http://vivin.net/?p=1477#comments" title="Comments on &quot;Regula: An annotation-based form-validator written in Javascript&quot;"><img src="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1477" alt="Comments" /></a><img src="http://vivin.net/?ak_action=api_record_view&#38;id=1477&#38;type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://vivin.net/2010/03/30/regula-an-annotation-based-form-validator-written-in-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
	
		<media:thumbnail url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1477" />
		<media:content url="http://vivin.net/wp-content/plugins/feed-comments-number/image.php?1477" medium="image">
			<media:title type="html">Comments</media:title>
		</media:content>
		<media:content url="http://vivin.net/?ak_action=api_record_view&#38;id=1477&#38;type=feed" medium="image" />
	</item>
	</channel>
</rss>

