Package org.springframework.validation

Examples of org.springframework.validation.Validator


     * @param clazz The class to be validated.
     * @return Whether this validator supports the given class.
     */
    public boolean supports(Class clazz) {
        for (Iterator i = validators.iterator(); i.hasNext();) {
            Validator validator = (Validator)i.next();
            if (validator.supports(clazz)) {
                return true;
            }
        }
        return false;
    }
View Full Code Here


     * @param obj The validated object.
     * @param errors A registry where validation errors are registered.
     */
    public void validate(Object obj, Errors errors) {
        for (Iterator i = validators.iterator(); i.hasNext();) {
            Validator validator = (Validator)i.next();
            if (validator.supports(obj.getClass())) {
                validator.validate(obj, errors);
            }
        }
    }
View Full Code Here

   * before creating a {@code LocalValidatorFactoryBean}.If a JSR-303
   * implementation is not available, a no-op {@link Validator} is returned.
   */
  @Bean
  public Validator mvcValidator() {
    Validator validator = getValidator();
    if (validator == null) {
      if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
        Class<?> clazz;
        try {
          String className = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean";
          clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
        } catch (ClassNotFoundException e) {
          throw new BeanInitializationException("Could not find default validator");
        } catch (LinkageError e) {
          throw new BeanInitializationException("Could not find default validator");
        }
        validator = (Validator) BeanUtils.instantiate(clazz);
      }
      else {
        validator = new Validator() {
          public boolean supports(Class<?> clazz) {
            return false;
          }
          public void validate(Object target, Errors errors) {
          }
View Full Code Here

  }

  public Validator getValidator() {
    Map<WebMvcConfigurer, Validator> validators = new HashMap<WebMvcConfigurer, Validator>();
    for (WebMvcConfigurer delegate : delegates) {
      Validator validator = delegate.getValidator();
      if (validator != null) {
        validators.put(delegate, validator);
      }
    }
    if (validators.size() == 0) {
View Full Code Here

   * before creating a {@code LocalValidatorFactoryBean}.If a JSR-303
   * implementation is not available, a no-op {@link Validator} is returned.
   */
  @Bean
  public Validator mvcValidator() {
    Validator validator = getValidator();
    if (validator == null) {
      if (ClassUtils.isPresent("javax.validation.Validator", getClass().getClassLoader())) {
        Class<?> clazz;
        try {
          String className = "org.springframework.validation.beanvalidation.LocalValidatorFactoryBean";
          clazz = ClassUtils.forName(className, WebMvcConfigurationSupport.class.getClassLoader());
        }
        catch (ClassNotFoundException e) {
          throw new BeanInitializationException("Could not find default validator", e);
        }
        catch (LinkageError e) {
          throw new BeanInitializationException("Could not find default validator", e);
        }
        validator = (Validator) BeanUtils.instantiate(clazz);
      }
      else {
        validator = new Validator() {
          public boolean supports(Class<?> clazz) {
            return false;
          }
          public void validate(Object target, Errors errors) {
          }
View Full Code Here

  }

  public Validator getValidator() {
    List<Validator> candidates = new ArrayList<Validator>();
    for (WebMvcConfigurer configurer : this.delegates) {
      Validator validator = configurer.getValidator();
      if (validator != null) {
        candidates.add(validator);
      }
    }
    return selectSingleInstance(candidates, Validator.class);
View Full Code Here

    BindException errors = (BindException)mav.getModel().get(ERRORS_KEY);
    assertEquals("There should be no errors", 0, errors.getErrorCount());
  }
 
  public void testWithValidatorNotSupportingCommandClass() throws Exception {
    Validator v = new Validator() {
      public boolean supports(Class c) {
        return false;
      }
      public void validate(Object o, Errors e) {}
    };
View Full Code Here

  public void testWithValidatorAddingGlobalError() throws Exception {
    final String errorCode = "someCode";
    final String defaultMessage = "validation error!";
    TestController tc = new TestController();
    tc.setValidator(new Validator() {
      public boolean supports(Class c) {
        return TestBean.class.isAssignableFrom(c);
      }
      public void validate(Object o, Errors e) {
        e.reject(errorCode, defaultMessage);
View Full Code Here

  public void testWithValidatorAndNullFieldError() throws Exception {
    final String errorCode = "someCode";
    final String defaultMessage = "validation error!";
    TestController tc = new TestController();
    tc.setValidator(new Validator() {
      public boolean supports(Class c) {
        return TestBean.class.isAssignableFrom(c);
      }
      public void validate(Object o, Errors e) {
        ValidationUtils.rejectIfEmpty(e, "name", errorCode, defaultMessage);
View Full Code Here

  public void testWithValidatorAndWhitespaceFieldError() throws Exception {
    final String errorCode = "someCode";
    final String defaultMessage = "validation error!";
    TestController tc = new TestController();
    tc.setValidator(new Validator() {
      public boolean supports(Class c) {
        return TestBean.class.isAssignableFrom(c);
      }
      public void validate(Object o, Errors e) {
        ValidationUtils.rejectIfEmptyOrWhitespace(e, "name", errorCode, defaultMessage);
View Full Code Here

TOP

Related Classes of org.springframework.validation.Validator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.