Example usage for org.springframework.validation FieldError getCode

List of usage examples for org.springframework.validation FieldError getCode

Introduction

In this page you can find the example usage for org.springframework.validation FieldError getCode.

Prototype

@Nullable
public String getCode() 

Source Link

Document

Return the default code of this resolvable, that is, the last one in the codes array.

Usage

From source file:org.zht.framework.validate.ValidateHandler.java

public static String getDefaultError(BindingResult result) {
    if (result.hasErrors()) {
        List<FieldError> fieldEist = result.getFieldErrors();
        if (fieldEist != null && fieldEist.size() > 0) {
            FieldError fieldError = fieldEist.get(0);
            fieldError.getCode();
            String prefix = "\r\n" + "???";
            String value = "{" + (fieldError.getRejectedValue() == null ? " " : fieldError.getRejectedValue())
                    + "}";
            String message = "" + fieldError.getDefaultMessage();
            return prefix + "" + value + "\r\n" + message;
        } else {//from   ww w .  j a va2  s  . c  o  m
            List<ObjectError> allErrorlist = result.getAllErrors();
            ObjectError oe = allErrorlist.get(0);
            return "" + oe.getDefaultMessage();
        }
    } else {
        return null;
    }
}

From source file:it.f2informatica.core.validator.utils.ErrorMessageResolver.java

public List<ErrorMessage> resolveErrorCodes(List<FieldError> fieldErrors, final Locale locale) {
    return Lists.newArrayList(Iterables.transform(fieldErrors, new Function<FieldError, ErrorMessage>() {
        @Override//from  ww  w  .jav  a 2 s.c o  m
        public ErrorMessage apply(FieldError input) {
            String errorCode = input.getCode();
            String errorMessage = messageSource.getMessage(errorCode, input.getArguments(), locale);
            String field = Iterables.getFirst(Arrays.asList(input.getField().split("\\.")), "");
            return new ErrorMessage(field, errorCode, errorMessage);
        }
    }));
}

From source file:org.terasoluna.tourreservation.app.searchtour.SearchTourFormDateValidatorTest.java

/**
 * Date parse Error/*from www  .  j  a va2 s.c om*/
 */
@Test
public void testValidate02() {
    SearchTourFormDateValidator validator = new SearchTourFormDateValidator();
    SearchTourForm criteria = new SearchTourForm();
    BindingResult result = new DirectFieldBindingResult(criteria, "SearchTourcriteria");
    criteria.setDepDay(31);
    criteria.setDepMonth(2);
    criteria.setDepYear(2000);

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

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

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

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

From source file:com.mitchellbosecke.pebble.spring.extension.function.bindingresult.GetFieldErrorsFunction.java

@Override
public Object execute(Map<String, Object> args) {
    List<String> results = new ArrayList<>();
    String formName = (String) args.get(PARAM_FORM_NAME);
    String field = (String) args.get(PARAM_FIELD_NAME);

    if (field == null) {
        throw new IllegalArgumentException("Field parameter is required in GetFieldErrorsFunction");
    }/*from w w  w . j a  v  a2 s.c o m*/

    EvaluationContext context = (EvaluationContext) args.get("_context");
    Locale locale = context.getLocale();
    BindingResult bindingResult = this.getBindingResult(formName, context);

    if (bindingResult != null) {
        for (FieldError error : bindingResult.getFieldErrors(field)) {
            String msg = this.messageSource.getMessage(error.getCode(), error.getArguments(),
                    error.getDefaultMessage(), locale);
            if (msg != null) {
                results.add(msg);
            }
        }
    }
    return results;
}

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

/**
 * Date parse Error//from w ww. j  a  v a 2 s  .co  m
 */
@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//from   w  ww.j  a  v  a 2s.co 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:org.iwethey.forums.web.user.test.NewUserValidatorTest.java

public void testValidateMissingCheck() {
    User u = new User();
    BindException errors = new BindException(u, "userInfo");
    u.setNickname("spork");
    u.setUnencryptedPassword("blancmange");
    u.setPasswordCheck("");

    mVal.validate(u, errors);//from  ww  w . jav  a  2 s.c om
    assertEquals("nickname", 0, errors.getFieldErrorCount("nickname"));
    assertEquals("password", 0, errors.getFieldErrorCount("password"));
    assertEquals("passwordCheck", 1, errors.getFieldErrorCount("passwordCheck"));

    FieldError err = errors.getFieldError("passwordCheck");
    assertEquals("null passwordCheck code", "error.password.empty", err.getCode());
}

From source file:org.jasig.schedassist.web.owner.schedule.ClearAvailableScheduleFormBackingObjectValidatorTest.java

/**
 * Test invalid weekOf value, assert 1 error.
 * /*from w  w  w. ja  v  a2 s  . c o m*/
 * @throws Exception
 */
@Test
@SuppressWarnings("rawtypes")
public void testInvalidWeekOf() throws Exception {
    ClearAvailableScheduleFormBackingObject fbo = new ClearAvailableScheduleFormBackingObject();
    fbo.setConfirmedCancelWeek(true);
    fbo.setWeekOfPhrase("abcdde");
    Errors errors = new BindException(fbo, "command");
    ClearAvailableScheduleFormBackingObjectValidator validator = new ClearAvailableScheduleFormBackingObjectValidator();
    validator.validate(fbo, errors);
    List fieldErrors = errors.getAllErrors();
    Assert.assertEquals(1, fieldErrors.size());
    FieldError weekOfError = errors.getFieldError("weekOfPhrase");
    Assert.assertNotNull(weekOfError);
    Assert.assertEquals("field.weekofphraseformat", weekOfError.getCode());
}

From source file:org.iwethey.forums.web.user.test.NewUserValidatorTest.java

public void testValidateDiffers() {
    User u = new User();
    BindException errors = new BindException(u, "userInfo");
    u.setNickname("spork");
    u.setUnencryptedPassword("blancmange");
    u.setPasswordCheck("sassy");

    mVal.validate(u, errors);/*from   ww w  . j av a 2s  .c  om*/
    assertEquals("nickname", 0, errors.getFieldErrorCount("nickname"));
    assertEquals("password", 0, errors.getFieldErrorCount("password"));
    assertEquals("passwordCheck", 1, errors.getFieldErrorCount("passwordCheck"));

    FieldError err = errors.getFieldError("passwordCheck");
    assertEquals("differing passwordCheck code", "error.password.differs", err.getCode());
}

From source file:org.iwethey.forums.web.user.test.LoginValidatorTest.java

public void testValidateNull() {
    User u = new User();
    BindException errors = new BindException(u, "nickname");

    mVal.validate(null, errors);/*  w w  w  .  j  av a 2  s  .  co m*/
    assertEquals("null 1", 1, errors.getFieldErrorCount("nickname"));
    assertEquals("null 2", 1, errors.getFieldErrorCount("unencryptedPassword"));
    FieldError err = errors.getFieldError("nickname");
    assertEquals("null code 1", "error.nickname.empty", err.getCode());
    err = errors.getFieldError("unencryptedPassword");
    assertEquals("null code 2", "error.password.empty", err.getCode());
}