PDA

View Full Version : Validation usage doubts



yvk
Oct 26th, 2006, 03:12 AM
Hi All,

Iam using built-in Validator(org.apringframework.validation.Validator ) interface in springs framework for server side validation.

For that I used to define ...

<bean id="editempdetailsController" class="emp.EditempdetailsController">
<property name="commandName"><value>editempdetailsform</value></property>
<property name="commandClass"><value>emp.EditempdetailsForm</value></property>
<property name="formView"><value>editempdetails</value></property>

<property name="validator"><ref bean="editempdetailsValidator"/></property>
<property name="dao" ref="empDAO"/>
</bean>

in my app-servlet.xml...

It worked properly...

But now I want to use client side validation combined with the Server side validation defined above.

I though using spring modules integration with apache commons-validator for client side validation.

I read somewhere that we have to define in app-servlet.xml

<property name="validator" ref="beanValidator"/>

Then my app-servlet.xml looks like...

<bean id="editempdetailsController" class="emp.EditempdetailsController">
<property name="commandName"><value>editempdetailsform</value></property>
<property name="commandClass"><value>emp.EditempdetailsForm</value></property>
<property name="formView"><value>editempdetails</value></property>

<property name="validator"><ref bean="editempdetailsValidator"/></property>
<property name="validator" ref="beanValidator"/>
<property name="dao" ref="empDAO"/>
</bean>


can I mix both spring built-in validator support with spring modules integration with apache commons-validator for client side validation?

Which one takes place first?

Thanks,
yvk

Sam Brannen
Oct 27th, 2006, 05:49 AM
Hi yvk,


can I mix both spring built-in validator support with spring modules integration with apache commons-validator for client side validation?

You can certainly use a custom Validator combined with the Commons Validator. To configure your controller to use both, you will need to use the validators property of your SimpleFormController (or subclass of BaseCommandController) instead of the validator property you were previously using.

FYI: in your above example configuration, you attempted to set the validator property of your controller twice. The second one overwrites the value set in the first call. This is analagous to sequential method calls in Java code.


Which one takes place first?

The validators will be executed sequentially in the order in which they are defined in the validators list/array. See bindAndValidate(HttpServletRequest request, Object command) from BaseFormController for details.

One more important thing to note is that your custom Validator will have no effect for client-side validation. Your custom Validator will naturally only be executed on the server.

If you have more questions regarding proper usage of the Commons Validator, I recommed you check out the examples.

Hope this helps,

Sam