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:com.virtusa.akura.staff.controller.StaffQualificationsController.java

/**
 * Add staffProfessional details in to system.
 * /*from ww  w .j a v  a2s .com*/
 * @param wrapperQualification - {@link wrapperQualification}
 * @param bindingResult - {@link BindingResult}
 * @param model - {@link ModelMap}
 * @return view of the staff qualifications details.
 * @throws AkuraAppException - throw detailed exception.
 */
@RequestMapping(value = SAVE_STAFF_PROFESSIONAL_QUALIFICATION, method = RequestMethod.POST)
public String saveStaffProfessionalQualificationDetails(
        @ModelAttribute(MODEL_ATT_WRAPPER_QUALIFICATION) WrapperQualification wrapperQualification,
        BindingResult bindingResult, ModelMap model) throws AkuraAppException {

    StaffProfessional staffProfessional = wrapperQualification.getStaffProfessional();
    staffProfessionalValidator.validate(staffProfessional, bindingResult);

    if (bindingResult.hasErrors()) {
        return VIEW_STAFF_MEMBER_QUALIFICATION;
    } else {
        boolean isNew = false;
        // save professional qualification
        try {
            if (staffProfessional.getStaffProfessionalId() != 0) {
                staffService.updateStaffProfessional(staffProfessional);

            } else {
                isNew = true;
                staffService.addStaffProfessional(staffProfessional);
            }
        } catch (AkuraAppException e) {
            if (e.getCause() instanceof DataIntegrityViolationException) {
                bindingResult.rejectValue(ERROR_PROFESSIONAL_QUALIFICATION_ID, STQ_UI_DUPLICATE_DESCRIPTION);
            }
            if (isNew) {
                staffProfessional.setStaffProfessionalId(0);
                wrapperQualification.setStaffProfessional(staffProfessional);
                model.addAttribute(MODEL_ATT_WRAPPER_QUALIFICATION, wrapperQualification);
            }
            return VIEW_STAFF_MEMBER_QUALIFICATION;
        }
    }
    return REDIRECT_STAFF_QUALIFICATIONS;
}

From source file:com.spd.ukraine.lucenewebsearch1.web.IndexingController.java

/**
 * Method used to initiate search of given keywords in data base.
 *
 * @param keywords keywords/*from   ww  w.  j  a  va  2 s  . c om*/
 * @param result used to detect errors in form
 * @param request for future code
 * @param errors for future code
 * @return model of the success view or that of the registration page
 */
@RequestMapping(value = { "/search" }) //,method = RequestMethod.POST)
public ModelAndView search(@ModelAttribute("q") WebPage keywords, BindingResult result, WebRequest request,
        Errors errors) {
    System.out.println("search for keywords q = " + keywords.getTitle());
    foundSearchResults.clear();
    foundSearchResultsSorted.clear();
    if (!result.hasErrors()) {
        System.out.println("!result.hasErrors()");
        try {
            foundSearchResults = new ArrayList<>(searchPhrase(keywords.getTitle(), CONTENT_FIELD));
        } catch (IOException | ParseException ex) {
            Logger.getLogger(IndexingController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    if (foundSearchResults.isEmpty()) {
        System.out.println("found.isEmpty()");
        result.rejectValue("title", "label.not.found");
    }
    if (result.hasErrors()) {
        System.out.println("result.hasErrors()");
        ModelAndView model = new ModelAndView("root");
        model.addObject("q", keywords);
        return model;
    } else {
        lastPageResultIndex = RESULTS_PER_PAGE;
        ModelAndView model = new ModelAndView("results");
        model.addObject("found", returnSubListOfSortedList()); //foundSearchResults.subList(0, lastPageResultIndex));
        model.addObject("sorting", returnInvertedSortingOrder());
        return model;
    }
}

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

@RequestMapping(value = "/add/save", method = RequestMethod.POST)
@RequireHardLogIn/*from ww  w.  j ava2 s  .  c  o  m*/
public String saveNewPermissionDetails(@Valid final B2BPermissionForm b2BPermissionForm,
        final BindingResult bindingResult, final Model model, final RedirectAttributes redirectModel)
        throws CMSItemNotFoundException, ParseException {
    b2BPermissionFormValidator.validate(b2BPermissionForm, bindingResult);
    if (bindingResult.hasErrors()) {
        model.addAttribute(b2BPermissionForm);
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return (ResponsiveUtils.isResponsive()) ? getAddErrorPermissionPage(model)
                : getAddPermissionPage(model);
    }

    final B2BPermissionData b2BPermissionData = populateB2BPermissionDataFromForm(b2BPermissionForm);
    try {
        b2bPermissionFacade.addPermission(b2BPermissionData);
    } catch (final Exception e) {
        LOG.warn("Exception while saving the permission details " + e);
        if (e.getCause() instanceof InterceptorException && ((InterceptorException) e.getCause())
                .getInterceptor().getClass().equals(UniqueAttributesInterceptor.class)) {
            model.addAttribute(b2BPermissionForm);
            bindingResult.rejectValue("code", "text.company.managePermissions.code.exists.error.title");
            GlobalMessages.addErrorMessage(model, "form.global.error");
            return (ResponsiveUtils.isResponsive()) ? getAddErrorPermissionPage(model)
                    : getAddPermissionPage(model);
        }
    }

    storeCmsPageInModel(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    final List<Breadcrumb> breadcrumbs = myCompanyBreadcrumbBuilder.createManagePermissionsBreadcrumb();
    model.addAttribute("breadcrumbs", breadcrumbs);
    GlobalMessages.addFlashMessage(redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER,
            "text.confirmation.permission.created");
    return String.format(REDIRECT_TO_PERMISSION_DETAILS, urlEncode(b2BPermissionData.getCode()));
}

From source file:com.epam.training.storefront.controllers.pages.AbstractRegisterPageController.java

/**
 * This method takes data from the registration form and create a new customer account and attempts to log in using
 * the credentials of this new user.//from ww w.  ja v  a  2  s  .c om
 * 
 * @param referer
 * @param form
 * @param bindingResult
 * @param model
 * @param request
 * @param response
 * @return true if there are no binding errors or the account does not already exists.
 * @throws CMSItemNotFoundException
 */
protected String processRegisterUserRequest(final String referer, final RegisterForm form,
        final BindingResult bindingResult, final Model model, final HttpServletRequest request,
        final HttpServletResponse response) throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
        model.addAttribute(new LoginForm());
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
    }

    final RegisterData data = new RegisterData();
    data.setFirstName(form.getFirstName());
    data.setLastName(form.getLastName());
    data.setLogin(form.getEmail());
    data.setPassword(form.getPwd());
    data.setTitleCode(form.getTitleCode());
    try {
        getCustomerFacade().register(data);
        getAutoLoginStrategy().login(form.getEmail(), form.getPwd(), request, response);

        getSubscriptionFacade().updateProfile(new HashMap<String, String>());

        RequestContextUtils.getOutputFlashMap(request).put(GlobalMessages.CONF_MESSAGES_HOLDER,
                Collections.singletonList("registration.confirmation.message.title"));
    } catch (final DuplicateUidException e) {
        LOG.warn("registration failed: " + e);
        model.addAttribute(new LoginForm());
        bindingResult.rejectValue("email", "registration.error.account.exists.title");
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
    } catch (final SubscriptionFacadeException e) {
        LOG.warn(String.format("Creating new subscription billing profile for user %s failed", form.getEmail()),
                e);
        model.addAttribute(new LoginForm());
        GlobalMessages.addErrorMessage(model, "registration.error.subscription.billing.profil");
        return handleRegistrationError(model);
    }

    return REDIRECT_PREFIX + getSuccessRedirect(request, response);
}

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

protected String saveCostCenter(@Valid final B2BCostCenterForm b2BCostCenterForm,
        final BindingResult bindingResult, final Model model, final RedirectAttributes redirectModel)
        throws CMSItemNotFoundException {
    if (bindingResult.hasFieldErrors()) {
        model.addAttribute(b2BCostCenterForm);
        return addCostCenter(model);
    }//w  w  w . j a  v a2s .com
    final B2BCostCenterData b2BCostCenterData = populateB2BCostCenterDataFromForm(b2BCostCenterForm);
    try {
        b2bCostCenterFacade.addCostCenter(b2BCostCenterData);
    } catch (final Exception e) {
        LOG.warn("Exception while saving the cost center details " + e);
        model.addAttribute(b2BCostCenterForm);
        bindingResult.rejectValue("code", "text.company.costCenter.code.exists.error.title");
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return addCostCenter(model);
    }
    storeCmsPageInModel(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    final List<Breadcrumb> breadcrumbs = myCompanyBreadcrumbBuilder.createManageCostCenterBreadCrumbs();
    breadcrumbs.add(new Breadcrumb(ADD_COSTCENTER_URL, getMessageSource()
            .getMessage("text.company.costCenter.addPage", null, getI18nService().getCurrentLocale()), null));
    model.addAttribute("breadcrumbs", breadcrumbs);
    GlobalMessages.addFlashMessage(redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER,
            "text.company.costCenter.create.success");
    return String.format(REDIRECT_TO_COSTCENTER_DETAILS, urlEncode(b2BCostCenterData.getCode()));
}

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

protected String saveCostCenter(@Valid final B2BCostCenterForm b2BCostCenterForm,
        final BindingResult bindingResult, final Model model, final RedirectAttributes redirectModel)
        throws CMSItemNotFoundException {
    if (bindingResult.hasFieldErrors()) {
        model.addAttribute(b2BCostCenterForm);
        return addCostCenter(model);
    }/*from w  w w. java2 s.co  m*/
    final B2BCostCenterData b2BCostCenterData = populateB2BCostCenterDataFromForm(b2BCostCenterForm);
    try {
        b2bCommerceCostCenterFacade.addCostCenter(b2BCostCenterData);
    } catch (final Exception e) {
        LOG.warn("Exception while saving the cost center details " + e);
        model.addAttribute(b2BCostCenterForm);
        bindingResult.rejectValue("code", "text.company.costCenter.code.exists.error.title");
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return addCostCenter(model);
    }
    storeCmsPageInModel(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    setUpMetaDataForContentPage(model, getContentPageForLabelOrId(ORGANIZATION_MANAGEMENT_CMS_PAGE));
    final List<Breadcrumb> breadcrumbs = myCompanyBreadcrumbBuilder.createManageCostCenterBreadCrumbs();
    breadcrumbs.add(new Breadcrumb(ADD_COSTCENTER_URL, getMessageSource()
            .getMessage("text.company.costCenter.addPage", null, getI18nService().getCurrentLocale()), null));
    model.addAttribute("breadcrumbs", breadcrumbs);
    GlobalMessages.addFlashMessage(redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER,
            "text.company.costCenter.create.success");
    return String.format(REDIRECT_TO_COSTCENTER_DETAILS, urlEncode(b2BCostCenterData.getCode()));
}

From source file:com.epam.cme.storefront.controllers.pages.AbstractRegisterPageController.java

/**
 * This method takes data from the registration form and create a new customer account and
 * attempts to log in using the credentials of this new user.
 * /* w w  w  . j a  va2 s  .co  m*/
 * @param referer
 * @param form
 * @param bindingResult
 * @param model
 * @param request
 * @param response
 * @return true if there are no binding errors or the account does not already exists.
 * @throws CMSItemNotFoundException
 */
protected String processRegisterUserRequest(final String referer, final RegisterForm form,
        final BindingResult bindingResult, final Model model, final HttpServletRequest request,
        final HttpServletResponse response) throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
        model.addAttribute(new LoginForm());
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
    }
    final RegisterData data = new RegisterData();
    data.setFirstName(form.getFirstName());
    data.setLastName(form.getLastName());
    data.setLogin(form.getEmail());
    data.setPassword(form.getPwd());
    data.setTitleCode(form.getTitleCode());
    try {
        getCustomerFacade().register(data);
        getAutoLoginStrategy().login(form.getEmail(), form.getPwd(), request, response);
        getSubscriptionFacade().updateProfile(new HashMap<String, String>());
        RequestContextUtils.getOutputFlashMap(request).put(GlobalMessages.CONF_MESSAGES_HOLDER,
                Collections.singletonList("registration.confirmation.message.title"));
    } catch (final DuplicateUidException e) {
        LOG.warn("registration failed: " + e);
        model.addAttribute(new LoginForm());
        bindingResult.rejectValue("email", "registration.error.account.exists.title");
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
    } catch (final SubscriptionFacadeException e) {
        LOG.warn(String.format("Creating new subscription billing profile for user %s failed", form.getEmail()),
                e);
        model.addAttribute(new LoginForm());
        GlobalMessages.addErrorMessage(model, "registration.error.subscription.billing.profil");
        return handleRegistrationError(model);
    }
    return REDIRECT_PREFIX + getSuccessRedirect(request, response);
}

From source file:com.epam.cme.storefront.controllers.pages.AbstractRegisterPageController.java

protected String processRegisterCmeUserRequest(final String referer, final RegisterForm form,
        final BindingResult bindingResult, final Model model, final HttpServletRequest request,
        final HttpServletResponse response) throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
        model.addAttribute(new LoginForm());
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
    }//  www .  ja v  a2  s . c o  m
    final CmeRegisterData data = new CmeRegisterData();
    data.setFirstName(form.getFirstName());
    data.setLastName(form.getLastName());
    data.setOrganizationsIds(form.getOrganizations());
    data.setLogin(form.getEmail());
    data.setPassword(form.getPwd());
    data.setTitleCode(form.getTitleCode());
    try {
        getBlockableCustomerFacade().register(data);
        getAutoLoginStrategy().login(form.getEmail(), form.getPwd(), request, response);
        getSubscriptionFacade().updateProfile(new HashMap<String, String>());
        RequestContextUtils.getOutputFlashMap(request).put(GlobalMessages.CONF_MESSAGES_HOLDER,
                Collections.singletonList("registration.confirmation.message.title"));
    } catch (final DuplicateUidException e) {
        LOG.warn("registration failed: " + e);
        model.addAttribute(new LoginForm());
        bindingResult.rejectValue("email", "registration.error.account.exists.title");
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
    } catch (final SubscriptionFacadeException e) {
        LOG.warn(String.format("Creating new subscription billing profile for user %s failed", form.getEmail()),
                e);
        model.addAttribute(new LoginForm());
        GlobalMessages.addErrorMessage(model, "registration.error.subscription.billing.profil");
        return handleRegistrationError(model);
    }
    return REDIRECT_PREFIX + getSuccessRedirect(request, response);
}

From source file:at.fh.swenga.firefighters.controller.FireFighterController.java

@RequestMapping(value = "registration", method = RequestMethod.POST)
public ModelAndView registerUserAccount(@ModelAttribute("user") @Valid UserDto accountDto, BindingResult result,
        WebRequest request, Errors errors) {
    User registered = new User();
    if (!result.hasErrors()) {
        registered = createUserAccount(accountDto, result);

    }/*from  w ww  .j av a 2 s .  c o  m*/
    if (registered == null) {
        result.rejectValue("email", "message.regError");
    }
    if (result.hasErrors()) {
        return new ModelAndView("registration", "user", accountDto);
    } else {
        return new ModelAndView("successRegister", "user", accountDto);
    }
}

From source file:de.hybris.platform.acceleratorstorefrontcommons.controllers.pages.AbstractRegisterPageController.java

/**
 * This method takes data from the registration form and create a new customer account and attempts to log in using
 * the credentials of this new user./*from  ww  w  . java2 s  .  c  o  m*/
 *
 * @return true if there are no binding errors or the account does not already exists.
 * @throws CMSItemNotFoundException
 */
protected String processRegisterUserRequest(final String referer, final RegisterForm form,
        final BindingResult bindingResult, final Model model, final HttpServletRequest request,
        final HttpServletResponse response, final RedirectAttributes redirectModel)
        throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
        model.addAttribute(form);
        model.addAttribute(new LoginForm());
        model.addAttribute(new GuestForm());
        GlobalMessages.addErrorMessage(model, FORM_GLOBAL_ERROR);
        return handleRegistrationError(model);
    }

    final RegisterData data = new RegisterData();
    data.setFirstName(form.getFirstName());
    data.setLastName(form.getLastName());
    data.setLogin(form.getEmail());
    data.setPassword(form.getPwd());
    data.setTitleCode(form.getTitleCode());
    try {
        getCustomerFacade().register(data);
        getAutoLoginStrategy().login(form.getEmail().toLowerCase(), form.getPwd(), request, response);

        GlobalMessages.addFlashMessage(redirectModel, GlobalMessages.CONF_MESSAGES_HOLDER,
                "registration.confirmation.message.title");
    } catch (final DuplicateUidException e) {
        LOGGER.warn("registration failed: " + e);
        model.addAttribute(form);
        model.addAttribute(new LoginForm());
        model.addAttribute(new GuestForm());
        bindingResult.rejectValue("email", "registration.error.account.exists.title");
        GlobalMessages.addErrorMessage(model, FORM_GLOBAL_ERROR);
        return handleRegistrationError(model);
    }

    return REDIRECT_PREFIX + getSuccessRedirect(request, response);
}