Announcement Announcement Module
Collapse
No announcement yet.
MVC tutorial and Validation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • MVC tutorial and Validation

    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.

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

    The relevent part of the config file is

    Code:
       
     <!--  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.

    Code:
    	/**
    	 * 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?

  • #2
    Perhaps it is call by an interceptor before.

    Fabien

    Comment


    • #3
      No - there are no interceptors configured. The complete config file is available in the tutorial

      Comment


      • #4
        from BaseCommandController source
        Code:
        	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.

        Comment


        • #5
          Doh!

          I must have missed that.

          Thanks

          Comment

          Working...
          X