Results 1 to 5 of 5

Thread: MVC tutorial and Validation

  1. #1

    Default 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. #2

    Default

    Perhaps it is call by an interceptor before.

    Fabien

  3. #3

    Default

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

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    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.
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

  5. #5

    Default Doh!

    I must have missed that.

    Thanks

Similar Threads

  1. How to code jsp to use validation
    By too_many_details in forum Web
    Replies: 4
    Last Post: Apr 19th, 2005, 05:00 AM
  2. Commons Validator integration
    By sjivan in forum Web
    Replies: 3
    Last Post: Oct 14th, 2004, 04:22 PM
  3. Validation Tutorial
    By G-Nome in forum Web
    Replies: 1
    Last Post: Aug 20th, 2004, 04:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •