use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.
the class RelaxedDataBinderTests method testDisallowedFields.
@Test
public void testDisallowedFields() throws Exception {
VanillaTarget target = new VanillaTarget();
RelaxedDataBinder binder = getBinder(target, null);
// Disallowed fields are not unknown...
binder.setDisallowedFields("foo", "bar");
binder.setIgnoreUnknownFields(false);
BindingResult result = bind(binder, target, "foo: bar\n" + "value: 123\n" + "bar: spam");
assertThat(target.getValue()).isEqualTo(123);
assertThat(target.getFoo()).isNull();
assertThat(result.getErrorCount()).isEqualTo(0);
}
use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.
the class RelaxedDataBinderTests method testBindMapWithClashInProperties.
@Test
public void testBindMapWithClashInProperties() throws Exception {
Map<String, Object> target = new LinkedHashMap<>();
BindingResult result = bind(target, "vanilla.spam: bar\n" + "vanilla.spam.value: 123", "vanilla");
assertThat(result.getErrorCount()).isEqualTo(0);
assertThat(target).hasSize(2);
assertThat(target.get("spam")).isEqualTo("bar");
assertThat(target.get("spam.value")).isEqualTo("123");
}
use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.
the class RelaxedDataBinderTests method testBindMapNestedMap.
@Test
public void testBindMapNestedMap() throws Exception {
Map<String, Object> target = new LinkedHashMap<>();
BindingResult result = bind(target, "spam: bar\n" + "vanilla.foo.value: 123", "vanilla");
assertThat(result.getErrorCount()).isEqualTo(0);
@SuppressWarnings("unchecked") Map<String, Object> map = (Map<String, Object>) target.get("foo");
assertThat(map.get("value")).isEqualTo("123");
}
use of org.springframework.validation.BindingResult in project spring-boot by spring-projects.
the class RelaxedDataBinderTests method testBindShallowMap.
@Test
public void testBindShallowMap() throws Exception {
Map<String, Object> target = new LinkedHashMap<>();
BindingResult result = bind(target, "vanilla.spam: bar\n" + "vanilla.value: 123", "vanilla");
assertThat(result.getErrorCount()).isEqualTo(0);
assertThat(target.get("value")).isEqualTo("123");
}
use of org.springframework.validation.BindingResult in project goci by EBISPOT.
the class AssociationController method addStandardSnps.
// Add new standard association/snp information to a study
@RequestMapping(value = "/studies/{studyId}/associations/add_standard", produces = MediaType.TEXT_HTML_VALUE, method = RequestMethod.POST)
public String addStandardSnps(@ModelAttribute("form") @Valid SnpAssociationStandardMultiForm snpAssociationStandardMultiForm, BindingResult bindingResult, @PathVariable Long studyId, Model model, @RequestParam(required = true) String measurementType, HttpServletRequest request) throws EnsemblMappingException {
Study study = studyRepository.findOne(studyId);
model.addAttribute("study", study);
model.addAttribute("measurementType", measurementType);
// Binding vs Validator issue. File: messages.properties
if (bindingResult.hasErrors()) {
model.addAttribute("form", snpAssociationStandardMultiForm);
return "add_standard_snp_association";
}
// Check for errors in form that would prevent saving an association
List<AssociationValidationView> rowErrors = associationOperationsService.checkSnpAssociationFormErrors(snpAssociationStandardMultiForm, measurementType);
if (!rowErrors.isEmpty()) {
model.addAttribute("errors", rowErrors);
model.addAttribute("form", snpAssociationStandardMultiForm);
model.addAttribute("criticalErrorsFound", true);
return "add_standard_snp_association";
} else {
// Create an association object from details in returned form
Association newAssociation = singleSnpMultiSnpAssociationService.createAssociation(snpAssociationStandardMultiForm);
// Save and validate form
String eRelease = ensemblRestTemplateService.getRelease();
Collection<AssociationValidationView> errors = null;
try {
errors = associationOperationsService.saveAssociationCreatedFromForm(study, newAssociation, currentUserDetailsService.getUserFromRequest(request), eRelease);
} catch (EnsemblMappingException e) {
return "ensembl_mapping_failure";
}
// Determine if we have any errors rather than warnings
long errorCount = errors.stream().filter(validationError -> !validationError.getWarning()).count();
if (errorCount > 0) {
model.addAttribute("errors", errors);
model.addAttribute("form", snpAssociationStandardMultiForm);
model.addAttribute("criticalErrorsFound", true);
return "add_standard_snp_association";
} else {
return "redirect:/associations/" + newAssociation.getId();
}
}
}
Aggregations