Tuesday, April 14, 2009

Spring.NET Short Circuiting Validators - Part 2

Yesterday I gave a possible solution to the problem of easily configurable short circuiting validators in Spring.NET. Several hours later another possiblity struck me. One can use the existing ValidatorGroup class with the <v:validator> tag and set FastValidate in the <v:property> tag instead using the <v:group> tag which doesn't let you set that property. The configuration would look like this


    1 <objects xmlns="http://www.springframework.net"

    2         xmlns:v="http://www.springframework.net/validation">

    3   <v:group id="UserValidator">

    4     <v:validator id="UserNameValidator" type="Spring.Validation.ValidatorGroup, Spring.Core">

    5       <v:property name="FastValidate" value="true"/>

    6       <v:property name="Validators">

    7         <list>

    8           <ref object="UserNameRequired"/>

    9           <ref object="UserNameMinimumComplexity"/>

   10           <ref object="UserNameIsUnique"/>

   11         </list>

   12       </v:property>

   13     </v:validator>

   14 

   15     <!-- ...more validators -->

   16   </v:group>

   17 

   18   <v:required id="UserNameRequired" test="UserName">

   19     <v:message id="Errors.UserName.Required"  providers="UserNameError"/>

   20   </v:required>

   21 

   22   <v:regex id="UserNameMinimumComplexity" test="UserName">

   23     <v:property name="Expression" value="^.{5,20}$"/>

   24     <v:message id="Errors.UserName.MinimumComplexity" providers="UserNameError"/>

   25   </v:regex>

   26 

   27   <v:validator id="UserNameIsUnique" test="UserName" type="XXX.Infrastructure.Validation.IsUniqueUserNameValidator, HSN.Infrastructure">

   28     <v:property name="UserRepository" ref="UserRepository"/>

   29     <v:message id="Errors.UserName.IsUnique" providers="UserNameError"/>

   30   </v:validator>

   31 

   32   <!-- ...more validators -->

   33 </objects>

No comments: