Search in sources :

Example 51 with BindingResult

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

Example 52 with BindingResult

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");
}
Also used : BindingResult(org.springframework.validation.BindingResult) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 53 with BindingResult

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");
}
Also used : BindingResult(org.springframework.validation.BindingResult) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 54 with BindingResult

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");
}
Also used : BindingResult(org.springframework.validation.BindingResult) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 55 with BindingResult

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();
        }
    }
}
Also used : FileUploadException(uk.ac.ebi.spot.goci.curation.exception.FileUploadException) java.util(java.util) SnpAssociationTableView(uk.ac.ebi.spot.goci.curation.model.SnpAssociationTableView) LoggerFactory(org.slf4j.LoggerFactory) SnpAssociationInteractionForm(uk.ac.ebi.spot.goci.curation.model.SnpAssociationInteractionForm) Autowired(org.springframework.beans.factory.annotation.Autowired) SimpleDateFormat(java.text.SimpleDateFormat) BindingResult(org.springframework.validation.BindingResult) Controller(org.springframework.stereotype.Controller) SnpAssociationStandardMultiForm(uk.ac.ebi.spot.goci.curation.model.SnpAssociationStandardMultiForm) EfoTraitRepository(uk.ac.ebi.spot.goci.repository.EfoTraitRepository) Value(org.springframework.beans.factory.annotation.Value) Valid(javax.validation.Valid) Model(org.springframework.ui.Model) uk.ac.ebi.spot.goci.curation.service(uk.ac.ebi.spot.goci.curation.service) HttpServletRequest(javax.servlet.http.HttpServletRequest) uk.ac.ebi.spot.goci.model(uk.ac.ebi.spot.goci.model) Qualifier(org.springframework.beans.factory.annotation.Qualifier) StudyRepository(uk.ac.ebi.spot.goci.repository.StudyRepository) Sort(org.springframework.data.domain.Sort) EnsemblRestTemplateService(uk.ac.ebi.spot.goci.service.EnsemblRestTemplateService) DateFormat(java.text.DateFormat) RedirectAttributes(org.springframework.web.servlet.mvc.support.RedirectAttributes) DataIntegrityException(uk.ac.ebi.spot.goci.curation.exception.DataIntegrityException) Logger(org.slf4j.Logger) AssociationRepository(uk.ac.ebi.spot.goci.repository.AssociationRepository) MediaType(org.springframework.http.MediaType) HttpServletResponse(javax.servlet.http.HttpServletResponse) SnpAssociationForm(uk.ac.ebi.spot.goci.curation.model.SnpAssociationForm) EnsemblMappingException(uk.ac.ebi.spot.goci.exception.EnsemblMappingException) SheetProcessingException(uk.ac.ebi.spot.goci.exception.SheetProcessingException) IOException(java.io.IOException) SnpFormColumn(uk.ac.ebi.spot.goci.curation.model.SnpFormColumn) AssociationUploadErrorView(uk.ac.ebi.spot.goci.curation.model.AssociationUploadErrorView) FileNotFoundException(java.io.FileNotFoundException) MapCatalogService(uk.ac.ebi.spot.goci.service.MapCatalogService) GetRequest(com.mashape.unirest.request.GetRequest) SnpFormRow(uk.ac.ebi.spot.goci.curation.model.SnpFormRow) WebDataBinder(org.springframework.web.bind.WebDataBinder) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) LastViewedAssociation(uk.ac.ebi.spot.goci.curation.model.LastViewedAssociation) MultipartFile(org.springframework.web.multipart.MultipartFile) AssociationValidationView(uk.ac.ebi.spot.goci.curation.model.AssociationValidationView) MappingDetails(uk.ac.ebi.spot.goci.curation.model.MappingDetails) LastViewedAssociation(uk.ac.ebi.spot.goci.curation.model.LastViewedAssociation) AssociationValidationView(uk.ac.ebi.spot.goci.curation.model.AssociationValidationView) EnsemblMappingException(uk.ac.ebi.spot.goci.exception.EnsemblMappingException)

Aggregations

BindingResult (org.springframework.validation.BindingResult)59 Test (org.junit.Test)29 Model (org.springframework.ui.Model)12 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)11 LinkedHashMap (java.util.LinkedHashMap)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ObjectError (org.springframework.validation.ObjectError)6 CreateUserCommand (org.asqatasun.webapp.command.CreateUserCommand)5 CreateUserFormValidator (org.asqatasun.webapp.validator.CreateUserFormValidator)5 ScriptableObject (org.mozilla.javascript.ScriptableObject)5 MvcResult (org.springframework.test.web.servlet.MvcResult)5 ResultMatcher (org.springframework.test.web.servlet.ResultMatcher)5 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)5 RedirectAttributes (org.springframework.web.servlet.mvc.support.RedirectAttributes)5 SimpleDateFormat (java.text.SimpleDateFormat)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 Valid (javax.validation.Valid)4 Controller (org.springframework.stereotype.Controller)4