Search in sources :

Example 6 with BindingResult

use of cn.taketoday.validation.BindingResult in project today-infrastructure by TAKETODAY.

the class ErrorResponseExceptionTests method methodArgumentNotValidException.

@Test
void methodArgumentNotValidException() {
    BindingResult bindingResult = new BindException(new Object(), "object");
    bindingResult.addError(new FieldError("object", "field", "message"));
    ErrorResponse ex = new MethodArgumentNotValidException(this.methodParameter, bindingResult);
    assertStatus(ex, HttpStatus.BAD_REQUEST);
    assertDetail(ex, "Invalid request content.");
    assertThat(ex.getHeaders()).isEmpty();
}
Also used : BindingResult(cn.taketoday.validation.BindingResult) BindException(cn.taketoday.validation.BindException) FieldError(cn.taketoday.validation.FieldError) MethodArgumentNotValidException(cn.taketoday.web.bind.MethodArgumentNotValidException) Test(org.junit.jupiter.api.Test)

Example 7 with BindingResult

use of cn.taketoday.validation.BindingResult in project today-infrastructure 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 8 with BindingResult

use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.

the class DataBinderTests method testBindingWithErrorsAndCustomEditors.

@Test
void testBindingWithErrorsAndCustomEditors() {
    TestBean rod = new TestBean();
    DataBinder binder = new DataBinder(rod, "person");
    binder.registerCustomEditor(String.class, "touchy", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("prefix_" + text);
        }

        @Override
        public String getAsText() {
            return getValue().toString().substring(7);
        }
    });
    binder.registerCustomEditor(TestBean.class, "spouse", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean(text, 0));
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    PropertyValues pvs = new PropertyValues();
    pvs.add("name", "Rod");
    pvs.add("age", "32x");
    pvs.add("touchy", "m.y");
    pvs.add("spouse", "Kerry");
    binder.bind(pvs);
    assertThatExceptionOfType(BindException.class).isThrownBy(binder::close).satisfies(ex -> {
        assertThat(rod.getName()).isEqualTo("Rod");
        Map<?, ?> model = binder.getBindingResult().getModel();
        TestBean tb = (TestBean) model.get("person");
        assertThat(tb).isEqualTo(rod);
        BindingResult br = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "person");
        assertThat(binder.getBindingResult()).isSameAs(br);
        assertThat(br.hasErrors()).isTrue();
        assertThat(br.getErrorCount()).isEqualTo(2);
        assertThat(br.hasFieldErrors("age")).isTrue();
        assertThat(br.getFieldErrorCount("age")).isEqualTo(1);
        assertThat(binder.getBindingResult().getFieldValue("age")).isEqualTo("32x");
        FieldError ageError = binder.getBindingResult().getFieldError("age");
        assertThat(ageError).isNotNull();
        assertThat(ageError.getCode()).isEqualTo("typeMismatch");
        assertThat(ageError.getRejectedValue()).isEqualTo("32x");
        assertThat(tb.getAge()).isEqualTo(0);
        assertThat(br.hasFieldErrors("touchy")).isTrue();
        assertThat(br.getFieldErrorCount("touchy")).isEqualTo(1);
        assertThat(binder.getBindingResult().getFieldValue("touchy")).isEqualTo("m.y");
        FieldError touchyError = binder.getBindingResult().getFieldError("touchy");
        assertThat(touchyError).isNotNull();
        assertThat(touchyError.getCode()).isEqualTo("methodInvocation");
        assertThat(touchyError.getRejectedValue()).isEqualTo("m.y");
        assertThat(tb.getTouchy()).isNull();
        assertThat(br.hasFieldErrors("spouse")).isFalse();
        assertThat(binder.getBindingResult().getFieldValue("spouse")).isEqualTo("Kerry");
        assertThat(tb.getSpouse()).isNotNull();
    });
}
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) FieldError(cn.taketoday.validation.FieldError) DataBinder(cn.taketoday.validation.DataBinder) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 9 with BindingResult

use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.

the class DataBinderTests method testBindingWithRequiredMapFields.

@Test
void testBindingWithRequiredMapFields() {
    TestBean tb = new TestBean();
    tb.setSpouse(new TestBean());
    DataBinder binder = new DataBinder(tb, "person");
    binder.setRequiredFields("someMap[key1]", "someMap[key2]", "someMap['key3']", "someMap[key4]");
    PropertyValues pvs = new PropertyValues();
    pvs.add("someMap[key1]", "value1");
    pvs.add("someMap['key2']", "value2");
    pvs.add("someMap[key3]", "value3");
    binder.bind(pvs);
    BindingResult br = binder.getBindingResult();
    assertThat(br.getErrorCount()).as("Wrong number of errors").isEqualTo(1);
    assertThat(br.getFieldError("someMap[key4]").getCode()).isEqualTo("required");
}
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 10 with BindingResult

use of cn.taketoday.validation.BindingResult in project today-framework by TAKETODAY.

the class DataBinderTests method testBindingNoErrors.

@Test
void testBindingNoErrors() throws BindException {
    TestBean rod = new TestBean();
    DataBinder binder = new DataBinder(rod, "person");
    assertThat(binder.isIgnoreUnknownFields()).isTrue();
    PropertyValues pvs = new PropertyValues();
    pvs.add("name", "Rod");
    pvs.add("age", "032");
    pvs.add("nonExisting", "someValue");
    binder.bind(pvs);
    binder.close();
    assertThat(rod.getName().equals("Rod")).as("changed name correctly").isTrue();
    assertThat(rod.getAge() == 32).as("changed age correctly").isTrue();
    Map<?, ?> map = binder.getBindingResult().getModel();
    assertThat(map.size() == 2).as("There is one element in map").isTrue();
    TestBean tb = (TestBean) map.get("person");
    assertThat(tb.equals(rod)).as("Same object").isTrue();
    BindingResult other = new BeanPropertyBindingResult(rod, "person");
    assertThat(binder.getBindingResult()).isEqualTo(other);
    assertThat(other).isEqualTo(binder.getBindingResult());
    BindException ex = new BindException(other);
    assertThat(other).isEqualTo(ex);
    assertThat(ex).isEqualTo(other);
    assertThat(binder.getBindingResult()).isEqualTo(ex);
    assertThat(ex).isEqualTo(binder.getBindingResult());
    other.reject("xxx");
    boolean condition = !other.equals(binder.getBindingResult());
    assertThat(condition).isTrue();
}
Also used : BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) BindingResult(cn.taketoday.validation.BindingResult) PropertyValues(cn.taketoday.beans.PropertyValues) BeanPropertyBindingResult(cn.taketoday.validation.BeanPropertyBindingResult) 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) BindException(cn.taketoday.validation.BindException) DataBinder(cn.taketoday.validation.DataBinder) 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