Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
Complex Validation and Commons Validator Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Complex Validation and Commons Validator

    Hi there,


    I'm trying to do some complex validation in combination with the Commons Validator i.e. some fields of my form can be validated with the Commons Validator that I inserted into my Controller/FormAction but other fields in the form should be validated in a more complex way (like for instance: I need to do some db-queries and some calculations). It's not clear to me how to do that. Maybe I have to implement org.springframework.validation.Validator (let's call it customVal) and inject my beanValidator (my configured org.springmodules.validation.commons.DefaultBeanVa lidator) into that. But somehow within my customVal I can't seem to tell my beanValidator to perform its validation. What am I missing here?

    TIA

  • #2
    "Multiple Validators per Controller" and CompositeValidator

    Hi flume,

    This is probably exactly what you're looking for: http://opensource.atlassian.com/conf...per+Controller

    If you'd like a generic validator that is aimed at combining two validators (i.e., one for validating formatting and one for validating business logic), you might consider using my CompositeValidator (see below).

    Note, however, that many controllers allow you to inject multiple Validators as is pointed out in the above tutorial. Thus, you would only want to use a CompositeValidator in cases where your code can handle only a single Validator.

    Hope this helps!

    Sam

    p.s. here's the code:

    Code:
    /**
     * Copyright (c) 2006-2007, Brannen Software Consulting.
     */
    package com.xyz.spring.validation;
    
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.validation.Errors;
    import org.springframework.validation.Validator;
    
    /**
     * CompositeValidator is a composite validator for an object which first allows
     * a <code>primaryValidator</code> to validate the object. Then a
     * <code>secondaryValidator</code> will be allowed to validate the object. The
     * intended usage scenario is that the primary validator will validate the
     * fields of the object based on data formatting rules, and the secondary
     * validator will validate the fields with regard to specific business rules
     * that can not be checked with simple formatting validation rules. A
     * CompositeValidator can, of course, be used in other ways.
     *
     * @author <a href="mailto:[email protected]" title="Sam Brannen">Sam Brannen</a>
     * @version $Revision: 1.4 $
     */
    public class CompositeValidator implements Validator {
    
        // ------------------------------------------------------------------------|
        // --- CONSTANTS ----------------------------------------------------------|
        // ------------------------------------------------------------------------|
    
        /** Class Logger. */
        private static final Log   LOG                = LogFactory.getLog(CompositeValidator.class);
    
    
        // ------------------------------------------------------------------------|
        // --- CLASS VARIABLES ----------------------------------------------------|
        // ------------------------------------------------------------------------|
    
        // ------------------------------------------------------------------------|
        // --- INSTANCE VARIABLES -------------------------------------------------|
        // ------------------------------------------------------------------------|
    
        private Validator          primaryValidator;
    
        private Validator          secondaryValidator;
    
        // ------------------------------------------------------------------------|
        // --- INSTANCE METHODS ---------------------------------------------------|
        // ------------------------------------------------------------------------|
    
        /**
         * @return Returns the primaryValidator.
         */
        public final Validator getPrimaryValidator() {
    
            return this.primaryValidator;
        }
    
        // ------------------------------------------------------------------------|
    
        /**
         * @return Returns the secondaryValidator.
         */
        public final Validator getSecondaryValidator() {
    
            return this.secondaryValidator;
        }
    
        // ------------------------------------------------------------------------|
    
        /**
         * Convenience method for requiring both validators.
         *
         * @throws IllegalStateException
         *             If either the the <tt>primaryValidator</tt> or the
         *             <tt>secondaryValidator</tt> has not been set.
         */
        protected final void requireBothValidators() throws IllegalStateException {
    
            if (getPrimaryValidator() == null)
                throw new IllegalStateException("The primaryValidator must be set!");
    
            if (getSecondaryValidator() == null)
                throw new IllegalStateException("The secondaryValidator must be set!");
        }
    
        // ------------------------------------------------------------------------|
    
        /**
         * @param primaryValidator
         *            The primaryValidator to set.
         */
        public final void setPrimaryValidator(final Validator primaryValidator) {
    
            this.primaryValidator = primaryValidator;
        }
    
        // ------------------------------------------------------------------------|
    
        /**
         * @param secondaryValidator
         *            The secondaryValidator to set.
         */
        public final void setSecondaryValidator(final Validator secondaryValidator) {
    
            this.secondaryValidator = secondaryValidator;
        }
    
        // ------------------------------------------------------------------------|
    
        /**
         * Implements supports(). Queries both the primary and secondary validators
         * to verify that they support the supplied class. Requires that both the
         * <tt>primaryValidator</tt> and the <tt>secondaryValidator</tt> have
         * been set.
         *
         * @see org.springframework.validation.Validator#supports(java.lang.Class)
         *
         * @throws IllegalStateException
         *             If either the the <tt>primaryValidator</tt> or the
         *             <tt>secondaryValidator</tt> has not been set.
         */
        public final boolean supports(final Class clazz) throws IllegalStateException {
    
            requireBothValidators();
    
            return getPrimaryValidator().supports(clazz) && getSecondaryValidator().supports(clazz);
        }
    
        // ------------------------------------------------------------------------|
    
        /**
         * Implements validate(). Allows the primary validator to validate the
         * object first and then the secondary validator. Requires that both the
         * <tt>primaryValidator</tt> and the <tt>secondaryValidator</tt> have
         * been set.
         *
         * @see org.springframework.validation.Validator#validate(java.lang.Object,
         *      org.springframework.validation.Errors)
         */
        public void validate(final Object obj, final Errors errors) {
    
            requireBothValidators();
    
            if (LOG.isDebugEnabled()) {
                LOG.debug("Allowing the primary validator to validate first...");
            }
    
            // Invoke the primary validator.
            getPrimaryValidator().validate(obj, errors);
    
            if (LOG.isDebugEnabled()) {
                LOG.debug("Allowing the secondary validator to validate now...");
            }
    
            // Invoke the secondary validator.
            getSecondaryValidator().validate(obj, errors);
    
        }
    
        // ------------------------------------------------------------------------|
    
    } // class CompositeValidator

    Comment

    Working...
    X