View Full Version : Declaritive Validation
ctaggart
Aug 13th, 2004, 07:57 PM
I was hoping that Spring v1.1 would have declaritive validation support as Keith Donald outlined in his blog back in April.
http://opensource.atlassian.com/confluence/spring/display/DOC/Declarative+Validation
According to the JIRA roadmap it is scheduled for v1.2.
http://opensource.atlassian.com/projects/spring/secure/BrowseProject.jspa?id=10000&report=roadmap
I would like to be able to declare validation rules somehow that match what and XML Schema defines. This included number ranges, matching against a list of values(enumeration), and a few others. I'm writing xsd2jibx (www.jibx.org) for Java/XML data binding. That it what I need to find some sort of validation form. Any suggestions would be appreciated.
Any status on the declaritive validation support?
Thanks,
Cameron
dmiller
Aug 13th, 2004, 09:37 PM
You can always try the Commons-Validator adapter for Spring. It's in the sandbox. If you have any problems getting it working, feel free to ask here on the forum. I will do my best to help out.
irbouho
Aug 13th, 2004, 09:47 PM
Matt Raible (http://www.jroller.com/raible) describes, in this web log entry (http://www.jroller.com/comments/raible/home/using_commons_validator_with_spring), how to use Commons Validator with Spring Framework.
Keith Donald
Aug 15th, 2004, 11:16 AM
The code in the sandbox, specifically, the org.springframework.rules package and related sub-packages contain the work that's been done on the declarative validation module to-date. We're using this code in rich client now to drive as-you-type client-side validation, and so far it's been footing the bill quite nicely.
As far as status:
- As the total size of the packages is over 100 KB, and it's plausible that as new out-of-the-box rules are provided it will grow further, we've decided to create a "spring-rules" sub-module within the /cvsroot/springframework repository. So we'll be effectively turning this effort into its own mini-subproject with its own release schedule. This will happen by the first rich client release date, and by Spring 1.2. I'll be posting announcements then, and to mark the module as fit for general use.
Keith
ctaggart
Aug 26th, 2004, 06:19 PM
I checked out Spring from CVS and I've been looking through the org.springframework.rules packages, but I'm not sure where to begin. How could I validate a Person bean like so:
public class Person{
public String name;
public float feetTall;
}
Let's say I wanted to make sure that ther person's name was set and feetTall was between 1.0 and 8.0. How would I do that simple validation?
bpolka
Sep 1st, 2004, 03:19 PM
check out the tests in sandbox, they might clue you in on how to start using it.
bpolka
Sep 2nd, 2004, 11:54 AM
Let's say I wanted to make sure that ther person's name was set and feetTall was between 1.0 and 8.0. How would I do that simple validation?
Let's see, here is what's currently in rules-context.xml of the tests that I was referring to in my previous post:
<beans>
<!-- The xml here is *not* the best format to define rules...it's too fine grained and as a result
way to verbose!!! -->
<!-- direct java / groovy / beanshell / jython or some other scripting solution is better
Using the Constraints factory and builders is preferred -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundle MessageSource">
<property name="basenames">
<list>
<value>org.springframework.rules.messages</value>
</list>
</property>
</bean>
<bean id="rulesSource" class="org.springframework.rules.DefaultRulesSource">
<property name="rules">
<list>
<bean class="org.springframework.rules.Rules">
<property name="beanClass">
<value>org.springframework.rules.Person</value>
</property>
<property name="propertyRules">
<map>
<entry key="firstName">
<ref bean="required"/>
</entry>
<entry key="lastName">
<list>
<ref bean="required"/>
<bean class="org.springframework.rules.predicates.StringLengthC onstraint">
<constructor-arg index="0">
<value>10</value>
</constructor-arg>
</bean>
<bean class="org.springframework.rules.predicates.UnaryNot">
<constructor-arg index="0">
<bean class="org.springframework.rules.predicates.Parameterized BinaryPredicate">
<constructor-arg index="0">
<ref bean="equals"/>
</constructor-arg>
<constructor-arg index="1">
<value>Keith</value>
</constructor-arg>
</bean>
</constructor-arg>
</bean>
</list>
</entry>
</map>
</property>
</bean>
</list>
</property>
</bean>
<bean id="equals" class="org.springframework.rules.predicates.EqualTo"/>
<bean id="required" class="org.springframework.rules.predicates.Required"/>
</beans>
My guess you would need to change this to something like:
<beans>
<!-- The xml here is *not* the best format to define rules...it's too fine grained and as a result
way to verbose!!! -->
<!-- direct java / groovy / beanshell / jython or some other scripting solution is better
Using the Constraints factory and builders is preferred -->
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundle MessageSource">
<property name="basenames">
<list>
<value>org.springframework.rules.messages</value>
</list>
</property>
</bean>
<bean id="rulesSource" class="org.springframework.rules.DefaultRulesSource">
<property name="rules">
<list>
<bean class="org.springframework.rules.Rules">
<property name="beanClass">
<value>org.springframework.rules.Person</value>
</property>
<property name="propertyRules">
<map>
<entry key="firstName">
<ref bean="required"/>
</entry>
<entry key="feetTall">
<list>
<ref bean="required"/>
<bean class="org.springframework.rules.predicates.Range">
<constructor-arg index="0">
<value>1.0</value>
</constructor-arg>
<constructor-arg index="1">
<value>8.0</value>
</constructor-arg>
</bean>
</list>
</entry>
</map>
</property>
</bean>
</list>
</property>
</bean>
<bean id="required" class="org.springframework.rules.predicates.Required"/>
</beans>
And this is just my guess as I have not tried it, but it seems to make sense :)
HTH
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.