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:com.hadoopvietnam.controller.member.ChangePasswordController.java

@PreAuthorize("isAuthenticated()")
@RequestMapping(value = { "/json" }, method = { org.springframework.web.bind.annotation.RequestMethod.POST })
@ResponseBody//from  w w  w. ja  v a2 s. co  m
public ValidationResponse changePasswordAjaxJson(Model model,
        @ModelAttribute("changePasswordForm") ChangePasswordForm form, HttpServletRequest request,
        BindingResult bindingResult) {
    ValidationResponse res = new ValidationResponse();
    this.validator.validate(form, bindingResult);
    if (!bindingResult.hasErrors()) {
        res.setStatus("SUCCESS");
    } else {
        res.setStatus("FAIL");
        List<FieldError> allErrors = bindingResult.getFieldErrors();
        List<ErrorMessage> errorMesages = new ArrayList<ErrorMessage>();
        for (FieldError objectError : allErrors) {
            errorMesages.add(new ErrorMessage(objectError.getField(),
                    this.resourceMessage.getMessage(objectError.getCode(), request)));
        }
        res.setErrorMessageList(errorMesages);
    }
    return res;
}

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

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

    mVal.validate(u, errors);//  w ww.  ja v a2 s  . c o m
    assertEquals("null nickname 1", 1, errors.getFieldErrorCount("nickname"));
    assertEquals("null nickname 2", 1, errors.getFieldErrorCount("unencryptedPassword"));
    FieldError err = errors.getFieldError("nickname");
    assertEquals("null nickname code", "error.nickname.empty", err.getCode());
    err = errors.getFieldError("unencryptedPassword");
    assertEquals("null password code", "error.password.empty", err.getCode());
}

From source file:by.creepid.docgeneration.validation.RegValidator.java

@Override
public void validate(FacesContext context, UIComponent component, Object value) {

    Locale locale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();

    WebUtils.clearFacesMessages(context);
    FirmReg firmReg = (FirmReg) WebUtils.findBean("reg");

    System.out.println(firmReg.toString());

    Errors errors = new BeanPropertyBindingResult(firmReg, "reg");
    firmRegValidator.validate(firmReg, errors);

    if (errors != null && errors.hasErrors()) {
        String message = messageSource.getMessage("error.validation", null, locale);

        context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR, message, null));

        List<FieldError> fieldErrors = errors.getFieldErrors();
        for (FieldError fieldError : fieldErrors) {
            message = messageSource.getMessage(fieldError, locale);
            System.out.println(//from   w  ww .ja  va2  s .  com
                    fieldError.getField() + "   " + fieldError.getCode() + " " + fieldError.getObjectName());

            context.addMessage(fieldError.getField(),
                    new FacesMessage(FacesMessage.SEVERITY_ERROR, message, message));
        }
    }
}

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

public void testValidateEmptyNickname() {
    User u = new User();
    BindException errors = new BindException(u, "nickname");
    u.setNickname("");

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

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

public void testValidateShortNickname() {
    User u = new User();
    BindException errors = new BindException(u, "nickname");
    u.setNickname("a");

    mVal.validate(u, errors);//  www  .  j a  va  2 s.  c  o  m
    assertEquals("short nickname 1", 1, errors.getFieldErrorCount("nickname"));
    assertEquals("short nickname 2", 1, errors.getFieldErrorCount("unencryptedPassword"));
    FieldError err = errors.getFieldError("nickname");
    assertEquals("short nickname code", "error.nickname.length", err.getCode());
    err = errors.getFieldError("unencryptedPassword");
    assertEquals("null password code", "error.password.empty", err.getCode());
}

From source file:org.openmrs.web.controller.program.PatientProgramFormController.java

private String validateWithErrorCodes(Object obj) {
    Errors errors = new BindException(obj, "");
    Context.getAdministrationService().validate(obj, errors);
    if (errors.hasErrors()) {
        StringBuilder message = new StringBuilder();
        for (FieldError error : errors.getFieldErrors()) {
            message.append(Context.getMessageSourceService().getMessage(error.getCode())).append("<br />");
        }//from  ww w . j a  v  a2 s  . c om
        return message.toString();
    }
    return null;
}

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

public void testValidateNullPassword() {
    User u = new User();
    BindException errors = new BindException(u, "nickname");
    u.setNickname("spork");

    mVal.validate(u, errors);/* www .j  a  va  2s  .  c om*/
    assertEquals("null password 1", 0, errors.getFieldErrorCount("nickname"));
    assertEquals("null password 2", 1, errors.getFieldErrorCount("unencryptedPassword"));
    FieldError err = errors.getFieldError("unencryptedPassword");
    assertEquals("null password code", "error.password.empty", err.getCode());
}

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

public void testValidateEmptyPassword() {
    User u = new User();
    BindException errors = new BindException(u, "nickname");
    u.setNickname("spork");
    u.setUnencryptedPassword("");

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

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

public void testValidateShortPassword() {
    User u = new User();
    BindException errors = new BindException(u, "nickname");
    u.setNickname("spork");
    u.setUnencryptedPassword("spork");

    mVal.validate(u, errors);/*from www.j  a  v a 2  s  . c om*/
    assertEquals("short password 1", 0, errors.getFieldErrorCount("nickname"));
    assertEquals("short password 2", 1, errors.getFieldErrorCount("unencryptedPassword"));
    FieldError err = errors.getFieldError("unencryptedPassword");
    assertEquals("short password code", "error.password.length", err.getCode());
}