Example usage for org.springframework.validation Errors hasFieldErrors

List of usage examples for org.springframework.validation Errors hasFieldErrors

Introduction

In this page you can find the example usage for org.springframework.validation Errors hasFieldErrors.

Prototype

boolean hasFieldErrors();

Source Link

Document

Are there any field errors?

Usage

From source file:alfio.util.Validator.java

public static ValidationResult evaluateValidationResult(Errors errors) {
    if (errors.hasFieldErrors()) {
        return ValidationResult.failed(errors.getFieldErrors().stream()
                .map(ValidationResult.ErrorDescriptor::fromFieldError).collect(Collectors.toList()));
    }//www  .j  a v a  2 s.c  o  m
    return ValidationResult.success();
}

From source file:org.ualerts.fixed.web.controller.AbstractFormController.java

private Map<String, List<String>> getFieldErrors(Errors errors) {
    if (!errors.hasFieldErrors())
        return null;

    Map<String, List<String>> fieldErrors = new HashMap<String, List<String>>();
    for (FieldError error : errors.getFieldErrors()) {
        if (!fieldErrors.containsKey(error.getField()))
            fieldErrors.put(error.getField(), new ArrayList<String>());
        fieldErrors.get(error.getField()).add(getMessage(error.getCode()));
    }//from  www . j  av  a  2  s .c  o m
    return fieldErrors;
}

From source file:controller.AddBikeController.java

@RequestMapping(value = "addproduct/bike", method = POST) //de form input wordt getoets aan de Product velden, bij fouten terug naar het formulier
public String processProductAdd(@Valid model.Bike product, Errors errors, RedirectAttributes model) { //RedirectAttributes zijn nodig om de gebruiker ook na redirect te onthouden
    System.out.println(errors.hasErrors());
    System.out.println(errors.hasFieldErrors());
    if (errors.hasErrors() || errors == null) {

        return "addBike";
    } else { //Is de validatie geslaagd van kan het object opgeslagen worden
        dao.saveProduct(product);/*from www.jav  a2 s .  c  o m*/
        model.addAttribute("id", product.getId());
        model.addFlashAttribute("product", product); //maakt tijdelijke sessie aan voor onthouden van product voorbij de redirect
        return "redirect:/addedbike/" + product.getId();
    }
}

From source file:controller.AddCarController.java

@RequestMapping(value = "addproduct/car", method = POST) //de form input wordt getoets aan de Product velden, bij fouten terug naar het formulier
public String processProductAdd(@Valid model.Car product, Errors errors, RedirectAttributes model) { //RedirectAttributes zijn nodig om de gebruiker ook na redirect te onthouden
    System.out.println(errors.hasErrors());
    System.out.println(errors.hasFieldErrors());
    if (errors.hasErrors() || errors == null) {

        return "addCar";
    } else { //Is de validatie geslaagd van kan het object opgeslagen worden
        dao.saveProduct(product);/*from   ww w  .j  av  a  2  s. c  o  m*/
        model.addAttribute("id", product.getId());
        model.addFlashAttribute("product", product); //maakt tijdelijke sessie aan voor onthouden van product voorbij de redirect
        return "redirect:/addedCar/" + product.getId();
    }
}

From source file:org.ualerts.fixed.web.controller.AbstractFormController.java

/**
 * Translate the provided errors into a Map that contains global and field
 * level errors/*w  w  w .  java2s  .co  m*/
 * @param errors The errors to be translated
 * @return A Map containing the following:
 *   - "global" => a List of error messages for global-level errors
 *   - "fields" => a Map of field names (String) to errors (List of Strings)
 */
protected Map<String, Object> getMappedErrors(Errors errors) {
    if (!errors.hasErrors())
        return null;

    Map<String, Object> bindErrors = new HashMap<String, Object>();
    if (errors.hasFieldErrors())
        bindErrors.put("fields", getFieldErrors(errors));
    if (errors.hasGlobalErrors())
        bindErrors.put("global", getGlobalErrors(errors));

    return bindErrors;
}

From source file:org.openmrs.web.controller.patient.ShortPatientFormValidatorTest.java

/**
 * @see ShortPatientFormValidator#validate(Object,Errors)
 *///from   w  w  w  .j  a  v a 2  s .  co  m
@Test
@Verifies(value = "should fail validation if birthdate is blank", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfBirthdateIsBlank() throws Exception {
    Patient p = ps.getPatient(2);
    p.setBirthdate(null);
    ShortPatientModel model = new ShortPatientModel(p);
    Errors errors = new BindException(model, "patientModel");
    validator.validate(model, errors);
    Assert.assertEquals(true, errors.hasFieldErrors());
}

From source file:org.openmrs.web.controller.patient.ShortPatientFormValidatorTest.java

/**
 * @see ShortPatientFormValidator#validate(Object,Errors)
 *//*from   ww w. j av  a  2 s.  c om*/
@Test
@Verifies(value = "should fail validation if gender is blank", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfGenderIsBlank() throws Exception {
    Patient p = ps.getPatient(2);
    p.setGender("");
    ShortPatientModel model = new ShortPatientModel(p);
    Errors errors = new BindException(model, "patientModel");
    validator.validate(model, errors);
    Assert.assertEquals(true, errors.hasFieldErrors());
}

From source file:org.openmrs.web.controller.patient.ShortPatientFormValidatorTest.java

/**
 * @see ShortPatientFormValidator#validate(Object,Errors)
 *///from  w  w w  .  j  a  va 2s  .  c  om
@Test
@Verifies(value = "should fail validation if causeOfDeath is blank when patient is dead", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfCauseOfDeathIsBlankWhenPatientIsDead() throws Exception {
    Patient p = ps.getPatient(2);
    p.setDead(true);
    p.setCauseOfDeath(null);
    p.setDeathDate(Calendar.getInstance().getTime());
    ShortPatientModel model = new ShortPatientModel(p);
    Errors errors = new BindException(model, "patientModel");
    validator.validate(model, errors);
    Assert.assertEquals(true, errors.hasFieldErrors());
}

From source file:org.openmrs.web.controller.patient.ShortPatientFormValidatorTest.java

/**
 * @see ShortPatientFormValidator#validate(Object,Errors)
 *///from   w  w  w .ja va 2 s  .c o  m
@Test
@Verifies(value = "should fail validation if birthdate makes patient 120 years old or older", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfBirthdateMakesPatient120YearsOldOrOlder() throws Exception {
    Patient p = ps.getPatient(2);
    Calendar c = Calendar.getInstance();
    c.roll(Calendar.YEAR, -121);
    p.setBirthdate(c.getTime());
    ShortPatientModel model = new ShortPatientModel(p);
    Errors errors = new BindException(model, "patientModel");
    validator.validate(model, errors);
    Assert.assertEquals(true, errors.hasFieldErrors());
}

From source file:org.openmrs.web.controller.patient.ShortPatientFormValidatorTest.java

/**
 * @see ShortPatientFormValidator#validate(Object,Errors)
 *///from  w  w  w.j a  va  2 s . co m
@Test
@Verifies(value = "should fail validation if birthdate is a future date", method = "validate(Object,Errors)")
public void validate_shouldFailValidationIfBirthdateIsAFutureDate() throws Exception {
    Patient p = ps.getPatient(2);
    Calendar c = Calendar.getInstance();
    // put the time into the future by a minute
    c.add(Calendar.MINUTE, 1);
    p.setBirthdate(c.getTime());
    ShortPatientModel model = new ShortPatientModel(p);
    Errors errors = new BindException(model, "patientModel");
    validator.validate(model, errors);
    Assert.assertEquals(true, errors.hasFieldErrors());
}