Announcement Announcement Module
Collapse
No announcement yet.
AbstractWizardFormController and declarative validation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment

    Working...
    X