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:whitelabel.cloud.webapp.impl.controller.NewCloudServersController.java

@RequestMapping(value = { "/createServer" }, method = { RequestMethod.POST })
public String createServer(HttpServletRequest request, HttpServletResponse response,
        @ModelAttribute("NewCloudServer") @Valid NewCloudServer newCloudServer, BindingResult bResult,
        Model model, Locale locale, RedirectAttributes redirectAttributes) {

    if (!bResult.hasErrors()) {
        boolean isOk = true;
        if (!pwdCompare(newCloudServer)) {
            bResult.rejectValue("passwordChk", "NotMatch");
            isOk = false;//from  w w w  . ja va  2 s .c o m
        }

        String dsk = request.getParameter("selectedDiskSize[1]");
        if (dsk == null) {
            newCloudServer.getSelectedDiskSize().put(1L, -1);
        }
        dsk = request.getParameter("selectedDiskSize[2]");
        if (dsk == null) {
            newCloudServer.getSelectedDiskSize().put(2L, -1);
        }
        dsk = request.getParameter("selectedDiskSize[3]");
        if (dsk == null) {
            newCloudServer.getSelectedDiskSize().put(3L, -1);
        }

        int res = validateHd(newCloudServer);
        if (res > 0) {
            isOk = false;
            int disk = res / 10;
            int errn = res % 10;
            String errMSG = (errn == 1 ? "NewCloudServer.error.size" : "NewCloudServer.error.hdrange");
            bResult.rejectValue("selectedDiskSize[" + disk + "]", errMSG);
        }
        res = validateEthParams(newCloudServer);
        if (res > 0) {
            isOk = false;
            switch (res) {
            case 2:
                bResult.rejectValue("vlan_eth02", "NotValid");
                break;
            case 3:
                bResult.rejectValue("vlan_eth03", "NotValid");
                break;
            case 23:
                bResult.rejectValue("vlan_eth03", "NotValid");
                break;
            default:
                bResult.rejectValue("vlan_eth02", "NotValid");
                break;
            }
        }
        if (isOk) {
            AppWsResult result = newCloudServerService.invokeServerCreation(newCloudServer);
            if (result != null && result.isSuccess()) {
                return "newCloudServer/inprogress";
            } else {
                bResult.rejectValue("createError", "NewCloudServer.create.error",
                        new String[] { result.getErrorMessage() }, null);
            }
        }
    }
    return "newCloudServer/view";
}

From source file:com.github.dbourdette.otto.web.controller.sources.SourcesController.java

@RequestMapping(value = "/sources/{name}/capping", method = RequestMethod.POST)
public String saveCapping(@PathVariable String name, @Valid @ModelAttribute("form") CappingForm form,
        BindingResult result, Model model) {
    if (StringUtils.isNotEmpty(form.getSize())) {
        try {// w w  w .j a v  a 2s .  co  m
            form.getSizeInBytes();
        } catch (Exception e) {
            result.rejectValue("size", "size.invalidPattern");
        }
    }

    if (result.hasErrors()) {
        return "sources/capping_form";
    }

    flashScope.message("Source " + name + " has been capped");

    Source.findByName(name).cap(form);

    return "redirect:/sources/{name}/configuration";
}

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

@RequestMapping(method = RequestMethod.POST, params = "publish")
public String saveAsPublished(@PathVariable String language,
        @Validated({ Default.class,
                ArticleCreateForm.GroupPublish.class }) @ModelAttribute("form") ArticleCreateForm form,
        BindingResult errors, String query, AuthorizedUser authorizedUser,
        RedirectAttributes redirectAttributes, Model model) {
    if (errors.hasErrors()) {
        return "article/create";
    }/*ww  w  .j av  a 2  s.  c  om*/

    Article article = null;
    try {
        article = articleService.createArticle(form.buildArticleCreateRequest(), Post.Status.PUBLISHED,
                authorizedUser);
    } catch (EmptyCodeException e) {
        errors.rejectValue("code", "NotNull");
    } catch (DuplicateCodeException e) {
        errors.rejectValue("code", "NotDuplicate");
    }
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "article/create";
    }

    redirectAttributes.addFlashAttribute("savedArticle", article);
    redirectAttributes.addAttribute("language", language);
    redirectAttributes.addAttribute("id", article.getId());
    redirectAttributes.addAttribute("query", query);
    return "redirect:/_admin/{language}/articles/describe";
}

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

@RequestMapping(method = RequestMethod.POST, params = "update")
public String update(@PathVariable String language,
        @Validated({ Default.class,
                PageEditForm.GroupPublish.class }) @ModelAttribute("form") PageEditForm form,
        BindingResult errors, String query, AuthorizedUser authorizedUser,
        RedirectAttributes redirectAttributes) {
    if (errors.hasErrors()) {
        return "page/edit";
    }//from   ww  w  . j  a  va2 s .co m

    Page page = null;
    try {
        page = pageService.savePage(form.buildPageUpdateRequest(), authorizedUser);
    } catch (EmptyCodeException e) {
        errors.rejectValue("code", "NotNull");
    } catch (DuplicateCodeException e) {
        errors.rejectValue("code", "NotDuplicate");
    }
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "page/edit";
    }

    redirectAttributes.addFlashAttribute("savedPage", page);
    redirectAttributes.addAttribute("language", language);
    redirectAttributes.addAttribute("id", page.getId());
    redirectAttributes.addAttribute("query", query);
    return "redirect:/_admin/{language}/pages/describe";
}

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

@RequestMapping(method = RequestMethod.POST, params = "publish")
public String saveAsPublished(@PathVariable String language,
        @Validated({ Default.class,
                PageEditForm.GroupPublish.class }) @ModelAttribute("form") PageEditForm form,
        BindingResult errors, String query, AuthorizedUser authorizedUser,
        RedirectAttributes redirectAttributes) {
    if (errors.hasErrors()) {
        return "page/edit";
    }//from  w ww  .ja va  2 s.  co m

    Page page = null;
    try {
        page = pageService.savePageAsPublished(form.buildPageUpdateRequest(), authorizedUser);
    } catch (EmptyCodeException e) {
        errors.rejectValue("code", "NotNull");
    } catch (DuplicateCodeException e) {
        errors.rejectValue("code", "NotDuplicate");
    }
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "page/edit";
    }

    redirectAttributes.addFlashAttribute("savedPage", page);
    redirectAttributes.addAttribute("language", language);
    redirectAttributes.addAttribute("id", page.getId());
    redirectAttributes.addAttribute("query", query);
    return "redirect:/_admin/{language}/pages/describe";
}

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

@RequestMapping(method = RequestMethod.POST, params = "unpublish")
public String saveAsUnpublished(@PathVariable String language,
        @Validated({ Default.class,
                PageEditForm.GroupPublish.class }) @ModelAttribute("form") PageEditForm form,
        BindingResult errors, String query, AuthorizedUser authorizedUser,
        RedirectAttributes redirectAttributes) {
    if (errors.hasErrors()) {
        return "page/edit";
    }//from w w w  .java  2  s .c  o  m

    Page page = null;
    try {
        page = pageService.savePageAsUnpublished(form.buildPageUpdateRequest(), authorizedUser);
    } catch (EmptyCodeException e) {
        errors.rejectValue("code", "NotNull");
    } catch (DuplicateCodeException e) {
        errors.rejectValue("code", "NotDuplicate");
    }
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "page/edit";
    }

    redirectAttributes.addFlashAttribute("savedPage", page);
    redirectAttributes.addAttribute("language", language);
    redirectAttributes.addAttribute("id", page.getId());
    redirectAttributes.addAttribute("query", query);
    return "redirect:/_admin/{language}/pages/describe";
}

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 ww.  j a  va 2s . co m
    }

    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.wallride.web.controller.admin.article.ArticleEditController.java

@RequestMapping(method = RequestMethod.POST, params = "update")
public String update(@PathVariable String language,
        @Validated({ Default.class,
                ArticleEditForm.GroupPublish.class }) @ModelAttribute("form") ArticleEditForm form,
        BindingResult errors, String query, AuthorizedUser authorizedUser,
        RedirectAttributes redirectAttributes) {
    if (errors.hasErrors()) {
        return "article/edit";
    }//  w w w .  jav  a 2 s .  c o  m

    Article article = null;
    try {
        article = articleService.saveArticle(form.buildArticleUpdateRequest(), authorizedUser);
    } catch (EmptyCodeException e) {
        errors.rejectValue("code", "NotNull");
    } catch (DuplicateCodeException e) {
        errors.rejectValue("code", "NotDuplicate");
    }
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "article/edit";
    }

    redirectAttributes.addFlashAttribute("savedArticle", article);
    redirectAttributes.addAttribute("language", language);
    redirectAttributes.addAttribute("id", article.getId());
    redirectAttributes.addAttribute("query", query);
    return "redirect:/_admin/{language}/articles/describe";
}

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

@RequestMapping(method = RequestMethod.POST, params = "publish")
public String saveAsPublished(@PathVariable String language,
        @Validated({ Default.class,
                ArticleEditForm.GroupPublish.class }) @ModelAttribute("form") ArticleEditForm form,
        BindingResult errors, String query, AuthorizedUser authorizedUser,
        RedirectAttributes redirectAttributes) {
    if (errors.hasErrors()) {
        return "article/edit";
    }/*from   w w  w  .  j  av  a 2 s. co  m*/

    Article article = null;
    try {
        article = articleService.saveArticleAsPublished(form.buildArticleUpdateRequest(), authorizedUser);
    } catch (EmptyCodeException e) {
        errors.rejectValue("code", "NotNull");
    } catch (DuplicateCodeException e) {
        errors.rejectValue("code", "NotDuplicate");
    }
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "article/edit";
    }

    redirectAttributes.addFlashAttribute("savedArticle", article);
    redirectAttributes.addAttribute("language", language);
    redirectAttributes.addAttribute("id", article.getId());
    redirectAttributes.addAttribute("query", query);
    return "redirect:/_admin/{language}/articles/describe";
}

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

@RequestMapping(method = RequestMethod.POST, params = "unpublish")
public String saveAsUnpublished(@PathVariable String language,
        @Validated({ Default.class,
                ArticleEditForm.GroupPublish.class }) @ModelAttribute("form") ArticleEditForm form,
        BindingResult errors, String query, AuthorizedUser authorizedUser,
        RedirectAttributes redirectAttributes) {
    if (errors.hasErrors()) {
        return "article/edit";
    }/*w w w . ja  v a  2s .  co m*/

    Article article = null;
    try {
        article = articleService.saveArticleAsUnpublished(form.buildArticleUpdateRequest(), authorizedUser);
    } catch (EmptyCodeException e) {
        errors.rejectValue("code", "NotNull");
    } catch (DuplicateCodeException e) {
        errors.rejectValue("code", "NotDuplicate");
    }
    if (errors.hasErrors()) {
        logger.debug("Errors: {}", errors);
        return "article/edit";
    }

    redirectAttributes.addFlashAttribute("savedArticle", article);
    redirectAttributes.addAttribute("language", language);
    redirectAttributes.addAttribute("id", article.getId());
    redirectAttributes.addAttribute("query", query);
    return "redirect:/_admin/{language}/articles/describe";
}