Package org.springframework.validation

Examples of org.springframework.validation.FieldError


            Field f = clazz.getDeclaredField(field);
            Object value = this.getValueObject(f.getType(), fieldValue);
            f.setAccessible(true);
            f.set(formBackingObject, value);
            Errors errors = validateObject(formBackingObject);
            FieldError error = errors.getFieldError(field);
            if (error != null) message = error.getCode();
        }
        return message;
    }
View Full Code Here


            Field f = clazz.getDeclaredField(field);
            Object value = this.getValueObject(f.getType(), fieldValue);
            f.setAccessible(true);
            f.set(formBackingObject, value);
            Errors errors = validateObject(formBackingObject);
            FieldError error = errors.getFieldError(field);
            if (error != null) message = error.getCode();
        }
        return message;
    }
View Full Code Here

    public <T> T visit(Object element) {
        try {
            return super.<T>visit(element);
        } catch (IllegalArgumentException ia) {
            if (FieldError.class.isAssignableFrom(element.getClass())) {
                FieldError e = (FieldError) element;
                FieldError t = new FieldError(e.getObjectName(), e.getField(), e.getRejectedValue(), false, e.getCodes(), null, this.visit(e.getCode()));
                return (T) t;
            } else {
                throw ia;
            }
        }
View Full Code Here

        assertEquals(ERROR_MESSAGE_EMPTY_TODO_TITLE, titleFieldErrorDTO.getMessage());
    }

    @Test
    public void handleFormValidationErrorWhenErrorMessagesAreNotFound() {
        FieldError titleError = createFieldError(OBJECT_NAME, FIELD_TITLE, ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE);
        FieldError descriptionError = createFieldError(OBJECT_NAME, FIELD_DESCRIPTION, ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION);

        List<FieldError> errors = new ArrayList<FieldError>();
        errors.add(titleError);
        errors.add(descriptionError);

        FormValidationError validationError = new FormValidationError(errors);

        when(localeHolderWrapperMock.getCurrentLocale()).thenReturn(Locale.US);

        when(messageSourceMock.getMessage(ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE, titleError.getArguments(), Locale.US)).thenReturn(ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE);
        when(messageSourceMock.getMessage(ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION, descriptionError.getArguments(), Locale.US)).thenReturn(ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION);

        FormValidationErrorDTO dto = controller.handleFormValidationError(validationError);

        verify(localeHolderWrapperMock, times(1)).getCurrentLocale();
        verifyNoMoreInteractions(localeHolderWrapperMock);

        verify(messageSourceMock, times(1)).getMessage(ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE, titleError.getArguments(), Locale.US);
        verify(messageSourceMock, times(1)).getMessage(ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION, descriptionError.getArguments(), Locale.US);
        verifyNoMoreInteractions(messageSourceMock);

        verifyZeroInteractions(serviceMock);

        List<FieldValidationErrorDTO> fieldErrorDTOs = dto.getFieldErrors();
View Full Code Here

        assertEquals(ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION, descriptionFieldErrorDTO.getMessage());
    }

    @Test
    public void handleFormValidationErrorWhenErrorMessagesAreNull() {
        FieldError titleError = createFieldError(OBJECT_NAME, FIELD_TITLE, ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE);
        FieldError descriptionError = createFieldError(OBJECT_NAME, FIELD_DESCRIPTION, ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION);

        List<FieldError> errors = new ArrayList<FieldError>();
        errors.add(titleError);
        errors.add(descriptionError);

        FormValidationError validationError = new FormValidationError(errors);

        when(localeHolderWrapperMock.getCurrentLocale()).thenReturn(Locale.US);

        when(messageSourceMock.getMessage(ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE, titleError.getArguments(), Locale.US)).thenReturn(null);
        when(messageSourceMock.getMessage(ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION, descriptionError.getArguments(), Locale.US)).thenReturn(null);

        FormValidationErrorDTO dto = controller.handleFormValidationError(validationError);

        verify(localeHolderWrapperMock, times(1)).getCurrentLocale();
        verifyNoMoreInteractions(localeHolderWrapperMock);

        verify(messageSourceMock, times(1)).getMessage(ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE, titleError.getArguments(), Locale.US);
        verify(messageSourceMock, times(1)).getMessage(ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION, descriptionError.getArguments(), Locale.US);
        verifyNoMoreInteractions(messageSourceMock);

        verifyZeroInteractions(serviceMock);

        List<FieldValidationErrorDTO> fieldErrorDTOs = dto.getFieldErrors();
View Full Code Here

        assertEquals(expected.getDescription(), actual.getDescription());
        assertEquals(expected.getTitle(), actual.getTitle());
    }

    public FieldError createFieldError(String objectName, String path, String... errorMessageCodes) {
        return new FieldError(objectName,
                path,
                null,
                false,
                errorMessageCodes,
                new Object[]{},
View Full Code Here

        verifyZeroInteractions(localeHolderWrapperMock, messageSourceMock);
    }

    @Test
    public void handleFormValidationError() {
        FieldError titleError = createFieldError(OBJECT_NAME, FIELD_TITLE, ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE);
        FieldError descriptionError = createFieldError(OBJECT_NAME, FIELD_DESCRIPTION, ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION);

        List<FieldError> errors = new ArrayList<FieldError>();
        errors.add(titleError);
        errors.add(descriptionError);

        FormValidationError validationError = new FormValidationError(errors);

        when(localeHolderWrapperMock.getCurrentLocale()).thenReturn(Locale.US);

        when(messageSourceMock.getMessage(ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE, titleError.getArguments(), Locale.US)).thenReturn(ERROR_MESSAGE_EMPTY_TODO_TITLE);
        when(messageSourceMock.getMessage(ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION, descriptionError.getArguments(), Locale.US)).thenReturn(ERROR_MESSAGE_TOO_LONG_DESCRIPTION);

        FormValidationErrorDTO dto = controller.handleFormValidationError(validationError);

        verify(localeHolderWrapperMock, times(1)).getCurrentLocale();
        verifyNoMoreInteractions(localeHolderWrapperMock);

        verify(messageSourceMock, times(1)).getMessage(ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE, titleError.getArguments(), Locale.US);
        verify(messageSourceMock, times(1)).getMessage(ERROR_MESSAGE_CODE_TOO_LONG_DESCRIPTION, descriptionError.getArguments(), Locale.US);
        verifyNoMoreInteractions(messageSourceMock);

        verifyZeroInteractions(serviceMock);

        List<FieldValidationErrorDTO> fieldErrorDTOs = dto.getFieldErrors();
View Full Code Here

        assertEquals(ERROR_MESSAGE_TOO_LONG_DESCRIPTION, descriptionFieldErrorDTO.getMessage());
    }

    @Test
    public void handleFormValidationErrorWhenErrorMessageIsNotFoundWithFirstErrorCode() {
        FieldError titleError = createFieldError(OBJECT_NAME, FIELD_TITLE, ERROR_MESSAGE_CODE_EMPTY_TITLE, ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE);

        List<FieldError> errors = new ArrayList<FieldError>();
        errors.add(titleError);

        FormValidationError validationError = new FormValidationError(errors);

        when(localeHolderWrapperMock.getCurrentLocale()).thenReturn(Locale.US);

        when(messageSourceMock.getMessage(ERROR_MESSAGE_CODE_EMPTY_TITLE, titleError.getArguments(), Locale.US)).thenReturn(ERROR_MESSAGE_CODE_EMPTY_TITLE);
        when(messageSourceMock.getMessage(ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE, titleError.getArguments(), Locale.US)).thenReturn(ERROR_MESSAGE_EMPTY_TODO_TITLE);

        FormValidationErrorDTO dto = controller.handleFormValidationError(validationError);

        verify(localeHolderWrapperMock, times(1)).getCurrentLocale();
        verifyNoMoreInteractions(localeHolderWrapperMock);

        verify(messageSourceMock, times(1)).getMessage(ERROR_MESSAGE_CODE_EMPTY_TITLE, titleError.getArguments(), Locale.US);
        verify(messageSourceMock, times(1)).getMessage(ERROR_MESSAGE_CODE_EMPTY_TODO_TITLE, titleError.getArguments(), Locale.US);
        verifyNoMoreInteractions(messageSourceMock);

        verifyZeroInteractions(serviceMock);

        List<FieldValidationErrorDTO> fieldErrorDTOs = dto.getFieldErrors();
View Full Code Here

    for (int i = 0; i < messages.length; i++) {
      Message message = messages[i];
      if (message.getSource() == null) {
        errors.add(new ObjectError(objectName, message.getText()));
      } else {
        errors.add(new FieldError(objectName, (String) message.getSource(), message.getText()));
      }
    }
    return errors;
  }
View Full Code Here

  public void addAllErrors(Errors errors) {
    Iterator it = errors.getAllErrors().iterator();
    while (it.hasNext()) {
      ObjectError error = (ObjectError) it.next();
      if (error instanceof FieldError) {
        FieldError fieldError = (FieldError) error;
        rejectValue(fieldError.getField(), error.getCode(), error.getArguments(), error.getDefaultMessage());
      } else {
        reject(error.getCode(), error.getArguments(), error.getDefaultMessage());
      }
    }
  }
View Full Code Here

TOP

Related Classes of org.springframework.validation.FieldError

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.