Search in sources :

Example 11 with BindingResult

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("");
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) BindingResult(cn.taketoday.validation.BindingResult) PropertyValues(cn.taketoday.beans.PropertyValues) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DataBinder(cn.taketoday.validation.DataBinder) Test(org.junit.jupiter.api.Test)

Example 12 with BindingResult

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();
    });
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) Test(org.junit.jupiter.api.Test)

Example 13 with BindingResult

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());
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) MapBindingResult(cn.taketoday.validation.MapBindingResult) ObjectError(cn.taketoday.validation.ObjectError) BindException(cn.taketoday.validation.BindException) MapBindingResult(cn.taketoday.validation.MapBindingResult) ServletException(jakarta.servlet.ServletException) BindException(cn.taketoday.validation.BindException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 14 with BindingResult

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();
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) ObjectError(cn.taketoday.validation.ObjectError)

Example 15 with BindingResult

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));
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) MapBindingResult(cn.taketoday.validation.MapBindingResult) ObjectError(cn.taketoday.validation.ObjectError) BindException(cn.taketoday.validation.BindException) MapBindingResult(cn.taketoday.validation.MapBindingResult) ServletException(jakarta.servlet.ServletException) BindException(cn.taketoday.validation.BindException) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Aggregations

BindingResult (cn.taketoday.validation.BindingResult)22 Test (org.junit.jupiter.api.Test)20 PropertyValues (cn.taketoday.beans.PropertyValues)11 BindException (cn.taketoday.validation.BindException)9 FieldError (cn.taketoday.validation.FieldError)8 ObjectError (cn.taketoday.validation.ObjectError)8 MethodArgumentNotValidException (cn.taketoday.web.bind.MethodArgumentNotValidException)8 MapBindingResult (cn.taketoday.validation.MapBindingResult)6 ServletException (jakarta.servlet.ServletException)6 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)5 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)5 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)5 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)5 BeanPropertyBindingResult (cn.taketoday.validation.BeanPropertyBindingResult)5 DataBinder (cn.taketoday.validation.DataBinder)5 TypeMismatchException (cn.taketoday.beans.TypeMismatchException)2 MethodParameter (cn.taketoday.core.MethodParameter)2 ConversionFailedException (cn.taketoday.core.conversion.ConversionFailedException)2 Method (java.lang.reflect.Method)2