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.store.rau.storefront.controllers.pages.AccountPageController.java

@RequestMapping(value = "/update-profile", method = RequestMethod.POST)
@RequireHardLogIn//from   www.j ava2s  .co  m
public String updateProfile(final UpdateProfileForm updateProfileForm, final BindingResult bindingResult,
        final Model model, final RedirectAttributes redirectAttributes) throws CMSItemNotFoundException {
    getProfileValidator().validate(updateProfileForm, bindingResult);

    String returnAction = REDIRECT_TO_UPDATE_PROFILE;
    final CustomerData currentCustomerData = customerFacade.getCurrentCustomer();
    final CustomerData customerData = new CustomerData();
    customerData.setTitleCode(updateProfileForm.getTitleCode());
    customerData.setFirstName(updateProfileForm.getFirstName());
    customerData.setLastName(updateProfileForm.getLastName());
    //Mobile Number Update
    customerData.setMobileNumber(updateProfileForm.getMobileNumber());
    customerData.setUid(currentCustomerData.getUid());
    customerData.setDisplayUid(currentCustomerData.getDisplayUid());

    model.addAttribute(TITLE_DATA_ATTR, userFacade.getTitles());

    storeCmsPageInModel(model, getContentPageForLabelOrId(UPDATE_PROFILE_CMS_PAGE));
    setUpMetaDataForContentPage(model, getContentPageForLabelOrId(UPDATE_PROFILE_CMS_PAGE));

    if (bindingResult.hasErrors()) {
        returnAction = setErrorMessagesAndCMSPage(model, UPDATE_PROFILE_CMS_PAGE);
    } else {
        try {
            customerFacade.updateProfile(customerData);
            GlobalMessages.addFlashMessage(redirectAttributes, GlobalMessages.CONF_MESSAGES_HOLDER,
                    "text.account.profile.confirmationUpdated", null);

        } catch (final DuplicateUidException e) {
            bindingResult.rejectValue("email", "registration.error.account.exists.title");
            returnAction = setErrorMessagesAndCMSPage(model, UPDATE_PROFILE_CMS_PAGE);
        }
    }

    model.addAttribute(BREADCRUMBS_ATTR, accountBreadcrumbBuilder.getBreadcrumbs(TEXT_ACCOUNT_PROFILE));
    return returnAction;
}

From source file:com.yqboots.dict.web.controller.DataDictController.java

@PreAuthorize(DataDictPermissions.WRITE)
@RequestMapping(value = WebKeys.MAPPING_IMPORTS, method = RequestMethod.POST)
public String imports(@ModelAttribute(WebKeys.FILE_UPLOAD_FORM) FileUploadForm fileUploadForm,
        @PageableDefault final Pageable pageable, final BindingResult bindingResult, final ModelMap model)
        throws IOException {
    new FileUploadFormValidator().validate(fileUploadForm, bindingResult);
    if (bindingResult.hasErrors()) {
        model.addAttribute(WebKeys.PAGE, dataDictManager.getDataDicts(StringUtils.EMPTY, pageable));
        return VIEW_HOME;
    }// w  w  w  .j  a va  2s  .  co  m

    try (final InputStream inputStream = fileUploadForm.getFile().getInputStream()) {
        dataDictManager.imports(inputStream);
    } catch (XmlMappingException e) {
        bindingResult.rejectValue(WebKeys.FILE, "I0003");
    }

    if (bindingResult.hasErrors()) {
        model.addAttribute(WebKeys.PAGE, dataDictManager.getDataDicts(StringUtils.EMPTY, pageable));
        return VIEW_HOME;
    }

    model.clear();

    return REDIRECT_VIEW_PATH;
}

From source file:com.yqboots.menu.web.controller.MenuItemController.java

@PreAuthorize(MenuItemPermissions.WRITE)
@RequestMapping(value = WebKeys.MAPPING_IMPORTS, method = RequestMethod.POST)
public String imports(@ModelAttribute(WebKeys.FILE_UPLOAD_FORM) FileUploadForm fileUploadForm,
        @PageableDefault final Pageable pageable, final BindingResult bindingResult, final ModelMap model)
        throws IOException {
    new FileUploadFormValidator().validate(fileUploadForm, bindingResult);
    if (bindingResult.hasErrors()) {
        model.addAttribute(WebKeys.PAGE, menuItemManager.getMenuItems(StringUtils.EMPTY, pageable));
        return VIEW_HOME;
    }//from   ww w  .j av a  2s .c om

    try (final InputStream inputStream = fileUploadForm.getFile().getInputStream()) {
        menuItemManager.imports(inputStream);
    } catch (XmlMappingException e) {
        bindingResult.rejectValue(WebKeys.FILE, "I0003");
    }

    if (bindingResult.hasErrors()) {
        model.addAttribute(WebKeys.PAGE, menuItemManager.getMenuItems(StringUtils.EMPTY, pageable));
        return VIEW_HOME;
    }

    model.clear();

    return REDIRECT_VIEW_PATH;
}

From source file:com.zanshang.controllers.web.RegisterController.java

@RequestMapping(value = "/company", method = RequestMethod.POST)
public Object companyRegister(@Valid CompanyForm companyForm, BindingResult result, Map<String, Object> model,
        Locale locale) {/*  ww  w  .ja v  a2 s  . c  o  m*/
    if (result.hasErrors()) {
        model.put("personalForm", new PersonalForm());
        return "2_1_2_2";
    } else {
        EmailAccount account = new EmailAccount(companyForm.getEmail(),
                encoder.encode(companyForm.getPassword()));
        account.setEnabled(false);//????
        account.setNonLocked(false);//??????
        try {
            userDetailsManager.createUser(account);
        } catch (DuplicateKeyException e) {
            model.put("personalForm", new PersonalForm());
            result.rejectValue("email", "register.email.duplicate");
            return "2_1_2_2";
        }
        Map<String, String> mailModel = new HashMap<>();
        mailModel.put("email", companyForm.getEmail());
        emailService.create(companyForm.getEmail(), EmailConstants.EMAIL_ACTIVE_TEMPLATENAME,
                messageSource.getMessage("email.title.activation", null, locale), mailModel);
        Setting setting = new Setting(account.getUid(), companyForm.getContact(), companyForm.getEmail());

        Company company = new Company(account.getUid(), companyForm.getCompanyName(),
                companyForm.getCompanyCode(), companyForm.getContact(), companyForm.getContactPhone(),
                companyForm.getLicense());
        companyService.save(company);
        AuditCompany auditCompany = new AuditCompany(companyForm.getEmail());
        mongoTemplate.save(auditCompany);
        settingService.save(setting);
        ModelAndView mav = new ModelAndView("return");
        mav.addObject("title", messageSource.getMessage("register.email.verification.title", null, locale));
        mav.addObject("content", messageSource.getMessage("register.email.verification.content", null, locale));
        return mav;
    }
}

From source file:com.zanshang.controllers.web.RegisterController.java

@RequestMapping(value = "/personal/email", method = RequestMethod.POST)
public Object emailRegister(@Valid PersonalForm personalForm, BindingResult result, Map<String, Object> model,
        Locale locale, HttpServletRequest request, HttpServletResponse response, @Ticket String ticket) {
    if (result.hasErrors()) {
        model.put("companyForm", new CompanyForm());
        return "2_1_2_2";
    } else {/* www  .  ja  va  2 s  . co  m*/
        EmailAccount account = new EmailAccount(personalForm.getEmail(),
                encoder.encode(personalForm.getPassword()));
        account.setEnabled(false);//????
        try {
            userDetailsManager.createUser(account);
        } catch (DuplicateKeyException e) {
            model.put("companyForm", new CompanyForm());
            result.rejectValue("email", "register.email.duplicate");
            return "2_1_2_2";
        }
        Map<String, String> mailModel = new HashMap<>();
        mailModel.put("email", personalForm.getEmail());
        emailService.create(personalForm.getEmail(), EmailConstants.EMAIL_ACTIVE_TEMPLATENAME,
                messageSource.getMessage("email.title.activation", null, locale), mailModel);
        Person person = new Person(account.getUid(), null);
        Setting setting = new Setting(account.getUid(), personalForm.getName(), personalForm.getEmail());
        personService.save(person);
        settingService.save(setting);
        ModelAndView mav = new ModelAndView("return");
        mav.addObject("title", messageSource.getMessage("register.email.verification.title", null, locale));
        mav.addObject("content", messageSource.getMessage("register.email.verification.content", null, locale));
        //register
        String returnUrl = oAuthUrlProcessService.getParamByKey(BusinessType.LOGIN, ticket, "return");
        if (returnUrl != null && !returnUrl.isEmpty()) {
            handler.setDefaultTargetUrl(returnUrl);
        }
        returnUrl = handler.onRegisterSuccess(request, response);
        mav.addObject("goto", returnUrl);
        return mav;
    }
}

From source file:com.zanshang.controllers.web.RegisterController.java

@RequestMapping(value = "/personal/phone", method = RequestMethod.POST)
public Object phoneRegister(@Valid PersonalForm personalForm, BindingResult result, Map<String, Object> model,
        Locale locale, HttpServletRequest request, HttpServletResponse response, @Ticket String ticket) {
    if (result.hasErrors()) {
        model.put("companyForm", new CompanyForm());
        return "2_1_2_2";
    } else {//from   w  w w.  j  av  a2s. c om
        if (!PhoneValidator.isValid(personalForm.getPhone())) {
            model.put("companyForm", new CompanyForm());
            result.rejectValue("phone", "register.personal.phone.format_error");
            return "2_1_2_2";
        }
        //verify code
        try {
            if (captchaService.verify(personalForm.getPhone(), personalForm.getCode())) {
                phoneService.delete(personalForm.getPhone());
            } else {
                model.put("companyForm", new CompanyForm());
                result.rejectValue("code", "register.connect.phone.error_expire");
                return "2_1_2_2";
            }
        } catch (CaptchaException e) {
            logger.error("Phone Verification Code Not exist." + personalForm.getPhone());
            model.put("companyForm", new CompanyForm());
            result.rejectValue("code", "register.connect.phone.error_expire");
            return "2_1_2_2";
        }

        PhoneAccount account = new PhoneAccount(personalForm.getPhone(),
                encoder.encode(personalForm.getPassword()));
        try {
            userDetailsManager.createUser(account);
        } catch (DuplicateKeyException e) {
            model.put("companyForm", new CompanyForm());
            result.rejectValue("phone", "register.phone.duplicate");
            return "2_1_2_2";
        }
        Setting setting = new Setting(account.getUid(), personalForm.getName(), null);
        settingService.save(setting);
        Person person = new Person(account.getUid(), personalForm.getPhone());
        personService.save(person);
        ModelAndView mav = new ModelAndView("registersuccess");
        //            mav.addObject("title", messageSource.getMessage("register.phone.verification.title", null, locale));
        //            mav.addObject("content", messageSource.getMessage("register.phone.verification.content", null, locale));
        //register
        String returnUrl = oAuthUrlProcessService.getParamByKey(BusinessType.LOGIN, ticket, "return");
        if (returnUrl != null && !returnUrl.isEmpty()) {
            handler.setDefaultTargetUrl(returnUrl);
        }
        returnUrl = handler.onRegisterSuccess(request, response);
        mav.addObject("return", returnUrl);
        return mav;
    }
}

From source file:com.zanshang.controllers.web.RegisterController.java

@RequestMapping(value = CONNECT_PATH + "/{platform}/email", method = RequestMethod.POST)
public Object createConnectEmailAccount(@PathVariable("platform") String platform,
        @Valid PersonalForm personalForm, BindingResult result, Map<String, Object> model,
        HttpServletRequest request, HttpServletResponse response, @Ticket String ticket, Locale locale,
        Device device) {//from   www. j ava 2s .co  m
    model.put("platform", platform);
    if (result.hasErrors()) {
        return "2_1_2_1_3";
    } else {
        EmailAccount account = new EmailAccount(personalForm.getEmail(),
                encoder.encode(personalForm.getPassword()));
        account.setEnabled(true);//????
        try {
            userDetailsManager.createUser(account);
        } catch (DuplicateKeyException e) {
            result.rejectValue("email", "register.email.duplicate");
            return "2_1_2_1_1";
        }
        Map<String, String> mailModel = new HashMap<>();
        mailModel.put("email", personalForm.getEmail());
        emailService.create(personalForm.getEmail(), EmailConstants.EMAIL_ACTIVE_TEMPLATENAME,
                messageSource.getMessage("email.title.activation", null, locale), mailModel);
        Setting setting = new Setting(account.getUid(), personalForm.getName(), personalForm.getEmail());
        settingService.save(setting);
        Person person = new Person(account.getUid(), null);
        personService.save(person);
        connectAccount(platform, ticket, person);
        authenticateUser(personalForm.getEmail(), personalForm.getPassword(), request);
        String targetUrl;
        if (device.isMobile()) {
            targetUrl = "redirect:/projects";
        } else {
            targetUrl = "redirect:/";
        }
        //register
        String returnUrl = oAuthUrlProcessService.getParamByKey(BusinessType.LOGIN, ticket, "return");
        if (returnUrl != null && !returnUrl.isEmpty()) {
            return "redirect:" + returnUrl;
        }
        handler.setDefaultTargetUrl(targetUrl);
        return handler.onRegisterSuccess(request, response);
    }
}

From source file:com.zanshang.controllers.web.RegisterController.java

@RequestMapping(value = CONNECT_PATH + "/{platform}/phone", method = RequestMethod.POST)
public Object createConnectPhoneAccount(@PathVariable("platform") String platform,
        @Valid PersonalPhoneForm personalPhoneForm, BindingResult result, Map<String, Object> model,
        HttpServletRequest request, HttpServletResponse response, @Ticket String ticket) {
    model.put("platform", platform);
    model.put("phone", personalPhoneForm.getPhone());
    if (result.hasErrors()) {
        return "2_1_2_1_4";
    } else {//  w  w  w. j a  v a2  s .co m
        if (!PhoneValidator.isValid(personalPhoneForm.getPhone())) {
            result.rejectValue("phone", "register.personal.phone.format_error");
            return "2_1_2_1_4";
        }
        //verify code
        try {
            if (captchaService.verify(personalPhoneForm.getPhone(), personalPhoneForm.getCode())) {
                //pass
            } else {
                result.rejectValue("code", "register.connect.phone.error_expire");
                return "2_1_2_1_4";
            }
        } catch (CaptchaException e) {
            logger.error("Phone Verification Code Not exist." + personalPhoneForm.getPhone());
            result.rejectValue("code", "register.connect.phone.error_expire");
            return "2_1_2_1_4";
        }

        //create phone account
        PhoneAccount account = new PhoneAccount(personalPhoneForm.getPhone(),
                encoder.encode(personalPhoneForm.getPassword()));
        try {
            userDetailsManager.createUser(account);
        } catch (DuplicateKeyException e) {
            logger.error("Register with same phone number which already exist in system."
                    + personalPhoneForm.getPhone());
            result.rejectValue("phone", "register.phone.duplicate");
            return "2_1_2_1_4";
        }
        Setting setting = new Setting(account.getUid(), personalPhoneForm.getName(), null);
        settingService.save(setting);
        Person person = new Person(account.getUid(), personalPhoneForm.getPhone());
        personService.save(person);
        authenticateUser(personalPhoneForm.getPhone(), personalPhoneForm.getPassword(), request);
        connectAccount(platform, ticket, person);
        //register
        String returnUrl = oAuthUrlProcessService.getParamByKey(BusinessType.LOGIN, ticket, "return");
        if (returnUrl != null && !returnUrl.isEmpty()) {
            return "redirect:" + returnUrl;
        }
        handler.setDefaultTargetUrl("redirect:/");
        return handler.onRegisterSuccess(request, response);
    }
}

From source file:cz.strmik.cmmitool.web.controller.MethodController.java

@RequestMapping(method = RequestMethod.POST, value = "/save-method.do")
public String saveMethod(@ModelAttribute(Attribute.METHOD) Method method, BindingResult result,
        ModelMap modelMap) {/*from ww w.  jav a 2  s .  com*/
    if (StringUtils.isEmpty(method.getName())) {
        result.rejectValue("name", "field-required");
        return METHOD_FORM;
    }
    if (method.isNew()) {
        method = methodService.createMethod(method);
    } else {
        method = methodService.updateMethod(method);
    }
    modelMap.addAttribute(Attribute.METHOD, method);
    modelMap.addAttribute(Attribute.MODEL_TREE,
            TreeGenerator.methodToTree(method, EDIT_SCALE, CHOOSE_RATING, REMOVE_SCALE));
    return METHOD_SCALES;
}

From source file:cz.strmik.cmmitool.web.controller.ModelController.java

@RequestMapping(method = RequestMethod.POST, value = "/add-group-{modelId}.do")
public String addGroup(@PathVariable("modelId") Long modelId,
        @ModelAttribute(Attribute.GROUP) ProcessGroup group, BindingResult result, ModelMap modelMap,
        SessionStatus status) {/*w w  w. java 2s .  c o m*/
    if (StringUtils.isEmpty(group.getName())) {
        result.rejectValue("name", "field-required");
        return MODEL_GROUPS;
    }
    group.setModel(modelDao.read(modelId));
    modelMap.addAttribute(Attribute.MODEL, modelService.addGroup(group));
    modelMap.addAttribute(Attribute.GROUP, new ProcessGroup());
    modelMap.addAttribute("saved", Boolean.TRUE);
    return MODEL_GROUPS;
}