PDA

View Full Version : MVC tutorial and Validation



nicklothian
Sep 7th, 2004, 07:38 PM
I'm trying to figure out how the spring validators are called.

In the MVC tutorial it shows how to wire up a Validator and a Controller (http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-3.html).

That seems to work fine, but I can't figure out how the validator gets called.

The relevent part of the config file is



<!-- Validator and Form Controller for the "Price Increase" page -->
<bean id="priceIncreaseValidator" class="bus.PriceIncreaseValidator"/>
<bean id="priceIncreaseForm" class="web.PriceIncreaseFormController">
<property name="sessionForm"><value>true</value></property>
<property name="commandName"><value>priceIncrease</value></property>
<property name="commandClass"><value>bus.PriceIncrease</value></property>
<property name="validator"><ref bean="priceIncreaseValidator"/></property>
<property name="formView"><value>priceincrease</value></property>
<property name="successView"><value>hello.htm</value></property>
<property name="productManager">
<ref bean="prodMan"/>
</property>
</bean>

Tracing through the Spring code, I think the validator gets called in the bindAndValidate method in BaseCommandController.



/**
* Bind the parameters of the given request to the given command object.
* @param request current HTTP request
* @param command the command to bind onto
* @return the ServletRequestDataBinder instance for additional custom validation
* @throws Exception in case of invalid state or arguments
*/
protected final ServletRequestDataBinder bindAndValidate&#40;HttpServletRequest request, Object command&#41;
throws Exception &#123;
ServletRequestDataBinder binder = createBinder&#40;request, command&#41;;
binder.bind&#40;request&#41;;
onBind&#40;request, command, binder.getErrors&#40;&#41;&#41;;
if &#40;this.validators != null && isValidateOnBinding&#40;&#41; && !suppressValidation&#40;request&#41;&#41; &#123;
for &#40;int i = 0; i < this.validators.length; i++&#41; &#123;
ValidationUtils.invokeValidator&#40;this.validators&#91;i&#93; , command, binder.getErrors&#40;&#41;&#41;;
&#125;
&#125;
onBindAndValidate&#40;request, command, binder.getErrors&#40;&#41;&#41;;
return binder;
&#125;

As far as I can see isValidateOnBinding() will always return false, unless the validateOnBinding property is set to true in the confiig.

What am I missing?

fmourioux
Sep 8th, 2004, 02:45 PM
Perhaps it is call by an interceptor before.

Fabien

nicklothian
Sep 8th, 2004, 07:23 PM
No - there are no interceptors configured. The complete config file is available in the tutorial (http://www.springframework.org/docs/MVC-step-by-step/Spring-MVC-step-by-step-Part-3.html)

irbouho
Sep 8th, 2004, 07:58 PM
from BaseCommandController source



private boolean validateOnBinding = true;


/**
* Set if the Validator should get applied when binding.
*/
public final void setValidateOnBinding&#40;boolean validateOnBinding&#41; &#123;
this.validateOnBinding = validateOnBinding;
&#125;

/**
* Return if the Validator should get applied when binding.
*/
protected final boolean isValidateOnBinding&#40;&#41; &#123;
return validateOnBinding;
&#125;


if validateOnBinding is not set to false, isValidateOnBinding() always returns true.

nicklothian
Sep 15th, 2004, 10:53 PM
I must have missed that.

Thanks