Example usage for org.springframework.validation BindingResult rejectValue

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

Introduction

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

Prototype

void rejectValue(@Nullable String field, String errorCode, String defaultMessage);

Source Link

Document

Register a field error for the specified field of the current object (respecting the current nested path, if any), using the given error description.

Usage

From source file:com.github.javarch.persistence.orm.hibernate.BeanValidator.java

/**
 * Realiza a validao de um objeto atravs da JSR 303.
 * /*  w  w  w  .ja v a 2s.c  o m*/
 * @param objeto Entidade que contm anotaes da JSR 303
 * 
 * @return BindingResult Objeto que encapsula as mensagens de erros encontradas 
 * nas validaes do objeto atravs das anotaes da JSR 303
 */
public static <T> BindingResult validate(T objeto) {

    Assert.notNull(objeto, "No  possivel aplicar regras de validao JSR-303 em um objeto nulo.");

    BindingResult bindingResult = new BeanPropertyBindingResult(objeto, objeto.getClass().getSimpleName());

    javax.validation.Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
    Set<ConstraintViolation<T>> constraintViolations = validator.validate(objeto);

    for (ConstraintViolation<T> constraint : constraintViolations) {
        bindingResult.rejectValue(constraint.getPropertyPath().toString(), "", constraint.getMessage());
    }
    return bindingResult;
}

From source file:ch.ralscha.extdirectspring_itest.UserService.java

@ExtDirectMethod(value = ExtDirectMethodType.FORM_POST, group = "itest_user_service", synchronizeOnSession = true)
public ExtDirectFormPostResult updateUser(HttpServletRequest request, @Valid User user, BindingResult result) {

    if (request.getParameter("addemailerror") != null) {
        result.rejectValue("email", "", "another email error");
        result.rejectValue("name", "", "a name error");
    }/*w w  w  .j  a va  2s  .com*/

    ExtDirectFormPostResult resp = new ExtDirectFormPostResult(result);

    resp.addResultProperty("name", user.getName());
    resp.addResultProperty("age", user.getAge());

    return resp;
}

From source file:org.modeshape.example.spring.web.RepositoryModel.java

boolean validateParentPath(BindingResult bindingResult) {
    if (parentPath == null || parentPath.trim().length() == 0) {
        bindingResult.rejectValue("parentPath", "parentPath.empty", "Parent path must not be empty");
        return false;
    }/*from  w ww.j  av a  2 s  .  c  o m*/
    return true;
}

From source file:org.modeshape.example.spring.web.RepositoryModel.java

boolean validateNodeName(BindingResult bindingResult) {
    if (newNodeName == null || newNodeName.trim().length() == 0) {
        bindingResult.rejectValue("newNodeName", "newNodeName.empty", "New node name must not be empty");
        return false;
    }/*w  w w . j a  v a 2 s.  c o  m*/
    return true;
}

From source file:ch.ralscha.extdirectspring_itest.UserController.java

@ExtDirectMethod(value = ExtDirectMethodType.FORM_POST, group = "itest_user")
@RequestMapping(method = RequestMethod.POST)
public void updateUser(HttpServletRequest request, HttpServletResponse response, @Valid User user,
        BindingResult result) {
    ExtDirectResponseBuilder builder = new ExtDirectResponseBuilder(request, response);

    if (request.getParameter("addemailerror") != null) {
        result.rejectValue("email", "", "another email error");
        result.rejectValue("name", "", "a name error");
    }/* w w w  .  j a va  2  s  . c  o m*/

    builder.addErrors(result);

    builder.addResultProperty("name", user.getName());
    builder.addResultProperty("age", user.getAge());

    builder.buildAndWrite();
}

From source file:ru.org.linux.util.ExceptionBindingErrorProcessor.java

@Override
public void processPropertyAccessException(PropertyAccessException e, BindingResult bindingResult) {
    if (e.getCause() instanceof IllegalArgumentException
            && (e.getCause().getCause() instanceof ScriptErrorException
                    || e.getCause().getCause() instanceof UserNotFoundException)) {
        bindingResult.rejectValue(e.getPropertyChangeEvent().getPropertyName(), null,
                e.getCause().getCause().getMessage());
    } else {/*from w ww  .  ja v  a 2  s .c o  m*/
        super.processPropertyAccessException(e, bindingResult);
    }
}

From source file:com.rambird.miles.web.UserController.java

@RequestMapping(value = "/auth", method = RequestMethod.GET)
public String processFindForm(User user, BindingResult result, Model model) {

    try {/*from  w w  w .ja v  a  2 s. co  m*/
        // find owners by last name
        user = this.rambirdService.authUser(user.getUserName(), user.getPassword());
        return "welcome";
    } catch (ObjectRetrievalFailureException ex) {
        result.rejectValue("userName", "notFound", "not found");
        return "users/signin";

    }
}

From source file:no.kantega.kwashc.server.controller.SiteController.java

private void validateSite(Site site, BindingResult result) {
    if (site.getAddress() != null) {
        String header = getHeaderValue(site, result);

        if (header == null || header.trim().isEmpty()) {
            result.rejectValue("address", "", "Could not get meta header!");
        } else if (!header.equals(site.getSecret())) {
            result.rejectValue("address", "", "The secret meta header did not match the one found on page!");
        }//  www . j ava 2s .  com
    }

    if (site.getId() == null) {
        // new site, check for uniquess
        if (siteRepository.findByName(site.getName()) != null) {
            result.rejectValue("name", "", "Name already taken.");
        }
    }
}

From source file:oauth2.controllers.ChangePasswordController.java

@RequestMapping(method = RequestMethod.POST)
public String submit(HttpServletRequest request, HttpServletResponse response,
        @Validated @ModelAttribute(FORM_ATTRIBUTE_NAME) ChangePasswordForm form, BindingResult result) {

    if (result.hasErrors()) {
        return VIEW_CHANGE_PASSWORD;
    }//from   w w  w .  java2s .  co  m
    try {
        changePasswordService.changePassword(form.getUserId(), form.getOldPassword(), form.getNewPassword());
    } catch (ChangePasswordException ex) {
        result.rejectValue("userId", ex.getMessage(), ex.getLocalizedMessage());
    } catch (AuthenticationException ex) {
        result.reject(ex.getMessage(), ex.getLocalizedMessage());
    }
    if (result.hasErrors()) {
        form.clearPasswords();
        return VIEW_CHANGE_PASSWORD;
    }
    return "redirect:" + getTargetUrl(request, response);
}

From source file:com.kdubb.socialshowcaseboot.signup.SignupController.java

private Account createAccount(SignupForm form, BindingResult formBinding) {
    try {/*from   w w w . jav a  2 s.  c o m*/
        Account account = new Account(form.getUsername(), form.getPassword(), form.getFirstName(),
                form.getLastName());
        accountRepository.createAccount(account);
        return account;
    } catch (UsernameAlreadyInUseException e) {
        formBinding.rejectValue("username", "user.duplicateUsername", "already in use");
        return null;
    }
}