@Override @Nullable public Object getFieldValue(String field) { Object value = this.source.getFieldValue(field); return (value instanceof String ? HtmlUtils.htmlEscape((String) value) : value); }
/** * Reject the given field with the given error code, error arguments * and default message if the value is empty. * <p>An 'empty' value in this context means either {@code null} or * the empty string "". * <p>The object whose field is being validated does not need to be passed * in because the {@link Errors} instance can resolve field values by itself * (it will usually hold an internal reference to the target object). * @param errors the {@code Errors} instance to register errors on * @param field the field name to check * @param errorCode the error code, interpretable as message key * @param errorArgs the error arguments, for argument binding via MessageFormat * (can be {@code null}) * @param defaultMessage fallback default message */ public static void rejectIfEmpty(Errors errors, String field, String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage) { Assert.notNull(errors, "Errors object must not be null"); Object value = errors.getFieldValue(field); if (value == null || !StringUtils.hasLength(value.toString())) { errors.rejectValue(field, errorCode, errorArgs, defaultMessage); } }
/** * Reject the given field with the given error code, error arguments * and default message if the value is empty or just contains whitespace. * <p>An 'empty' value in this context means either {@code null}, * the empty string "", or consisting wholly of whitespace. * <p>The object whose field is being validated does not need to be passed * in because the {@link Errors} instance can resolve field values by itself * (it will usually hold an internal reference to the target object). * @param errors the {@code Errors} instance to register errors on * @param field the field name to check * @param errorCode the error code, interpretable as message key * @param errorArgs the error arguments, for argument binding via MessageFormat * (can be {@code null}) * @param defaultMessage fallback default message */ public static void rejectIfEmptyOrWhitespace( Errors errors, String field, String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage) { Assert.notNull(errors, "Errors object must not be null"); Object value = errors.getFieldValue(field); if (value == null ||!StringUtils.hasText(value.toString())) { errors.rejectValue(field, errorCode, errorArgs, defaultMessage); } }
@Override @Nullable public Object getFieldValue(String field) { Object value = this.source.getFieldValue(field); return (value instanceof String ? HtmlUtils.htmlEscape((String) value) : value); }
/** * Reject the given field with the given error code, error arguments * and default message if the value is empty. * <p>An 'empty' value in this context means either {@code null} or * the empty string "". * <p>The object whose field is being validated does not need to be passed * in because the {@link Errors} instance can resolve field values by itself * (it will usually hold an internal reference to the target object). * @param errors the {@code Errors} instance to register errors on * @param field the field name to check * @param errorCode the error code, interpretable as message key * @param errorArgs the error arguments, for argument binding via MessageFormat * (can be {@code null}) * @param defaultMessage fallback default message */ public static void rejectIfEmpty(Errors errors, String field, String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage) { Assert.notNull(errors, "Errors object must not be null"); Object value = errors.getFieldValue(field); if (value == null || !StringUtils.hasLength(value.toString())) { errors.rejectValue(field, errorCode, errorArgs, defaultMessage); } }
/** * Reject the given field with the given error code, error arguments * and default message if the value is empty or just contains whitespace. * <p>An 'empty' value in this context means either {@code null}, * the empty string "", or consisting wholly of whitespace. * <p>The object whose field is being validated does not need to be passed * in because the {@link Errors} instance can resolve field values by itself * (it will usually hold an internal reference to the target object). * @param errors the {@code Errors} instance to register errors on * @param field the field name to check * @param errorCode the error code, interpretable as message key * @param errorArgs the error arguments, for argument binding via MessageFormat * (can be {@code null}) * @param defaultMessage fallback default message */ public static void rejectIfEmptyOrWhitespace( Errors errors, String field, String errorCode, @Nullable Object[] errorArgs, @Nullable String defaultMessage) { Assert.notNull(errors, "Errors object must not be null"); Object value = errors.getFieldValue(field); if (value == null ||!StringUtils.hasText(value.toString())) { errors.rejectValue(field, errorCode, errorArgs, defaultMessage); } }
this.value = this.errors.getFieldValue(this.expression); this.valueType = this.errors.getFieldType(this.expression); if (this.errors instanceof BindingResult) {
this.value = this.errors.getFieldValue(this.expression); this.valueType = this.errors.getFieldType(this.expression); if (this.errors instanceof BindingResult) {
errors.getFieldValue("name") + "-" + errors.getFieldValue("age"));
this.value = this.errors.getFieldValue(this.expression); this.valueType = this.errors.getFieldType(this.expression); if (this.errors instanceof BindingResult) {
@Test public void testValidationWithOptionalField() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); MainBeanWithOptional mainBean = new MainBeanWithOptional(); Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean"); validator.validate(mainBean, errors); Object rejected = errors.getFieldValue("inner.value"); assertNull(rejected); }
@Test public void testInnerBeanValidation() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); MainBean mainBean = new MainBean(); Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean"); validator.validate(mainBean, errors); Object rejected = errors.getFieldValue("inner.value"); assertNull(rejected); }
@Test public void testValidationWithOptionalField() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); MainBeanWithOptional mainBean = new MainBeanWithOptional(); Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean"); validator.validate(mainBean, errors); Object rejected = errors.getFieldValue("inner.value"); assertNull(rejected); }
@Test public void testInnerBeanValidation() { LocalValidatorFactoryBean validator = new LocalValidatorFactoryBean(); validator.afterPropertiesSet(); MainBean mainBean = new MainBean(); Errors errors = new BeanPropertyBindingResult(mainBean, "mainBean"); validator.validate(mainBean, errors); Object rejected = errors.getFieldValue("inner.value"); assertNull(rejected); }
FieldError fieldError = errors.getFieldError(); assertTrue("Field error code not escaped", "NAME_EMPTY &".equals(fieldError.getCode())); assertTrue("Field value escaped", "empty &".equals(errors.getFieldValue("name"))); FieldError fieldErrorInList = errors.getFieldErrors().get(0); assertTrue("Same field error in list", fieldError.getDefaultMessage().equals(fieldErrorInList.getDefaultMessage())); assertTrue("Name error message escaped", "message: &".equals(nameError.getDefaultMessage())); assertTrue("Name error code not escaped", "NAME_EMPTY &".equals(nameError.getCode())); assertTrue("Name value escaped", "empty &".equals(errors.getFieldValue("name"))); FieldError nameErrorInList = errors.getFieldErrors("name").get(0); assertTrue("Same name error in list", nameError.getDefaultMessage().equals(nameErrorInList.getDefaultMessage())); assertTrue("Age error message escaped", "message: <tag>".equals(ageError.getDefaultMessage())); assertTrue("Age error code not escaped", "AGE_NOT_SET <tag>".equals(ageError.getCode())); assertTrue("Age value not escaped", (new Integer(0)).equals(errors.getFieldValue("age"))); FieldError ageErrorInList = errors.getFieldErrors("age").get(0); assertTrue("Same name error in list", ageError.getDefaultMessage().equals(ageErrorInList.getDefaultMessage()));
errors.rejectValue("array[0]", "NOT_ROD", "are you sure you're not Rod?"); assertEquals("arraya", errors.getFieldValue("array[0]")); assertEquals(1, errors.getFieldErrorCount("array[0]")); assertEquals("NOT_ROD", errors.getFieldError("array[0]").getCode()); assertEquals("NOT_ROD.org.springframework.tests.sample.beans.DerivedTestBean", errors.getFieldError("array[0]").getCodes()[4]); assertEquals("NOT_ROD", errors.getFieldError("array[0]").getCodes()[5]); assertEquals("arraya", errors.getFieldValue("array[0]"));
/** * Test the field lengths are valid * * @param errors * @param aClass the class of the object being tested * @param fields a var args that contains all of the fields from the model * @should pass validation if regEx field length is not too long * @should fail validation if regEx field length is too long * @should fail validation if name field length is too long * @should return immediately if validation is disabled and have no errors */ public static void validateFieldLengths(Errors errors, Class<?> aClass, String... fields) { if (disableValidation) { return; } Assert.notNull(errors, "Errors object must not be null"); for (String field : fields) { Object value = errors.getFieldValue(field); if (value == null || !(value instanceof String)) { continue; } int length = Context.getAdministrationService().getMaximumPropertyLength((Class<? extends OpenmrsObject>) aClass, field); if (length == -1) { return; } if (((String) value).length() > length) { errors.rejectValue(field, "error.exceededMaxLengthOfField", new Object[] { length }, null); } } }
errors.rejectValue("map[key0]", "NOT_NULL", "should not be null"); assertEquals("arraya", errors.getFieldValue("array[0]")); assertEquals(1, errors.getFieldErrorCount("array[0]")); assertEquals("NOT_ROD", errors.getFieldError("array[0]").getCode()); assertEquals("NOT_ROD.org.springframework.tests.sample.beans.DerivedTestBean", errors.getFieldError("array[0]").getCodes()[4]); assertEquals("NOT_ROD", errors.getFieldError("array[0]").getCodes()[5]); assertEquals("arraya", errors.getFieldValue("array[0]"));
assertEquals("argh", errors.getFieldValue("age")); Validator spouseValidator = new SpouseValidator(); spouseValidator.validate(tb.getSpouse(), errors);
@Override @Nullable public Object getFieldValue(String field) { Object value = this.source.getFieldValue(field); return (value instanceof String ? HtmlUtils.htmlEscape((String) value) : value); }