Example usage for org.springframework.validation BindingResult addError

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

Introduction

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

Prototype

void addError(ObjectError error);

Source Link

Document

Add a custom ObjectError or FieldError to the errors list.

Usage

From source file:org.jtalks.jcommune.web.controller.UserController.java

/**
 * Tries to restore a password by email.
 * If e-mail given has not been registered
 * before view with an error will be returned.
 *
 * @param dto    with email address to identify the user
 * @param result email validation result
 * @return view with a parameters bound// w  w  w.jav  a2s.c o  m
 */
@RequestMapping(value = "/password/restore", method = RequestMethod.POST)
public ModelAndView restorePassword(@Valid @ModelAttribute("dto") RestorePasswordDto dto,
        BindingResult result) {
    ModelAndView mav = new ModelAndView("restorePassword");
    if (result.hasErrors()) {
        return mav;
    }
    try {
        userService.restorePassword(dto.getUserEmail());
        mav.addObject("message", "label.restorePassword.completed");
    } catch (MailingFailedException e) {
        result.addError(new FieldError("dto", "email", "email.failed"));
    }
    return mav;
}

From source file:org.mifos.ui.core.controller.DefineProductCategoryPreviewController.java

@RequestMapping(method = RequestMethod.POST)
public ModelAndView processFormSubmit(@RequestParam(value = CANCEL_PARAM, required = false) String cancel,
        @RequestParam(value = EDIT_PARAM, required = false) String edit,
        @ModelAttribute("formBean") ProductCategoryFormBean bean, BindingResult result) {
    ModelAndView modelAndView = new ModelAndView("defineNewCategory");
    if (StringUtils.isNotBlank(cancel)) {
        modelAndView.setViewName(REDIRECT_TO_ADMIN);
    } else if (StringUtils.isNotBlank(edit)) {
        modelAndView.setViewName("defineNewCategory");
        modelAndView.addObject("formBean", bean);
        modelAndView.addObject("typeList", this.getProductCategoryTypes());
    } else if (result.hasErrors()) {
        modelAndView.setViewName("newProductCategoryPreview");
        modelAndView.addObject("formBean", bean);
    } else {//from www .  j a v  a  2 s. c o m
        CreateOrUpdateProductCategory productCategory = new CreateOrUpdateProductCategory(
                Short.parseShort(bean.getProductTypeId()), bean.getProductCategoryName(),
                bean.getProductCategoryDesc(), Short.parseShort(bean.getProductCategoryStatusId()),
                bean.getGlobalPrdCategoryNum());

        try {
            this.adminServiceFacade.createProductCategory(productCategory);
            modelAndView.setViewName(REDIRECT_TO_ADMIN);
        } catch (BusinessRuleException e) {
            ObjectError error = new ObjectError("formBean", new String[] { e.getMessageKey() }, new Object[] {},
                    "default: ");
            result.addError(error);
            modelAndView.setViewName("newProductCategoryPreview");
            modelAndView.addObject("formBean", bean);
        }
    }
    return modelAndView;
}

From source file:org.mifos.ui.core.controller.ProductCategoryPreviewController.java

@RequestMapping(method = RequestMethod.POST)
public ModelAndView processFormSubmit(@RequestParam(value = EDIT_PARAM, required = false) String edit,
        @RequestParam(value = CANCEL_PARAM, required = false) String cancel,
        @ModelAttribute("formBean") ProductCategoryFormBean formBean, BindingResult result) {

    String viewName = REDIRECT_TO_ADMIN_SCREEN;
    ModelAndView modelAndView = new ModelAndView();

    if (StringUtils.isNotBlank(edit)) {
        viewName = "editCategoryInformation";
        modelAndView.setViewName(viewName);
        modelAndView.addObject("formBean", formBean);
        modelAndView.addObject("breadcrumbs",
                new AdminBreadcrumbBuilder()
                        .withLink("admin.viewproductcategories", "viewProductCategories.ftl")
                        .withLink(formBean.getProductCategoryName(), "").build());
    } else if (StringUtils.isNotBlank(cancel)) {
        viewName = REDIRECT_TO_ADMIN_SCREEN;
        modelAndView.setViewName(viewName);
    } else if (result.hasErrors()) {
        viewName = "categoryPreview";
        modelAndView.setViewName(viewName);
        modelAndView.addObject("formBean", formBean);
        modelAndView.addObject("breadcrumbs",
                new AdminBreadcrumbBuilder()
                        .withLink("admin.viewproductcategories", "viewProductCategories.ftl")
                        .withLink(formBean.getProductCategoryName(), "").build());
    } else {/* ww  w.  j a v  a  2  s. c  o m*/
        Integer productStatusId = Integer.parseInt(formBean.getProductCategoryStatusId());
        Integer productTypeId = Integer.parseInt(formBean.getProductTypeId());

        CreateOrUpdateProductCategory productCategory = new CreateOrUpdateProductCategory(
                productTypeId.shortValue(), formBean.getProductCategoryName(),
                formBean.getProductCategoryDesc(), productStatusId.shortValue(),
                formBean.getGlobalPrdCategoryNum());

        try {
            this.adminServiceFacade.updateProductCategory(productCategory);
            modelAndView.setViewName(REDIRECT_TO_ADMIN_SCREEN);
        } catch (BusinessRuleException e) {
            ObjectError error = new ObjectError("formBean", new String[] { e.getMessageKey() }, new Object[] {},
                    "default: ");
            result.addError(error);
            modelAndView.setViewName("categoryPreview");
            modelAndView.addObject("formBean", formBean);
            modelAndView.addObject("breadcrumbs",
                    new AdminBreadcrumbBuilder()
                            .withLink("admin.viewproductcategories", "viewProductCategories.ftl")
                            .withLink(formBean.getProductCategoryName(), "").build());
        }
    }
    return modelAndView;
}

From source file:org.mifos.ui.core.controller.SavingsProductPreviewController.java

private void handleBusinessRuleViolation(String editFormview, SavingsProductFormBean savingsProduct,
        BindingResult result, ModelAndView modelAndView, String messageKey) {
    ObjectError error = new ObjectError("savingsProduct", new String[] { messageKey }, new Object[] {},
            "Error: Problem persisting savings product.");
    result.addError(error);
    modelAndView.setViewName("previewSavingsProducts");
    modelAndView.addObject("savingsProduct", savingsProduct);
    modelAndView.addObject("editFormview", editFormview);
    populateModelAndViewForPreview(savingsProduct, modelAndView);
}

From source file:org.mifos.ui.core.controller.SavingsProductValidator.java

public void validateGroup(SavingsProductFormBean savingsProductFormBean, BindingResult result) {
    if (savingsProductFormBean.isGroupSavingAccount()
            && StringUtils.isBlank(savingsProductFormBean.getSelectedGroupSavingsApproach())) {
        ObjectError error = new ObjectError("savingsProduct",
                new String[] { "NotEmpty.savingsProduct.selectedGroupSavingsApproach" }, new Object[] {},
                "default: ");
        result.addError(error);
    }/*w  w  w  .  j  a v a2 s  .  c  o  m*/
}

From source file:org.mifos.ui.core.controller.SavingsProductValidator.java

public void validateManadtorySavingsProduct(SavingsProductFormBean savingsProductFormBean,
        BindingResult result) {
    if (savingsProductFormBean.isMandatory() && savingsProductFormBean.getAmountForDeposit() == null
            || savingsProductFormBean.getAmountForDeposit().intValue() <= 0) {
        ObjectError error = new ObjectError("savingsProduct",
                new String[] { "Min.savingsProduct.amountForDesposit" }, new Object[] {}, "default: ");
        result.addError(error);
    }//from   w w  w . ja  v  a 2s  . c o m
}

From source file:org.mifos.ui.core.controller.UploadLogoController.java

@RequestMapping(value = "/uploadNewLogo.ftl", method = RequestMethod.POST)
public String uploadNewLogo(@ModelAttribute LogoUpload logoUpload, BindingResult result, Model model,
        HttpServletRequest request) {/*from   w  ww.j av  a 2s  . com*/
    String[] availableContentTypes = { "image/png", "image/gif", "image/jpeg", "image/pjpeg" };
    if (Arrays.asList(availableContentTypes).contains(logoUpload.getFile().getContentType())) {
        try {
            logoServiceFacade.uploadNewLogo(logoUpload.getFile());
            model.addAttribute("success", true);
        } catch (IOException e) {
            result.addError(new ObjectError("logoUpload", messageSource
                    .getMessage("admin.uploadLogo.ioexception", null, RequestContextUtils.getLocale(request))));
        }
    } else {
        result.addError(new ObjectError("logoUpload", messageSource.getMessage("admin.uploadLogo.badType", null,
                RequestContextUtils.getLocale(request))));
    }
    return "uploadNewLogo";
}

From source file:org.mifos.ui.pentaho.controller.PentahoReportingController.java

private void addErrorToBindingResult(PentahoValidationError validationError, BindingResult bindingResult) {
    ObjectError error;/*from   w  w  w  .  j  a va 2  s . c  o  m*/
    if (validationError.isFieldError()) {
        error = new FieldError("pentahoReportFormBean", validationError.getParamName(),
                validationError.getParamName() + ": " + validationError.getErrorMessage());
    } else {
        error = new ObjectError("pentahoReportFormBean", validationError.getErrorMessage());
    }
    bindingResult.addError(error);
}

From source file:org.opentestsystem.authoring.testauth.service.impl.FormPartitionServiceImpl.java

private void validateFormPartition(final FormPartition formPartition) {
    final BindingResult bindingResult = new BeanPropertyBindingResult(formPartition, "formPartition");

    if (formPartition.getFormId() != null) {
        final Form form = this.formRepository.findOne(formPartition.getFormId());
        if (form == null) {
            final String messageCode = "formPartition.formId.notfound";
            bindingResult.addError(new FieldError("formPartition", "formId", null, false,
                    new String[] { messageCode }, new String[] { formPartition.getSegmentId() }, messageCode));
        }// w ww.  j  a  va  2  s  .c  o m
    } else {
        final String messageCode = "formPartition.formId.required";
        bindingResult.addError(new FieldError("formPartition", "formId", null, false,
                new String[] { messageCode }, new String[] { formPartition.getSegmentId() }, messageCode));
    }

    if (formPartition.getSegmentId() == null) {
        final String messageCode = "formPartition.segmentId.required";
        bindingResult.addError(new FieldError("formPartition", "segmentId", null, false,
                new String[] { messageCode }, new String[] { formPartition.getSegmentId() }, messageCode));
    } else {
        final Segment segment = this.segmentService.getSegment(formPartition.getSegmentId());
        if (segment == null) {
            final String messageCode = "formPartition.segmentId.notfound";
            bindingResult.addError(new FieldError("formPartition", "segmentId", null, false,
                    new String[] { messageCode }, new String[] { formPartition.getSegmentId() }, messageCode));
        } else {
            if (segment.getItemSelectionAlgorithm()
                    .getItemSelectionAlgorithmType() != ItemSelectionAlgorithmType.FIXEDFORM) {
                final String messageCode = "formPartition.segmentId.notfixed";
                bindingResult.addError(
                        new FieldError("formPartition", "segmentId", null, false, new String[] { messageCode },
                                new String[] { formPartition.getSegmentId() }, messageCode));
            }
        }
    }

    if (bindingResult.hasErrors()) {
        throw ValidationHelper.convertErrorsToConstraintException(formPartition, bindingResult);
    }
}

From source file:org.opentestsystem.authoring.testauth.service.impl.PerformanceLevelServiceImpl.java

private void formatReferenceIdMessage(final BindingResult bindingResult,
        final PerformanceLevel performanceLevel) {
    bindingResult.addError(new FieldError("performanceLevel", // objectName
            "blueprintReferenceId", // field
            null, // rejectedValue
            false, // bindingFailure
            paramArray("blueprintReferenceId.notfound"), // messageCode
            paramArray(// arguments
                    performanceLevel.getBlueprintReferenceType().name(),
                    performanceLevel.getBlueprintReferenceId(),
                    performanceLevel.getBlueprintReferenceType().getTitle(),
                    performanceLevel.getAssessmentId()),
            "blueprintReferenceId.notfound")); // defaultMessage

}