public static void checkBreakfast(Breakfast breakfast, BindingResult result)
      throws FormException {
    Date date = breakfast.getDate();
    String name = breakfast.getName();
    String commentaire = breakfast.getComment();
    List<String> errors = new ArrayList<>();
    Map<String, FormExceptionFeedBack> feedBacks = new HashMap<>();
    if (date == null) {
      errors.add("La date de l'évènement est obligatoire.");
      feedBacks.put("Date", FormExceptionFeedBack.ERROR);
    }
    if (name == null || name == "") {
      errors.add("Le nom est obligatoire.");
      feedBacks.put("Name", FormExceptionFeedBack.ERROR);
    }
    if (commentaire == null || commentaire == "") {
      feedBacks.put("Comment", FormExceptionFeedBack.WARNING);
    }

    if (result.hasFieldErrors("name")) {
      errors.add(result.getFieldError("name").getDefaultMessage());
      feedBacks.put("Name", FormExceptionFeedBack.ERROR);
    }
    if (result.hasFieldErrors("comment")) {
      errors.add(result.getFieldError("comment").getDefaultMessage());
      feedBacks.put("Comment", FormExceptionFeedBack.ERROR);
    }
    if (errors.size() > 0 || result.hasErrors())
      throw new FormException("Erreur sauvegarde membre", feedBacks, errors);
  }
  @RequestMapping("adicionaTarefa")
  public String adiciona(@Valid Tarefa tarefa, BindingResult result) {

    if (result.hasFieldErrors("descricao")) return "tarefa/formulario";

    dao.adiciona(tarefa);
    return "tarefa/adicionada";
  }
  @Test
  public void shouldHaveNoErrorsWhenBindingResultHasNoFieldErrors() throws Exception {
    given(bindingResult.hasFieldErrors()).willReturn(false);

    ContactResult contactResult = contactService.processContactForm(contactForm(), bindingResult);

    assertThat(contactResult.isHasErrors()).isFalse();
  }
  @Test
  public void shouldReturnSuccessfulResult() throws Exception {
    given(bindingResult.hasFieldErrors()).willReturn(false);

    ContactResult contactResult = contactService.processContactForm(contactForm(), bindingResult);

    assertThat(contactResult.isSuccess()).isTrue();
  }
 private void convertPasswordError(BindingResult result) {
   for (ObjectError error : result.getGlobalErrors()) {
     String msg = error.getDefaultMessage();
     if (SignupForm.PASSWORD_MISMATCH.equals(msg)) {
       if (!result.hasFieldErrors("repeatPassword")) {
         result.rejectValue("repeatPassword", SignupForm.PASSWORD_MISMATCH);
       }
     }
   }
 }
Example #6
0
  @RequestMapping(value = "/lisaa", method = RequestMethod.POST)
  public String createNewItem(
      @Valid @ModelAttribute Item item, BindingResult bindingResult, @PathVariable String id) {

    if (bindingResult.hasFieldErrors("name") || bindingResult.hasFieldErrors("amount")) {
      return "tavarat";
    }
    ItemList itemList = itemListRepository.findById(id);
    if (!itemList.getPerson().getId().equals(getPersonId())) {
      throw new IllegalAccessError();
    }
    item.setItemList(itemList);
    item.setReserved(0);
    item.setReservedBy(new ArrayList<>());
    item.setId(null);
    item = itemRepository.save(item);

    itemList.getItems().add(item);
    itemListRepository.save(itemList);

    return "redirect:/tapahtumat/" + id;
  }
  /**
   * 保存文章
   *
   * @param article
   * @param result
   * @param request
   * @return
   */
  @RequiresUser
  @ResponseBody
  @RequestMapping(
      value = "save",
      method = {RequestMethod.POST, RequestMethod.GET})
  public ReturnDto save(
      @Valid @ModelAttribute("article") Article article,
      BindingResult result,
      ServletRequest request) {
    ReturnDto returnDto = new ReturnDto();
    if (result.hasFieldErrors()) {
      returnDto.setMessage("参数验证出现异常:" + result.getFieldError().getDefaultMessage());
      returnDto.setSuccess(false);
    } else {
      returnDto = articleService.saveOrModify(article);
    }
    String locate = request.getParameter("locate");

    return returnDto;
  }
Example #8
0
 @RequestMapping("/handleupdate")
 public ModelAndView handleUpdate(@Valid User user, BindingResult result) {
   ModelAndView mv = new ModelAndView();
   if (result.hasFieldErrors()) {
     List<FieldError> list1 = result.getFieldErrors();
     List<String> list = new LinkedList<String>();
     Iterator<FieldError> it = list1.iterator();
     while (it.hasNext()) {
       FieldError f = (FieldError) it.next();
       String s = f.getDefaultMessage();
       list.add(s);
     }
     mv.addObject("errors", list);
     mv.setViewName("error");
     return mv;
   } else {
     service.updateUser(user);
     mv.setView(new RedirectView("/"));
     return mv;
   }
 }
 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);
   }
   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()));
 }
  @RequestMapping("adicionaTarefa")
  public String adiciona(@Valid Tarefa tarefa, BindingResult result) {
    // o erro passou a ser guardado no objeto BindingResult , variável
    // result

    /*
     * if(tarefa.getDescricao()==null ||
     * tarefa.getDescricao().toString().equals("")){ return
     * "tarefa/formulario.jsp?erro=1"; } ESTE IF DE VALIDAÇÃO É SUBSTITUIDO
     * PELO @valid que é declarado como @NotNull @Size (min=5) na classe
     * Tarefa. é possível através do "bean validation"
     */

    if (result.hasFieldErrors("descricao")) {
      return "tarefa/formulario";
      // hasErrors() - pode ser utilizado afim de capturar erros mais
      // genéricos
    }
    // JdbcTarefaDao dao = new JdbcTarefaDao();
    dao.adiciona(tarefa);
    return "redirect:listaTarefas?inc=1";
  }
 private void givenThatFieldErrorsArePresent() {
   List<FieldError> fieldErrors = fieldErrors();
   given(bindingResult.hasFieldErrors()).willReturn(true);
   given(bindingResult.getFieldErrors()).willReturn(fieldErrors);
 }