Example usage for org.springframework.validation BindingResult getFieldError

List of usage examples for org.springframework.validation BindingResult getFieldError

Introduction

In this page you can find the example usage for org.springframework.validation BindingResult getFieldError.

Prototype

@Nullable
FieldError getFieldError(String field);

Source Link

Document

Get the first error associated with the given field, if any.

Usage

From source file:com.bh.subject.std.common.error.BindResultExceptionRaiser.java

public static void raiseBindingResultException(List<String> fieldNames, BindingResult bindingResult) {
    for (String errorField : fieldNames) {
        if (bindingResult.hasFieldErrors(errorField)) {
            throw new GMSException(bindingResult.getFieldError(errorField));
        }//from w ww .  jav a 2s.  c  o  m
    }
}

From source file:org.fede.calculator.web.controller.LaPlataAddressCalculator.java

@RequestMapping(value = "/address", method = RequestMethod.POST)
public LaPlataAddressDTO computeAddress(@RequestBody @Valid LaPlataAddressDTO address, BindingResult result) {

    if (result.hasErrors()) {
        return new LaPlataAddressDTO(result.getFieldError("originalText").getDefaultMessage(),
                address.getOriginalText());
    }/*w w w. jav  a  2  s  . co m*/
    LaPlataAddressDTO answer = this.addressService.getAddress(address);
    if (!answer.isValid()) {
        return new LaPlataAddressDTO("La direccin ingresada no es vlida.", address.getOriginalText());
    }
    return answer;
}

From source file:com.expedia.common.controller.WeatherControllerTest.java

@Test
public void testPost() throws Exception {
    request.setMethod("POST");
    request.setRequestURI("/weather/retrieve");
    request.setParameter("zipCode", "23451");
    final ModelAndView mav = handle(request, response);
    final Weather weather = assertAndReturnModelAttributeOfType(mav, "weather", Weather.class);
    assertEquals(new Integer(23451), weather.getZipCode());
    /* if myForm is not valid */
    assertViewName(mav, "WeatherForm");
    final BindingResult errors = getBindingResult(mav, "weather");
    assertTrue(//from  ww w .  j  a v a2 s  . co m
            null != errors.getFieldError("zipCode") ? errors.getFieldError("zipCode").getDefaultMessage() : "",
            !errors.hasErrors());
}

From source file:com.expedia.controller.WeatherControllerTest.java

@Test
public void testPost() throws Exception {
    request.setMethod("POST");
    request.setRequestURI("/weather/retrieve/xml");
    request.setParameter("zipCode", "23451");
    final ModelAndView mav = handle(request, response);
    final Weather weather = assertAndReturnModelAttributeOfType(mav, "weather", Weather.class);
    assertEquals(new Integer(23451), weather.getZipCode());
    /* if myForm is not valid */
    assertViewName(mav, "WeatherForm");
    final BindingResult errors = getBindingResult(mav, "weather");
    assertTrue(/*from   w  w w .j  a va 2s.  c o m*/
            null != errors.getFieldError("zipCode") ? errors.getFieldError("zipCode").getDefaultMessage() : "",
            !errors.hasErrors());
}

From source file:com.tapas.evidence.fe.controller.UserController.java

private void validateCaptcha(String captcha, BindingResult result, String sessionId, String errorCode) {
    // If the captcha field is already rejected
    if (null != result.getFieldError("captcha")) {
        return;//from   w  w  w. j  a  v a 2s .  c om
    }

    boolean validCaptcha = false;
    try {
        validCaptcha = captchaService.validateResponseForID(sessionId, captcha);
    } catch (CaptchaServiceException e) {
        // should not happen, may be thrown if the id is not valid
        log.warn("Catcha validation problem:", e);
    }
    if (!validCaptcha) {
        result.rejectValue("captcha", errorCode);
    }
}

From source file:org.terasoluna.tourreservation.app.managecustomer.CustomerBirthdayValidatorTest.java

/**
 * Date parse Error//from w  ww.j av  a 2  s .c  om
 */
@Test
public void testValidate02() {
    CustomerBirthdayValidator validator = new CustomerBirthdayValidator();

    CustomerForm customer = new CustomerForm();
    customer.setCustomerBirthYear(2011);
    customer.setCustomerBirthMonth(02);
    customer.setCustomerBirthDay(29);

    BindingResult result = new DirectFieldBindingResult(customer, "CustomerForm");

    // run
    validator.validate(customer, result);

    // assert
    assertThat(result.hasErrors(), is(true));

    FieldError error = result.getFieldError("customerBirthYear");

    if (error != null) {
        assertThat(error.getCode(), is("IncorrectDate.customerBirth"));
        assertThat(error.getDefaultMessage(), is("Incorrect date was entered."));

    } else {
        fail("error");
    }
}

From source file:org.terasoluna.tourreservation.app.managecustomer.CustomerPassEqualsValidatorTest.java

/**
 * password check error/*  w  ww . ja  v  a  2s.  c o  m*/
 */
@Test
public void testValidate02() {
    CustomerPassEqualsValidator validator = new CustomerPassEqualsValidator();
    CustomerForm customer = new CustomerForm();
    customer.setCustomerPass("12345");
    customer.setCustomerPassConfirm("1234");

    BindingResult result = new DirectFieldBindingResult(customer, "CustomerForm");

    // run
    validator.validate(customer, result);

    // assert
    assertThat(result.hasErrors(), is(true));

    FieldError error = result.getFieldError("customerPass");

    if (error != null) {
        assertThat(error.getCode(), is("NotEquals.customerPass"));
        assertThat(error.getDefaultMessage(), is("Password and password confirm is not same."));
    } else {
        fail("error");
    }

}

From source file:com.ccserver.digital.validator.CreditCardApplicationValidatorTest.java

@Test
public void validatorGeneralTest() {

    CreditCardApplicationDTO creditCardApplicationDTO = new CreditCardApplicationDTO();
    creditCardApplicationDTO.setProcessStep(ProcessStep.General);
    LocalDateTime ldt = LocalDateTime.now();
    creditCardApplicationDTO.setDateOfIssue(ldt);
    BindingResult errors = new BeanPropertyBindingResult(creditCardApplicationDTO, "target");
    creditCardApplicationValidator.validate(creditCardApplicationDTO, errors);
    Assert.assertNull(errors.getFieldError("dateOfIssue"));
}

From source file:com.ccserver.digital.validator.CreditCardApplicationValidatorTest.java

@Test
public void validatorBasicInformationTest() {
    // Arrange//from  w w  w  .ja va 2s . com
    CreditCardApplicationDTO ccApp = new CreditCardApplicationDTO();
    ccApp.setFirstName(null);
    ccApp.setLastName(null);
    ccApp.setEmail(null);
    ccApp.setMobile(null);
    BindingResult errors = new BeanPropertyBindingResult(ccApp, "target");
    // Act
    creditCardApplicationValidator.validate(ccApp, errors);
    // Assert
    Assert.assertNotNull(errors.getFieldError("firstName"));
    Assert.assertNotNull(errors.getFieldError("email"));
    Assert.assertNotNull(errors.getFieldError("mobile"));
}

From source file:com.ccserver.digital.validator.CreditCardApplicationValidatorTest.java

@Test
public void validatorPersonalTest() {

    CreditCardApplicationDTO creditCardApplicationDTO = new CreditCardApplicationDTO();
    creditCardApplicationDTO.setProcessStep(ProcessStep.Personal);
    creditCardApplicationDTO.setEducation(null);
    creditCardApplicationDTO.setMaritalStatus(null);
    creditCardApplicationDTO.setNumberOfChildren(5);

    BindingResult errors = new BeanPropertyBindingResult(creditCardApplicationDTO, "target");
    creditCardApplicationValidator.validate(creditCardApplicationDTO, errors);
    Assert.assertNotNull(errors.getFieldError("education"));
    Assert.assertNotNull(errors.getFieldError("maritalStatus"));
    Assert.assertNull(errors.getFieldError("numberOfChildren"));
}