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, @Nullable Object[] errorArgs,
        @Nullable 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.epam.trade.storefront.controllers.pages.AccountPageController.java

@RequestMapping(value = "/update-password", method = RequestMethod.POST)
public String updatePassword(final UpdatePasswordForm updatePasswordForm, final BindingResult bindingResult,
        final Model model, final RedirectAttributes redirectAttributes) throws CMSItemNotFoundException {
    getPasswordValidator().validate(updatePasswordForm, bindingResult);
    if (!bindingResult.hasErrors()) {
        if (updatePasswordForm.getNewPassword().equals(updatePasswordForm.getCheckNewPassword())) {
            try {
                customerFacade.changePassword(updatePasswordForm.getCurrentPassword(),
                        updatePasswordForm.getNewPassword());
            } catch (final PasswordMismatchException localException) {
                bindingResult.rejectValue("currentPassword", "profile.currentPassword.invalid", new Object[] {},
                        "profile.currentPassword.invalid");
            }// ww w .ja  v  a 2  s. c om
        } else {
            bindingResult.rejectValue("checkNewPassword", "validation.checkPwd.equals", new Object[] {},
                    "validation.checkPwd.equals");
        }
    }

    if (bindingResult.hasErrors()) {
        GlobalMessages.addErrorMessage(model, "form.global.error");
        storeCmsPageInModel(model, getContentPageForLabelOrId(UPDATE_PASSWORD_CMS_PAGE));
        setUpMetaDataForContentPage(model, getContentPageForLabelOrId(UPDATE_PASSWORD_CMS_PAGE));

        model.addAttribute("breadcrumbs",
                accountBreadcrumbBuilder.getBreadcrumbs("text.account.profile.updatePasswordForm"));
        return getViewForPage(model);
    } else {
        GlobalMessages.addFlashMessage(redirectAttributes, GlobalMessages.CONF_MESSAGES_HOLDER,
                "text.account.confirmation.password.updated", null);
        return REDIRECT_TO_PROFILE_PAGE;
    }
}

From source file:com.ctc.storefront.controllers.pages.AccountPageController.java

@RequestMapping(value = "/update-password", method = RequestMethod.POST)
@RequireHardLogIn//from w w w  . jav a  2 s  .  c  om
public String updatePassword(final UpdatePasswordForm updatePasswordForm, final BindingResult bindingResult,
        final Model model, final RedirectAttributes redirectAttributes) throws CMSItemNotFoundException {
    getPasswordValidator().validate(updatePasswordForm, bindingResult);
    if (!bindingResult.hasErrors()) {
        if (updatePasswordForm.getNewPassword().equals(updatePasswordForm.getCheckNewPassword())) {
            try {
                customerFacade.changePassword(updatePasswordForm.getCurrentPassword(),
                        updatePasswordForm.getNewPassword());
            } catch (final PasswordMismatchException localException) {
                bindingResult.rejectValue("currentPassword", PROFILE_CURRENT_PASSWORD_INVALID, new Object[] {},
                        PROFILE_CURRENT_PASSWORD_INVALID);
            }
        } else {
            bindingResult.rejectValue("checkNewPassword", "validation.checkPwd.equals", new Object[] {},
                    "validation.checkPwd.equals");
        }
    }

    if (bindingResult.hasErrors()) {
        GlobalMessages.addErrorMessage(model, FORM_GLOBAL_ERROR);
        storeCmsPageInModel(model, getContentPageForLabelOrId(UPDATE_PASSWORD_CMS_PAGE));
        setUpMetaDataForContentPage(model, getContentPageForLabelOrId(UPDATE_PASSWORD_CMS_PAGE));

        model.addAttribute(BREADCRUMBS_ATTR,
                accountBreadcrumbBuilder.getBreadcrumbs("text.account.profile.updatePasswordForm"));
        return getViewForPage(model);
    } else {
        GlobalMessages.addFlashMessage(redirectAttributes, GlobalMessages.CONF_MESSAGES_HOLDER,
                "text.account.confirmation.password.updated", null);
        return REDIRECT_TO_PASSWORD_UPDATE_PAGE;
    }
}

From source file:com.healthcit.cacure.web.controller.question.TableElementEditController.java

/**
 * Process data entered by user// w  w  w.  java 2s.  c om
 * @param question
 * @param formId
 * @return
 */
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.POST)
public ModelAndView onSubmit(@ModelAttribute(COMMAND_NAME) TableElement table, BindingResult result,
        @ModelAttribute(LOOKUP_DATA) Map lookupData, SessionStatus status, HttpServletRequest req) {

    validateEditOperation(table);
    boolean isNew = table.isNew();
    Long formId = null;

    try {
        if (table.isNew()) {
            formId = getFormId();
            qaManager.addNewFormElement(table, formId);
        } else {
            qaManager.updateFormElement(table);
            formId = table.getForm().getId();
        }
    } catch (PersistenceException e) {
        Throwable t = e.getCause();
        if (t instanceof GenericJDBCException) {
            String message = ((GenericJDBCException) t).getSQLException().getNextException().getMessage();
            if (isNew) {
                table.resetId();
                table.getDescriptionList().clear();
            }
            if (message.indexOf("A table with the same short name already exists") > -1) {
                int beginIndex = message.indexOf('[');
                int endIndex = message.indexOf(']');
                String shortName = message.substring(beginIndex + 1, endIndex);
                Object[] params = { shortName };
                result.rejectValue("tableShortName", "notunique.shortName", params, "Short name is not unique");
                return new ModelAndView("questionTableEdit");
            }
            if (message.indexOf("A question with the same short name already exists") > -1) {
                int beginIndex = message.indexOf('[');
                int endIndex = message.indexOf(']');
                String shortName = message.substring(beginIndex + 1, endIndex);
                Object[] params = { shortName };
                result.reject("notunique.shortName", params, "Short name is not unique");
                return new ModelAndView("questionTableEdit");
            } else {
                throw e;
            }
        } else {
            throw e;
        }
    }

    table.setCategories(prepareCategories(req, lookupData));
    //      TODO Save 2 times is not good idea. We clear constraints by second update (see implementation)
    //      Changes to attached to session object save automatically. So categories are seved
    //      qaManager.updateFormElement(table);

    // after question is saved - return to question listing
    return new ModelAndView(new RedirectView(Constants.QUESTION_LISTING_URI + "?formId=" + formId, true));
}

From source file:cec.easyshop.storefront.controllers.pages.AccountPageController.java

@RequestMapping(value = "/update-password", method = RequestMethod.POST)
@RequireHardLogIn/*ww  w  .ja v a 2 s  .  co  m*/
public String updatePassword(final UpdatePasswordForm updatePasswordForm, final BindingResult bindingResult,
        final Model model, final RedirectAttributes redirectAttributes) throws CMSItemNotFoundException {
    getPasswordValidator().validate(updatePasswordForm, bindingResult);
    if (!bindingResult.hasErrors()) {
        if (updatePasswordForm.getNewPassword().equals(updatePasswordForm.getCheckNewPassword())) {
            try {
                customerFacade.changePassword(updatePasswordForm.getCurrentPassword(),
                        updatePasswordForm.getNewPassword());
            } catch (final PasswordMismatchException localException) {
                bindingResult.rejectValue("currentPassword", "profile.currentPassword.invalid", new Object[] {},
                        "profile.currentPassword.invalid");
            }
        } else {
            bindingResult.rejectValue("checkNewPassword", "validation.checkPwd.equals", new Object[] {},
                    "validation.checkPwd.equals");
        }
    }

    if (bindingResult.hasErrors()) {
        GlobalMessages.addErrorMessage(model, "form.global.error");
        storeCmsPageInModel(model, getContentPageForLabelOrId(UPDATE_PASSWORD_CMS_PAGE));
        setUpMetaDataForContentPage(model, getContentPageForLabelOrId(UPDATE_PASSWORD_CMS_PAGE));

        model.addAttribute("breadcrumbs",
                accountBreadcrumbBuilder.getBreadcrumbs("text.account.profile.updatePasswordForm"));
        return getViewForPage(model);
    } else {
        GlobalMessages.addFlashMessage(redirectAttributes, GlobalMessages.CONF_MESSAGES_HOLDER,
                "text.account.confirmation.password.updated", null);
        return REDIRECT_TO_PASSWORD_UPDATE_PAGE;
    }
}

From source file:de.hybris.platform.commerceorgaddon.controllers.pages.UserManagementPageController.java

@RequestMapping(value = "/resetpassword", method = RequestMethod.POST)
@RequireHardLogIn/*www  . ja  v  a  2 s. c om*/
public String updatePassword(@RequestParam("user") final String user,
        @Valid final CustomerResetPasswordForm customerResetPasswordForm, final BindingResult bindingResult,
        final Model model, final RedirectAttributes redirectModel) throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
        model.addAttribute(customerResetPasswordForm);
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return updatePassword(customerResetPasswordForm.getUid(), model);
    }

    if (customerResetPasswordForm.getNewPassword().equals(customerResetPasswordForm.getCheckNewPassword())) {

        b2bUserFacade.resetCustomerPassword(customerResetPasswordForm.getUid(),
                customerResetPasswordForm.getNewPassword());
        GlobalMessages.addFlashMessage(redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER,
                "text.confirmation.password.updated");
    } else {
        model.addAttribute(customerResetPasswordForm);
        bindingResult.rejectValue("checkNewPassword", "validation.checkPwd.equals", new Object[] {},
                "validation.checkPwd.equals");
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return updatePassword(customerResetPasswordForm.getUid(), model);
    }

    final List<Breadcrumb> breadcrumbs = myCompanyBreadcrumbBuilder
            .createManageUnitsDetailsBreadcrumbs(customerResetPasswordForm.getUid());
    breadcrumbs.add(new Breadcrumb(
            String.format("/my-company/organization-management/manage-users/restpassword?user=%s",
                    urlEncode(customerResetPasswordForm.getUid())),
            getMessageSource().getMessage("text.company.manageusers.restpassword.breadcrumb",
                    new Object[] { customerResetPasswordForm.getUid() }, "Reset Password {0}  Customer ",
                    getI18nService().getCurrentLocale()),
            null));
    model.addAttribute("breadcrumbs", breadcrumbs);
    return String.format(REDIRECT_TO_USER_DETAILS, urlEncode(customerResetPasswordForm.getUid()));
}

From source file:com.exxonmobile.ace.hybris.storefront.controllers.pages.UserManagementPageController.java

@RequestMapping(value = "/resetpassword", method = RequestMethod.POST)
@RequireHardLogIn/*  ww w .j  av a 2  s .com*/
public String updatePassword(@RequestParam("user") final String user,
        @Valid final CustomerResetPasswordForm customerResetPasswordForm, final BindingResult bindingResult,
        final Model model, final RedirectAttributes redirectModel) throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
        model.addAttribute(customerResetPasswordForm);
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return updatePassword(customerResetPasswordForm.getUid(), model);
    }

    if (customerResetPasswordForm.getNewPassword().equals(customerResetPasswordForm.getCheckNewPassword())) {

        b2bCommerceUserFacade.resetCustomerPassword(customerResetPasswordForm.getUid(),
                customerResetPasswordForm.getNewPassword());
        GlobalMessages.addFlashMessage(redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER,
                "text.confirmation.password.updated");
    } else {
        model.addAttribute(customerResetPasswordForm);
        bindingResult.rejectValue("checkNewPassword", "validation.checkPwd.equals", new Object[] {},
                "validation.checkPwd.equals");
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return updatePassword(customerResetPasswordForm.getUid(), model);
    }

    final List<Breadcrumb> breadcrumbs = myCompanyBreadcrumbBuilder
            .createManageUnitsDetailsBreadcrumbs(customerResetPasswordForm.getUid());
    breadcrumbs.add(new Breadcrumb(
            String.format("/my-company/organization-management/manage-users/restpassword?user=%s",
                    urlEncode(customerResetPasswordForm.getUid())),
            getMessageSource().getMessage("text.company.manageusers.restpassword.breadcrumb",
                    new Object[] { customerResetPasswordForm.getUid() }, "Reset Password {0}  Customer ",
                    getI18nService().getCurrentLocale()),
            null));
    model.addAttribute("breadcrumbs", breadcrumbs);
    return String.format(REDIRECT_TO_USER_DETAILS, urlEncode(customerResetPasswordForm.getUid()));
}

From source file:com.jnj.b2b.storefront.controllers.pages.AccountPageController.java

@RequestMapping(value = "/update-password", method = RequestMethod.POST)
@RequireHardLogIn//from   w  w  w  . j a v  a 2s .c o m
public String updatePassword(@Valid final UpdatePasswordForm updatePasswordForm,
        final BindingResult bindingResult, final Model model, final RedirectAttributes redirectAttributes)
        throws CMSItemNotFoundException {
    b2bUpdatePasswordFormValidator.validate(updatePasswordForm, bindingResult);

    if (!bindingResult.hasErrors()) {
        try {
            customerFacade.changePassword(updatePasswordForm.getCurrentPassword(),
                    updatePasswordForm.getNewPassword());
        } catch (final PasswordMismatchException localException) {
            bindingResult.rejectValue("currentPassword", "profile.currentPassword.invalid", new Object[] {},
                    "profile.currentPassword.invalid");
        }
    }

    if (bindingResult.hasErrors()) {
        GlobalMessages.addErrorMessage(model, "form.global.error");
        storeCmsPageInModel(model, getContentPageForLabelOrId(PROFILE_CMS_PAGE));
        setUpMetaDataForContentPage(model, getContentPageForLabelOrId(PROFILE_CMS_PAGE));

        model.addAttribute("breadcrumbs",
                accountBreadcrumbBuilder.getBreadcrumbs("text.account.profile.updatePasswordForm"));
        return ControllerConstants.Views.Pages.Account.AccountChangePasswordPage;
    } else {
        GlobalMessages.addFlashMessage(redirectAttributes, GlobalMessages.CONF_MESSAGES_HOLDER,
                "text.account.confirmation.password.updated");
        return REDIRECT_TO_PROFILE_PAGE;
    }
}

From source file:com.exxonmobile.ace.hybris.storefront.controllers.pages.AccountPageController.java

@RequestMapping(value = "/update-password", method = RequestMethod.POST)
@RequireHardLogIn/*from w w w .j a  v  a 2  s  .c  o  m*/
public String updatePassword(@Valid final UpdatePasswordForm updatePasswordForm,
        final BindingResult bindingResult, final Model model, final RedirectAttributes redirectAttributes)
        throws CMSItemNotFoundException {

    if (!bindingResult.hasErrors()) {
        if (updatePasswordForm.getNewPassword().equals(updatePasswordForm.getCheckNewPassword())) {
            try {
                customerFacade.changePassword(updatePasswordForm.getCurrentPassword(),
                        updatePasswordForm.getNewPassword());
            } catch (final PasswordMismatchException localException) {
                bindingResult.rejectValue("currentPassword", "profile.currentPassword.invalid", new Object[] {},
                        "profile.currentPassword.invalid");
            }

        } else {
            bindingResult.rejectValue("checkNewPassword", "validation.checkPwd.equals", new Object[] {},
                    "validation.checkPwd.equals");
        }
    }

    if (bindingResult.hasErrors()) {
        GlobalMessages.addErrorMessage(model, "form.global.error");
        storeCmsPageInModel(model, getContentPageForLabelOrId(PROFILE_CMS_PAGE));
        setUpMetaDataForContentPage(model, getContentPageForLabelOrId(PROFILE_CMS_PAGE));

        model.addAttribute("breadcrumbs",
                accountBreadcrumbBuilder.getBreadcrumbs("text.account.profile.updatePasswordForm"));
        return ControllerConstants.Views.Pages.Account.AccountChangePasswordPage;
    } else {
        GlobalMessages.addFlashMessage(redirectAttributes, GlobalMessages.CONF_MESSAGES_HOLDER,
                "text.account.confirmation.password.updated");
        return REDIRECT_TO_PROFILE_PAGE;
    }
}

From source file:com.healthcit.cacure.web.controller.question.QuestionElementEditController.java

/**
 * Process data entered by user//from  w  w w  . j a v  a 2  s.c om
 * @param question
 * @param formId
 * @return
 */
@SuppressWarnings("unchecked")
@RequestMapping(method = RequestMethod.POST)
public ModelAndView onSubmit(@ModelAttribute(COMMAND_NAME) FormElement qElement, BindingResult result,
        @ModelAttribute(LOOKUP_DATA) Map lookupData, SessionStatus status, HttpServletRequest req,
        @RequestParam(value = IS_EXTERNAL, required = false) String isExternal) {

    validateEditOperation(qElement);

    log.debug("QuestionElement: " + qElement.toString());

    log.debug(qElement.toString());
    Long formId = null;
    boolean isNew = qElement.isNew();
    try {

        if (isNew) {
            //The ExternalQuestionElement can only be created via import.
            formId = getFormId();
            qaManager.addNewFormElement(qElement, formId);
        } else {
            formId = qElement.getForm().getId();

            if (qElement instanceof ExternalQuestionElement) {
                //unlink because of modifications
                ((ExternalQuestionElement) qElement).unlink();
            }
            qaManager.updateFormElement(qElement);

        }
    } catch (PersistenceException e) {
        Throwable t = e.getCause();
        if (t instanceof GenericJDBCException) {
            String message = ((GenericJDBCException) t).getSQLException().getNextException().getMessage();
            if (message.indexOf("short name already exists") > -1) {
                if (isNew) {
                    qElement.resetId();
                    qElement.getDescriptionList().clear();
                }
                int beginIndex = message.indexOf('[');
                int endIndex = message.indexOf(']');
                String shortName = message.substring(beginIndex + 1, endIndex);
                Object[] params = { shortName };
                result.rejectValue("question.shortName", "notunique.shortName", params,
                        "Short name is not unique");
                return new ModelAndView("questionEdit");
            } else {
                throw e;
            }
        } else {
            throw e;
        }
    }

    qElement.setCategories(prepareCategories(req, lookupData));
    //      TODO Save 2 times is not good idea. We clear constraints by second update (see implementation)
    //      Changes to attached to session object save automatically. So categories are seved
    //      qaManager.updateFormElement(qElement);

    // after question is saved - return to question listing
    return new ModelAndView(new RedirectView(Constants.QUESTION_LISTING_URI + "?formId=" + formId, true));
}