use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.
the class DataBinderTests method testBindingWithRequiredFields.
/**
* Tests for required field, both null, non-existing and empty strings.
*/
@Test
void testBindingWithRequiredFields() {
TestBean tb = new TestBean();
tb.setSpouse(new TestBean());
DataBinder binder = new DataBinder(tb, "person");
binder.setRequiredFields("touchy", "name", "age", "date", "spouse.name");
PropertyValues pvs = new PropertyValues();
pvs.add("touchy", "");
pvs.add("name", null);
pvs.add("age", null);
pvs.add("spouse.name", " ");
binder.bind(pvs);
BindingResult br = binder.getBindingResult();
assertThat(br.getErrorCount()).as("Wrong number of errors").isEqualTo(5);
assertThat(br.getFieldError("touchy").getCode()).isEqualTo("required");
assertThat(br.getFieldValue("touchy")).isEqualTo("");
assertThat(br.getFieldError("name").getCode()).isEqualTo("required");
assertThat(br.getFieldValue("name")).isEqualTo("");
assertThat(br.getFieldError("age").getCode()).isEqualTo("required");
assertThat(br.getFieldValue("age")).isEqualTo("");
assertThat(br.getFieldError("date").getCode()).isEqualTo("required");
assertThat(br.getFieldValue("date")).isEqualTo("");
assertThat(br.getFieldError("spouse.name").getCode()).isEqualTo("required");
assertThat(br.getFieldValue("spouse.name")).isEqualTo("");
}
use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.
the class RequestResponseBodyMethodProcessorMockTests method resolveArgumentNotValid.
@Test
public void resolveArgumentNotValid() throws Throwable {
assertThatExceptionOfType(MethodArgumentNotValidException.class).isThrownBy(() -> testResolveArgumentWithValidation(new SimpleBean(null))).satisfies(ex -> {
BindingResult bindingResult = ex.getBindingResult();
assertThat(bindingResult.getObjectName()).isEqualTo("simpleBean");
assertThat(bindingResult.getErrorCount()).isEqualTo(1);
assertThat(bindingResult.getFieldError("name")).isNotNull();
});
}
use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.
the class DefaultErrorAttributesTests method withoutBindingErrors.
@Test
void withoutBindingErrors() {
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
bindingResult.addError(new ObjectError("c", "d"));
Exception ex = new BindException(bindingResult);
testBindingResult(bindingResult, ex, ErrorAttributeOptions.defaults());
}
use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.
the class MethodArgumentNotValidException method getMessage.
@Override
public String getMessage() {
StringBuilder sb = new StringBuilder("Validation failed for argument [").append(this.parameter.getParameterIndex()).append("] in ").append(this.parameter.getExecutable().toGenericString());
BindingResult bindingResult = getBindingResult();
if (bindingResult.getErrorCount() > 1) {
sb.append(" with ").append(bindingResult.getErrorCount()).append(" errors");
}
sb.append(": ");
for (ObjectError error : bindingResult.getAllErrors()) {
sb.append('[').append(error).append("] ");
}
return sb.toString();
}
use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.
the class DefaultErrorAttributesTests method withBindingErrors.
@Test
void withBindingErrors() {
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
bindingResult.addError(new ObjectError("c", "d"));
Exception ex = new BindException(bindingResult);
testBindingResult(bindingResult, ex, ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
}
Aggregations