1 16 17 package org.springframework.validation; 18 19 import org.springframework.beans.PropertyAccessException; 20 import org.springframework.context.support.DefaultMessageSourceResolvable; 21 22 43 public class DefaultBindingErrorProcessor implements BindingErrorProcessor { 44 45 50 public static final String MISSING_FIELD_ERROR_CODE = "required"; 51 52 53 public void processMissingFieldError(String missingField, BindingResult bindingResult) { 54 String [] codes = bindingResult.resolveMessageCodes(MISSING_FIELD_ERROR_CODE, missingField); 56 Object [] arguments = getArgumentsForBindError(bindingResult.getObjectName(), missingField); 57 bindingResult.addError(new FieldError( 58 bindingResult.getObjectName(), missingField, "", true, 59 codes, arguments, "Field '" + missingField + "' is required")); 60 } 61 62 public void processPropertyAccessException(PropertyAccessException ex, BindingResult bindingResult) { 63 String field = ex.getPropertyChangeEvent().getPropertyName(); 65 Object value = ex.getPropertyChangeEvent().getNewValue(); 66 String [] codes = bindingResult.resolveMessageCodes(ex.getErrorCode(), field); 67 Object [] arguments = getArgumentsForBindError(bindingResult.getObjectName(), field); 68 bindingResult.addError(new FieldError( 69 bindingResult.getObjectName(), field, value, true, 70 codes, arguments, ex.getLocalizedMessage())); 71 } 72 73 83 protected Object [] getArgumentsForBindError(String objectName, String field) { 84 String [] codes = new String [] {objectName + Errors.NESTED_PATH_SEPARATOR + field, field}; 85 String defaultMessage = field; 86 return new Object [] {new DefaultMessageSourceResolvable(codes, defaultMessage)}; 87 } 88 89 } 90 | Popular Tags |