Results 1 to 2 of 2

Thread: AbstractWizardFormController and declarative validation

  1. #1
    Join Date
    Oct 2004
    Posts
    3

    Default AbstractWizardFormController and declarative validation

    I'm just messing with validation support in sandbox.
    I couldn't find support for declarative validation for AbstractWizardFormController.

    To solve the problem I just did some changes in sandbox source code and added a method in class org.springframework.validation.commons.ValidatorAd aptor:

    public void validate(String beanName, Object obj, Errors errors, int page) {
    Validator validator = factory.getValidator(beanName, obj, errors);
    try {
    validator.setPage(page);
    validator.validate();
    } catch (ValidatorException e) {
    log.error("An exception was thrown while validating bean: " + beanName, e);
    }
    }


    and a method in class org.springframework.validation.commons.BeanValidat or:

    public void validate(Object obj, Errors errors, int page) {
    validator.validate(getBeanName(obj.getClass()), obj, errors, page);
    }

    Everythig is working fine writing

    protected void validatePage(Object obj, Errors errors, int page) {
    User user = (User) obj;
    BeanValidator validator = (BeanValidator)getValidator();
    validator.validate(obj, errors, page);
    }

    in my implementations of AbstractWizardFormController.
    Later I will try to read source code more accurately to try to understand if this way everything is thread-safe.

    But, well, just would like to ask if I missed something and this work is useless because there's another way to do it.
    Thx
    Francesco

  2. #2
    Join Date
    Aug 2004
    Location
    Mount Joy, PA
    Posts
    34

    Default

    Francesco,

    What you've done looks good to me. You may also want to add the following methods to BeanValidator and NamedBeanValidator as they are classes usually used by client code. BeanValidator adds automatic form-name resolution based on the object type. NamedBeanValidator allows declarative specification of the bean name (aka form name) in the application context.

    BeanValidator
    Code:
    public void validate(Object obj, Errors errors, int page) {
      validator.validate(getBeanName(obj.getClass()), obj, errors, page);
    }
    NamedBeanValidator
    Code:
    public void validate(Object obj, Errors errors, int page) {
      if (beanName != null) {
        validator.validate(beanName, obj, errors, page);
      } else {
        log.error("Cannot validate bean (" + obj.getClass()
            + "). NamedBeanValidator bean name not set. "
            + "Set the bean name in the validator configuration or "
            + "explicitly call validate(beanName, obj, errors).");
      }
    }
    
    public void validate(String beanName, Object obj, Errors errors, int page) {
      validator.validate(beanName, obj, errors, page);
    }
    Submit a patch to the JIRA if you want to contribute your code for others to use. It's good to see someone else inovating with the Commons Validator support.
    ~ Daniel Miller

Similar Threads

  1. declarative validation rules in Spring
    By tentacle in forum Container
    Replies: 10
    Last Post: Jan 31st, 2006, 07:36 AM
  2. New Spring validation framework
    By garyfisher in forum Architecture
    Replies: 1
    Last Post: Aug 10th, 2005, 06:25 PM
  3. Replies: 2
    Last Post: Mar 2nd, 2005, 11:15 AM
  4. Domain Object Validation
    By lhilden in forum Architecture
    Replies: 4
    Last Post: Dec 14th, 2004, 07:00 AM
  5. Replies: 10
    Last Post: Nov 2nd, 2004, 09:38 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
  •