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);

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:alfio.controller.form.PaymentForm.java

private static void rejectIfOverLength(BindingResult bindingResult, String field, String errorCode,
        String value, int maxLength) {
    if (value != null && value.length() > maxLength) {
        bindingResult.rejectValue(field, errorCode);
    }//from   w  w w. jav a2  s .com
}

From source file:es.jffa.tsc.sip04.web.AccountController.java

/**
 *
 * @param result/*from   w  w  w. j a  va 2 s .  c  o  m*/
 */
private static void _convertPasswordError(final BindingResult result) {
    for (ObjectError error : result.getGlobalErrors()) {
        String msg = error.getDefaultMessage();

        if ("account.password.mismatch.message".equals(msg) == true) {
            if (result.hasFieldErrors("password") == false)
                result.rejectValue("password", "error.mismatch");
        }
    }
}

From source file:com.stephengream.simplecms.web.controllers.ProfileController.java

@RequestMapping(value = "new", method = RequestMethod.POST)
public String postNewUser(@ModelAttribute("user") @Valid UserCreationForm form, BindingResult result) {
    if (!result.hasErrors()) {
        if (userService.hasUser(form.getUsername())) {
            result.rejectValue("username", "newuser.error.usernameInUse");
            return NEW_PROFILE;
        }//from  w w w.  j av a2 s.c  o m
        userService.createUser(form.getUsername(), form.getPassword(), form.getEmail());
        return NEW_USER_OK;
    }
    return NEW_PROFILE;
}

From source file:org.wallride.web.controller.admin.user.UserDeleteController.java

@RequestMapping
public String delete(@Valid @ModelAttribute("form") UserDeleteForm form, BindingResult errors, String query,
        RedirectAttributes redirectAttributes) {
    if (!form.isConfirmed()) {
        errors.rejectValue("confirmed", "Confirmed");
    }/* w  w  w. j  av  a2s  .c  o m*/
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "user/describe";
    }

    User deletedUser;
    try {
        deletedUser = userService.deleteUser(form.buildUserDeleteRequest(), errors);
    } catch (BindException e) {
        if (errors.hasErrors()) {
            logger.debug("Errors: {}", errors);
            return "user/describe";
        }
        throw new RuntimeException(e);
    }

    redirectAttributes.addFlashAttribute("deletedUser", deletedUser);
    redirectAttributes.addAttribute("query", query);
    return "redirect:/_admin/{language}/users/index";
}

From source file:ems.emsystem.controller.DepartmentController.java

@RequestMapping(value = "/add", method = RequestMethod.POST)
public String addNewDepartment(@Valid Department dep, BindingResult bindingResult, Model model) {
    Department department = departmentService.getDepartment(dep.getName());
    if (department != null) {
        bindingResult.rejectValue("name", "department.exists");
    }//from   www . j  av  a  2  s. c  om
    if (bindingResult.hasErrors()) {
        return "newDepartment";
    }

    long id = departmentService.addDepartment(dep);
    return "redirect: /" + id;
}

From source file:org.wallride.web.controller.admin.page.PageBulkDeleteController.java

@RequestMapping
public String delete(@Valid @ModelAttribute("form") PageBulkDeleteForm form, BindingResult errors, String query,
        RedirectAttributes redirectAttributes) {
    redirectAttributes.addAttribute("query", query);

    if (!form.isConfirmed()) {
        errors.rejectValue("confirmed", "Confirmed");
    }/*  www  .j a  v a  2 s . c  o m*/
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "redirect:/_admin/{language}/pages/index";
    }

    Collection<Page> pages = null;
    try {
        pages = pageService.bulkDeletePage(form.buildPageBulkDeleteRequest(), errors);
    } catch (ValidationException e) {
        if (errors.hasErrors()) {
            logger.debug("Errors: {}", errors);
            return "redirect:/_admin/{language}/pages/index";
        }
        throw e;
    }

    List<String> errorMessages = null;
    if (errors.hasErrors()) {
        errorMessages = new ArrayList<>();
        for (ObjectError error : errors.getAllErrors()) {
            errorMessages.add(messageSourceAccessor.getMessage(error));
        }
    }

    redirectAttributes.addFlashAttribute("deletedPages", pages);
    redirectAttributes.addFlashAttribute("errorMessages", errorMessages);
    return "redirect:/_admin/{language}/pages/index";
}

From source file:org.wallride.web.controller.admin.comment.CommentBulkDeleteController.java

@RequestMapping
public String delete(@Valid @ModelAttribute("form") CommentBulkDeleteForm form, BindingResult errors,
        String query, AuthorizedUser authorizedUser, RedirectAttributes redirectAttributes, Model model) {
    redirectAttributes.addAttribute("query", query);

    if (!form.isConfirmed()) {
        errors.rejectValue("confirmed", "Confirmed");
    }// w  w w  .  j  a v  a  2  s .c  o m
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "redirect:/_admin/{language}/comments/index";
    }

    Collection<Comment> deletedComments = null;
    try {
        deletedComments = commentService.bulkDeleteComment(form.toCommentBulkDeleteRequest(), authorizedUser);
    } catch (ServiceException e) {
        return "redirect:/_admin/{language}/comments/index";
    }

    redirectAttributes.addFlashAttribute("deletedComments", deletedComments);
    return "redirect:/_admin/{language}/comments/index";
}

From source file:org.wallride.web.controller.admin.tag.TagBulkDeleteController.java

@RequestMapping
public String delete(@Valid @ModelAttribute("form") TagBulkDeleteForm form, BindingResult errors, String query,
        AuthorizedUser authorizedUser, RedirectAttributes redirectAttributes) {
    redirectAttributes.addAttribute("query", query);

    if (!form.isConfirmed()) {
        errors.rejectValue("confirmed", "Confirmed");
    }//from  www  . ja va  2s .  c o  m
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "redirect:/_admin/{language}/tags/index";
    }

    Collection<Tag> tags = null;
    try {
        tags = tagService.bulkDeleteTag(form.buildTagBulkDeleteRequest(), errors);
    } catch (ValidationException e) {
        if (errors.hasErrors()) {
            logger.debug("Errors: {}", errors);
            return "redirect:/_admin/{language}/tags/index";
        }
        throw e;
    }

    List<String> errorMessages = null;
    if (errors.hasErrors()) {
        errorMessages = new ArrayList<>();
        for (ObjectError error : errors.getAllErrors()) {
            errorMessages.add(messageSourceAccessor.getMessage(error));
        }
    }

    redirectAttributes.addFlashAttribute("deletedTags", tags);
    redirectAttributes.addFlashAttribute("errorMessages", errorMessages);
    return "redirect:/_admin/{language}/tags/index";
}

From source file:org.wallride.web.controller.admin.article.ArticleBulkDeleteController.java

@RequestMapping
public String delete(@Valid @ModelAttribute("form") ArticleBulkDeleteForm form, BindingResult errors,
        String query, AuthorizedUser authorizedUser, RedirectAttributes redirectAttributes) {
    redirectAttributes.addAttribute("query", query);

    if (!form.isConfirmed()) {
        errors.rejectValue("confirmed", "Confirmed");
    }/*from  w  w  w  .  j  a  v a  2 s.co m*/
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "redirect:/_admin/{language}/articles/index";
    }

    Collection<Article> articles = null;
    try {
        articles = articleService.bulkDeleteArticle(form.buildArticleBulkDeleteRequest(), errors);
    } catch (ValidationException e) {
        if (errors.hasErrors()) {
            logger.debug("Errors: {}", errors);
            return "redirect:/_admin/{language}/articles/index";
        }
        throw e;
    }

    List<String> errorMessages = null;
    if (errors.hasErrors()) {
        errorMessages = new ArrayList<>();
        for (ObjectError error : errors.getAllErrors()) {
            errorMessages.add(messageSourceAccessor.getMessage(error));
        }
    }

    redirectAttributes.addFlashAttribute("deletedArticles", articles);
    redirectAttributes.addFlashAttribute("errorMessages", errorMessages);
    return "redirect:/_admin/{language}/articles/index";
}

From source file:org.wallride.web.controller.admin.user.UserBulkDeleteController.java

@RequestMapping
public String delete(@Valid @ModelAttribute("form") UserBulkDeleteForm form, BindingResult errors, String query,
        AuthorizedUser authorizedUser, RedirectAttributes redirectAttributes) {
    redirectAttributes.addAttribute("query", query);

    if (!form.isConfirmed()) {
        errors.rejectValue("confirmed", "Confirmed");
    }// w  w w.  j  a v  a  2  s.  com
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "redirect:/_admin/user/";
    }

    Collection<User> users = null;
    try {
        users = userService.bulkDeleteUser(form.buildUserBulkDeleteRequest(), errors);
    } catch (ValidationException e) {
        if (errors.hasErrors()) {
            logger.debug("Errors: {}", errors);
            return "redirect:/_admin/user/";
        }
        throw e;
    }

    List<String> errorMessages = null;
    if (errors.hasErrors()) {
        errorMessages = new ArrayList<>();
        for (ObjectError error : errors.getAllErrors()) {
            errorMessages.add(messageSourceAccessor.getMessage(error));
        }
    }

    redirectAttributes.addFlashAttribute("deletedArticles", users);
    redirectAttributes.addFlashAttribute("errorMessages", errorMessages);
    return "redirect:/_admin/{language}/users/index";
}