@Override public void processMissingFieldError(String missingField, BindingResult bindingResult) { // Create field error with code "required". String fixedField = bindingResult.getNestedPath() + missingField; String[] codes = bindingResult.resolveMessageCodes(MISSING_FIELD_ERROR_CODE, missingField); Object[] arguments = getArgumentsForBindError(bindingResult.getObjectName(), fixedField); FieldError error = new FieldError(bindingResult.getObjectName(), fixedField, "", true, codes, arguments, "Field '" + fixedField + "' is required"); bindingResult.addError(error); }
/** * Close this DataBinder, which may result in throwing * a BindException if it encountered any errors. * @return the model Map, containing target object and Errors instance * @throws BindException if there were any errors in the bind operation * @see BindingResult#getModel() */ public Map<?, ?> close() throws BindException { if (getBindingResult().hasErrors()) { throw new BindException(getBindingResult()); } return getBindingResult().getModel(); }
/** * Return the underlying PropertyAccessor of this binder's BindingResult. */ protected ConfigurablePropertyAccessor getPropertyAccessor() { return getInternalBindingResult().getPropertyAccessor(); }
@Test public void testValidationUtilsNull() throws Exception { TestBean tb = new TestBean(); Errors errors = new BeanPropertyBindingResult(tb, "tb"); Validator testValidator = new EmptyValidator(); testValidator.validate(tb, errors); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY", errors.getFieldError("name").getCode()); }
@Test public void testInvokeValidatorSunnyDay() throws Exception { TestBean tb = new TestBean(); Errors errors = new BeanPropertyBindingResult(tb, "tb"); ValidationUtils.invokeValidator(new EmptyValidator(), tb, errors); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY", errors.getFieldError("name").getCode()); }
/** * Return the underlying TypeConverter of this binder's BindingResult. */ protected TypeConverter getTypeConverter() { if (getTarget() != null) { return getInternalBindingResult().getPropertyAccessor(); } else { return getSimpleTypeConverter(); } }
@Override public String[] resolveMessageCodes(String errorCode) { return getMessageCodesResolver().resolveMessageCodes(errorCode, getObjectName()); }
/** * Returns the underlying PropertyAccessor. * @see #getPropertyAccessor() */ @Override public PropertyEditorRegistry getPropertyEditorRegistry() { return (getTarget() != null ? getPropertyAccessor() : null); }
/** * Return the internal BindingResult held by this DataBinder, * as an AbstractPropertyBindingResult. */ protected AbstractPropertyBindingResult getInternalBindingResult() { if (this.bindingResult == null) { initBeanPropertyAccess(); } return this.bindingResult; }
/** * Return the BindingResult instance created by this DataBinder. * This allows for convenient access to the binding results after * a bind operation. * @return the BindingResult instance, to be treated as BindingResult * or as Errors instance (Errors is a super-interface of BindingResult) * @see Errors * @see #bind */ public BindingResult getBindingResult() { return getInternalBindingResult(); }
/** * Post-process the given message code, built by this resolver. * <p>The default implementation applies the specified prefix, if any. * @param code the message code as built by this resolver * @return the final message code to be returned * @see #setPrefix */ protected String postProcessMessageCode(String code) { return getPrefix() + code; }
@Override public void setAsText(String text) throws IllegalArgumentException { setValue("list" + text); } @Override
@Override public String format(String errorCode, String objectName, String field) { return DefaultMessageCodesResolver.Format.toDelimitedString( "CUSTOM-" + errorCode, objectName, field); } });
@Override public void setAsText(String text) throws IllegalArgumentException { setValue("list" + text); } @Override
@Override public void setAsText(String text) throws IllegalArgumentException { setValue("prefix_" + text); } @Override
@Override public void setAsText(String text) throws IllegalArgumentException { setValue("prefix" + text); } @Override
@Override public String convert(NameBean source) { return "[" + source.getName() + "]"; } }
@Override public void setAsText(String text) throws IllegalArgumentException { setValue("list" + text); } });
@Test public void testValidationUtilsEmpty() throws Exception { TestBean tb = new TestBean(""); Errors errors = new BeanPropertyBindingResult(tb, "tb"); Validator testValidator = new EmptyValidator(); testValidator.validate(tb, errors); assertTrue(errors.hasFieldErrors("name")); assertEquals("EMPTY", errors.getFieldError("name").getCode()); }
/** * Return the underlying TypeConverter of this binder's BindingResult. */ protected PropertyEditorRegistry getPropertyEditorRegistry() { if (getTarget() != null) { return getInternalBindingResult().getPropertyAccessor(); } else { return getSimpleTypeConverter(); } }