Package org.springframework.validation

Examples of org.springframework.validation.BindException


        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());
    }
View Full Code Here


    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);

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

    }
View Full Code Here

        ClassPathXmlApplicationContext context =
            new ClassPathXmlApplicationContext("org/springmodules/validation/bean/beanValidator-tests.xml");

        Person person = new Person("Uri");
        person.setPhone("1234"); // should be validation error - length < 7
        BindException errors = new BindException(person, "person");

        Validator validator = (Validator) context.getBean("validator");
        validator.validate(person, errors);

        assertTrue(errors.hasFieldErrors("phone"));
        assertEquals(1, errors.getFieldErrorCount("phone"));
        assertEquals("Person.phone[min.length]", errors.getFieldError("phone").getCode());
    }
View Full Code Here

        person.setAge(-1);
        person.setEmail("uri@b");
        person.setPassword("pa");
        person.setConfirmPassword("pa1");

        BindException errors = new BindException(person, "person");
        validator.validate(person, errors);

        assertEquals(1, errors.getGlobalErrorCount());

        // testing the custom error code converter
        String[] codes = errors.getGlobalError().getCodes();
        assertTrue(ArrayUtils.contains(codes, "test.passwords.do.not.match"));

        assertEquals(3, errors.getFieldErrorCount());

    }
View Full Code Here

        person.setAge(-1);
        person.setEmail("uri@b");
        person.setPassword("pa");
        person.setConfirmPassword("pa1");

        BindException errors = new BindException(person, "person");
        validator.validate(person, errors);

        assertEquals(1, errors.getGlobalErrorCount());

        // testing the custom error code converter
        String[] codes = errors.getGlobalError().getCodes();
        assertTrue(ArrayUtils.contains(codes, "test.passwords.do.not.match"));

        assertEquals(4, errors.getFieldErrorCount());

    }
View Full Code Here

        person.setNullableInteger(new Integer(3));

        AnnotationBeanValidationConfigurationLoader loader = new AnnotationBeanValidationConfigurationLoader();
        BeanValidator validator = new BeanValidator(loader);

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

        validator.validate(person, errors);

        assertTrue(errors.hasGlobalErrors());
        assertEquals(3, errors.getGlobalErrorCount());
        assertTrue(errors.hasFieldErrors());
        assertTrue(errors.hasFieldErrors("firstName"));
        assertTrue(errors.hasFieldErrors("lastName"));
        assertEquals(1, errors.getFieldErrorCount("lastName"));
        assertTrue(errors.hasFieldErrors("birthday"));
        assertEquals(1, errors.getFieldErrorCount("birthday"));
        assertEquals(1, errors.getFieldErrorCount("age"));
        assertEquals("Person.age[just.another.error.code]", errors.getFieldError("age").getCode());
        assertTrue(errors.hasFieldErrors("father"));
        assertTrue(errors.hasFieldErrors("mother.*"));
        assertTrue(errors.hasFieldErrors("friends"));
        assertTrue(errors.hasFieldErrors("address.street"));
        assertFalse(errors.hasFieldErrors("nullableString"));
        assertTrue(errors.hasFieldErrors("nullableInteger"));
        assertEquals("Address.street[not.null]", errors.getFieldError("address.street").getCode());

    }
View Full Code Here

        person.setAge(-1);
        person.setEmail("uri@b");
        person.setPassword("pa");
        person.setConfirmPassword("pa1");

        BindException errors = new BindException(person, "person");
        BeanValidator validator = new BeanValidator(loader);
        validator.validate(person, errors);

        assertEquals(2, errors.getGlobalErrorCount());
        assertEquals(1, errors.getFieldErrorCount("smallInteger"));
        assertEquals(1, errors.getFieldErrorCount("lastName"));
        assertEquals("Person.lastName[validateLastNameIsLongerThanTen()]", errors.getFieldError("lastName").getCode());

    }
View Full Code Here

        person.setPassword("pa");
        person.setConfirmPassword("pa1");

        ValidationContextUtils.setContext("ctx1");

        BindException errors = new BindException(person, "person");
        validator.validate(person, errors);

        assertEquals(1, errors.getGlobalErrorCount());
        assertFalse(errors.hasFieldErrors("smallInteger"));
        assertEquals(1, errors.getFieldErrorCount("lastName"));

        ValidationContextUtils.clearContext();

        ValidationContextUtils.setContext("ctx2");

        errors = new BindException(person, "person");
        validator.validate(person, errors);

        assertEquals(1, errors.getGlobalErrorCount());
        assertEquals(1, errors.getFieldErrorCount("smallInteger"));
        assertEquals(1, errors.getFieldErrorCount("lastName"));
        assertEquals("Person.lastName[validateLastNameIsLongerThanTen()]", errors.getFieldError("lastName").getCode());

        ValidationContextUtils.clearContext();

    }
View Full Code Here

        person.setNullableInteger(new Integer(3)); // invalid (ctx2) - min value is 10

        AnnotationBeanValidationConfigurationLoader loader = new AnnotationBeanValidationConfigurationLoader();
        BeanValidator validator = new BeanValidator(loader);

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

        setContext("ctx1");

        validator.validate(person, errors);

        assertTrue(errors.hasGlobalErrors());
        assertEquals(2, errors.getGlobalErrorCount());
        assertTrue(errors.hasFieldErrors());
        assertTrue(errors.hasFieldErrors("firstName"));
        assertTrue(errors.hasFieldErrors("lastName"));
        assertEquals(1, errors.getFieldErrorCount("lastName"));
        assertFalse(errors.hasFieldErrors("birthday"));
        assertEquals(0, errors.getFieldErrorCount("age"));
        assertTrue(errors.hasFieldErrors("father"));
        assertTrue(errors.hasFieldErrors("mother.*"));
        assertTrue(errors.hasFieldErrors("friends"));
        assertTrue(errors.hasFieldErrors("address.street"));
        assertFalse(errors.hasFieldErrors("nullableString"));
        assertFalse(errors.hasFieldErrors("nullableInteger"));
        assertEquals("Address.street[not.null]", errors.getFieldError("address.street").getCode());

        clearContext();

        setContext("ctx2");

        errors = new BindException(person, "person");
        validator.validate(person, errors);

        assertFalse(errors.hasGlobalErrors());
        assertTrue(errors.hasFieldErrors());
        assertFalse(errors.hasFieldErrors("firstName"));
        assertFalse(errors.hasFieldErrors("lastName"));
        assertEquals(0, errors.getFieldErrorCount("lastName"));
        assertTrue(errors.hasFieldErrors("birthday"));
        assertEquals(1, errors.getFieldErrorCount("birthday"));
        assertEquals(1, errors.getFieldErrorCount("age"));
        assertEquals("PersonWithContext.age[just.another.error.code]", errors.getFieldError("age").getCode());
        assertFalse(errors.hasFieldErrors("father"));
        assertTrue(errors.hasFieldErrors("mother.*"));
        assertFalse(errors.hasFieldErrors("friends"));
        assertTrue(errors.hasFieldErrors("address.street"));
        assertFalse(errors.hasFieldErrors("nullableString"));
        assertTrue(errors.hasFieldErrors("nullableInteger"));
        assertEquals("Address.street[not.null]", errors.getFieldError("address.street").getCode());

        clearContext();

    }
View Full Code Here

        DefaultXmlBeanValidationConfigurationLoader loader = createLoader("TestBean1.vld.xml", context);

        BeanValidator validator = new BeanValidator(loader);
       
        TestBean bean = new TestBean();
        BindException errors = new BindException(bean, "bean");
        validator.validate(bean, errors);
        assertTrue(errors.hasErrors());
        assertTrue(errors.hasFieldErrors("name"));

        bean = new TestBean("name");
        errors = new BindException(bean, "bean");
        validator.validate(bean, errors);
        assertFalse(errors.hasErrors());
    }
View Full Code Here

TOP

Related Classes of org.springframework.validation.BindException

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.