Example usage for org.springframework.validation BindException hasGlobalErrors

List of usage examples for org.springframework.validation BindException hasGlobalErrors

Introduction

In this page you can find the example usage for org.springframework.validation BindException hasGlobalErrors.

Prototype

@Override
    public boolean hasGlobalErrors() 

Source Link

Usage

From source file:org.springmodules.validation.bean.BeanValidatorIntegrationTests.java

public void testBeanValidator_WithCustomValangFunctions() throws Exception {

    Map functionsByName = new HashMap();
    functionsByName.put("tupper", UpperCaseFunction.class.getName());
    ValangCondition goodCondition = new ValangCondition("tupper(name) == 'URI'", functionsByName, null);
    ValangCondition badCondition = new ValangCondition("tupper(name) == 'Uri'", functionsByName, null);

    // creating the validation configuration for the bean.
    DefaultBeanValidationConfiguration personValidationConfiguration = new DefaultBeanValidationConfiguration();
    personValidationConfiguration.addGlobalRule(new DefaultValidationRule(goodCondition, "good"));
    personValidationConfiguration.addGlobalRule(new DefaultValidationRule(badCondition, "bad"));
    SimpleBeanValidationConfigurationLoader loader = new SimpleBeanValidationConfigurationLoader();
    loader.setClassValidation(Person.class, personValidationConfiguration);

    BeanValidator validator = new BeanValidator(loader);

    validator.setErrorCodeConverter(new ModelAwareErrorCodeConverter());
    validator.setConfigurationLoader(loader);

    Person person = new Person("Uri");
    BindException errors = new BindException(person, "person");

    validator.validate(person, errors);/*from  ww w.java 2s . c o  m*/

    assertTrue(errors.hasGlobalErrors());
    assertEquals(1, errors.getGlobalErrorCount());
    assertEquals("Person[bad]", errors.getGlobalError().getCode());
}

From source file:org.springmodules.validation.bean.BeanValidatorIntegrationTests.java

public void testBeanValidator_WithCustomValangFunctions2() throws Exception {

    Map functionsByName = new HashMap();
    functionsByName.put("tupper", UpperCaseFunction.class.getName());

    ValangConditionExpressionParser conditionExpressionParser = new ValangConditionExpressionParser();
    conditionExpressionParser.setCustomFunctions(functionsByName);

    DefaultValidationRuleElementHandlerRegistry registry = new DefaultValidationRuleElementHandlerRegistry();
    registry.setConditionExpressionParser(conditionExpressionParser);
    registry.afterPropertiesSet();// w ww.j a  va 2  s . c o m

    DefaultXmlBeanValidationConfigurationLoader loader = new DefaultXmlBeanValidationConfigurationLoader();
    loader.setResource(new ClassPathResource("validation.xml", getClass()));
    loader.setElementHandlerRegistry(registry);
    loader.afterPropertiesSet();

    BeanValidator validator = new BeanValidator(loader);
    validator.setErrorCodeConverter(new ModelAwareErrorCodeConverter());

    Person person = new Person("Uri");
    person.setHomeless(false);
    person.setAddress(new Address(null, "Amsterdam"));
    BindException errors = new BindException(person, "person");

    validator.validate(person, errors);

    assertTrue(errors.hasGlobalErrors());
    assertEquals(1, errors.getGlobalErrorCount());
    assertEquals(1, errors.getFieldErrorCount());
    assertEquals("Person[bad]", errors.getGlobalError().getCode());
    assertEquals("Address.street[not.null]", errors.getFieldError("address.street").getCode());
}

From source file:org.springmodules.validation.bean.BeanValidatorIntegrationTests.java

public void testBeanValidator_WithManualConfiguration() throws Exception {

    // creating the validation configuration for the bean.
    DefaultBeanValidationConfiguration personValidationConfiguration = new DefaultBeanValidationConfiguration();
    personValidationConfiguration//from   ww w . j a v a  2s . c o m
            .addGlobalRule(new DefaultValidationRule(Conditions.minLength("name", 4), "minLength"));
    personValidationConfiguration.addPropertyRule("name",
            new DefaultValidationRule(Conditions.minLength("name", 5), "minLength"));
    SimpleBeanValidationConfigurationLoader loader = new SimpleBeanValidationConfigurationLoader();
    loader.setClassValidation(Person.class, personValidationConfiguration);

    BeanValidator validator = new BeanValidator();
    validator.setErrorCodeConverter(new ModelAwareErrorCodeConverter());
    validator.setConfigurationLoader(loader);

    Person person = new Person("Uri");
    BindException errors = new BindException(person, "person");

    validator.validate(person, errors);

    assertTrue(errors.hasFieldErrors("name"));
    assertTrue(errors.hasGlobalErrors());
    assertEquals(1, errors.getFieldErrorCount("name"));
    assertEquals(1, errors.getGlobalErrorCount());
    assertEquals("Person[minLength]", errors.getGlobalError().getCode());
    assertEquals("Person.name[minLength]", errors.getFieldError("name").getCode());

}

From source file:org.springmodules.validation.bean.BeanValidatorIntegrationTests.java

public void testBeanValidator_WithApplicationContext() throws Exception {
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
            "org/springmodules/validation/bean/beanValidator-tests.xml");

    Person person = new Person("Uri");
    BindException errors = new BindException(person, "person");

    Validator validator = (Validator) context.getBean("validator");
    validator.validate(person, errors);/*  w w  w  .  ja v a2s. co  m*/

    assertTrue(errors.hasGlobalErrors());
    assertEquals(1, errors.getGlobalErrorCount());
    assertEquals("Person[bad]", errors.getGlobalError().getCode());

}