use of org.springframework.validation.BindingResult in project spring-framework by spring-projects.
the class ModelResultMatchers method attributeHasFieldErrors.
/**
* Assert the given model attribute field(s) have errors.
*/
public ResultMatcher attributeHasFieldErrors(final String name, final String... fieldNames) {
return new ResultMatcher() {
@Override
public void match(MvcResult mvcResult) throws Exception {
ModelAndView mav = getModelAndView(mvcResult);
BindingResult result = getBindingResult(mav, name);
assertTrue("No errors for attribute: [" + name + "]", result.hasErrors());
for (final String fieldName : fieldNames) {
boolean hasFieldErrors = result.hasFieldErrors(fieldName);
assertTrue("No errors for field: [" + fieldName + "] of attribute [" + name + "]", hasFieldErrors);
}
}
};
}
use of org.springframework.validation.BindingResult in project spring-framework by spring-projects.
the class BindingAwareModelMap method removeBindingResultIfNecessary.
private void removeBindingResultIfNecessary(Object key, Object value) {
if (key instanceof String) {
String attributeName = (String) key;
if (!attributeName.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
String bindingResultKey = BindingResult.MODEL_KEY_PREFIX + attributeName;
BindingResult bindingResult = (BindingResult) get(bindingResultKey);
if (bindingResult != null && bindingResult.getTarget() != value) {
remove(bindingResultKey);
}
}
}
}
use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.
the class DefaultErrorAttributesTests method extractMethodArgumentNotValidExceptionBindingResultErrors.
@Test
public void extractMethodArgumentNotValidExceptionBindingResultErrors() throws Exception {
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
bindingResult.addError(new ObjectError("c", "d"));
Exception ex = new MethodArgumentNotValidException(null, bindingResult);
testBindingResult(bindingResult, ex);
}
use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.
the class DefaultErrorAttributesTests method extractBindingResultErrors.
@Test
public void extractBindingResultErrors() throws Exception {
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
bindingResult.addError(new ObjectError("c", "d"));
Exception ex = new BindException(bindingResult);
testBindingResult(bindingResult, ex);
}
use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.
the class RelaxedDataBinderTests method testBindErrorNotWritableWithPrefix.
@Test
public void testBindErrorNotWritableWithPrefix() throws Exception {
VanillaTarget target = new VanillaTarget();
BindingResult result = bind(target, "spam: bar\n" + "vanilla.value: 123", "vanilla");
assertThat(result.getErrorCount()).isEqualTo(0);
assertThat(target.getValue()).isEqualTo(123);
}
Aggregations