Search in sources :

Example 71 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ValidatorFactoryTests method testSpringValidationWithErrorInSetElement.

@Test
public void testSpringValidationWithErrorInSetElement() throws Exception {
    LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean();
    validator.afterPropertiesSet();
    ValidPerson person = new ValidPerson();
    person.getAddressSet().add(new ValidAddress());
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, result);
    assertEquals(3, result.getErrorCount());
    FieldError fieldError = result.getFieldError("name");
    assertEquals("name", fieldError.getField());
    fieldError = result.getFieldError("address.street");
    assertEquals("address.street", fieldError.getField());
    fieldError = result.getFieldError("addressSet[].street");
    assertEquals("addressSet[].street", fieldError.getField());
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) FieldError(org.springframework.validation.FieldError) Test(org.junit.Test)

Example 72 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project chassis by Kixeye.

the class RawWebSocketMessage method deserialize.

/**
	 * Deserializes the given message.
	 * 
	 * @param action
	 * @return
	 * @throws Exception
	 */
public T deserialize(WebSocketAction action) throws Exception {
    // first deserialize
    T message = null;
    if (messageClass != null) {
        message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass);
    }
    // then validate
    if (message != null && action.shouldValidatePayload()) {
        SpringValidatorAdapter validatorAdapter = new SpringValidatorAdapter(messageValidator);
        BeanPropertyBindingResult result = new BeanPropertyBindingResult(message, messageClass.getName());
        validatorAdapter.validate(message, result);
        if (result.hasErrors()) {
            throw new MethodArgumentNotValidException(new MethodParameter(action.getMethod(), action.getPayloadParameterIndex()), result);
        }
    }
    return message;
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SpringValidatorAdapter(org.springframework.validation.beanvalidation.SpringValidatorAdapter) MethodParameter(org.springframework.core.MethodParameter) MethodArgumentNotValidException(org.springframework.web.bind.MethodArgumentNotValidException) ByteBufferBackedInputStream(com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream)

Example 73 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project Asqatasun by Asqatasun.

the class UserManagementControllerTest method testSubmitEditUserForm.

/**
     * Test of submitEditUserForm method, of class UserManagementController.
     */
public void testSubmitEditUserForm() throws Exception {
    System.out.println("submitEditUserForm");
    instance = new UserManagementController();
    setUpMockRoleDataService();
    setUpMockUserDataService(false, false, false, true, false);
    setUpMockAuthenticationContext();
    instance.setUserDataService(mockUserDataService);
    CreateUserFormValidator createUserFormValidator = new CreateUserFormValidator();
    createUserFormValidator.setUserDataService(mockUserDataService);
    instance.setCreateUserFormValidator(createUserFormValidator);
    // Finally the form is conform and the admin page is returned
    CreateUserCommand createUserCommand = CreateUserCommandFactory.getInstance().getNewCreateUserCommand();
    createUserCommand.setEmail("user@test.com");
    createUserCommand.setLastName("newName");
    createUserCommand.setFirstName("newFirstName");
    createUserCommand.setPhoneNumber("0102030405");
    createUserCommand.setActivated(false);
    createUserCommand.setAdmin(true);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.getSession().setAttribute(TgolKeyStore.USER_ID_KEY, Long.valueOf(5));
    BindingResult bindingResult = new BeanPropertyBindingResult(createUserCommand, "createUserCommand");
    Model model = new ExtendedModelMap();
    String result = instance.submitEditUserForm(createUserCommand, bindingResult, request, model);
    assertEquals(TgolKeyStore.ADMIN_VIEW_NAME, result);
    assertFalse(bindingResult.hasErrors());
    assertTrue(bindingResult.getFieldErrors().isEmpty());
    assertEquals(2, model.asMap().size());
    assertEquals("user@test.com", model.asMap().get(TgolKeyStore.UPDATED_USER_NAME_KEY));
    assertTrue(((List<User>) model.asMap().get(TgolKeyStore.USER_LIST_KEY)).isEmpty());
}
Also used : BindingResult(org.springframework.validation.BindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) User(org.asqatasun.webapp.entity.user.User) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Model(org.springframework.ui.Model) CreateUserFormValidator(org.asqatasun.webapp.validator.CreateUserFormValidator) CreateUserCommand(org.asqatasun.webapp.command.CreateUserCommand)

Aggregations

BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)73 Test (org.junit.Test)60 TestBean (org.springframework.tests.sample.beans.TestBean)19 Errors (org.springframework.validation.Errors)18 StringReader (java.io.StringReader)17 Document (org.dom4j.Document)17 Element (org.dom4j.Element)17 SAXReader (org.dom4j.io.SAXReader)17 BindingResult (org.springframework.validation.BindingResult)11 PropertyEditorSupport (java.beans.PropertyEditorSupport)10 List (java.util.List)10 ArrayList (java.util.ArrayList)8 CreateUserCommand (org.asqatasun.webapp.command.CreateUserCommand)6 CreateUserFormValidator (org.asqatasun.webapp.validator.CreateUserFormValidator)6 MockBodyContent (org.springframework.mock.web.test.MockBodyContent)6 LinkedList (java.util.LinkedList)5 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)5 Model (org.springframework.ui.Model)5 FieldError (org.springframework.validation.FieldError)5 ObjectError (org.springframework.validation.ObjectError)5