Example usage for org.springframework.validation BindingResult hasErrors

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

Introduction

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

Prototype

boolean hasErrors();

Source Link

Document

Return if there were any errors.

Usage

From source file:com.github.iexel.fontus.web.rest.GlobalControllerAdviceRest.java

private String[] fetchValidationErrorsAsStringArray(BindingResult bindingResult, Locale locale) {

    String[] errorArray = null;/*  w  ww.  j a va 2s .co  m*/

    if (bindingResult.hasErrors()) {
        List<FieldError> errors = bindingResult.getFieldErrors();
        errorArray = new String[errors.size() * 2];
        int counter = 0;

        for (FieldError error : errors) {
            errorArray[counter] = error.getField();
            errorArray[counter + 1] = messageSource.getMessage(error, locale);
            counter += 2;
        }
    }
    return errorArray;
}

From source file:com.trenako.web.controllers.CommentsController.java

@RequestMapping(method = RequestMethod.POST)
public String postComment(@ModelAttribute @Valid CommentForm commentForm, BindingResult bindingResult,
        ModelMap model, RedirectAttributes redirectAtts) {

    if (bindingResult.hasErrors()) {
        model.addAttribute(commentForm);
        return "redirect:/rollingstocks/{slug}";
    }/*from ww  w .ja  v  a  2s  .  c o  m*/

    RollingStock rs = rsService.findBySlug(commentForm.getRsSlug());
    service.postComment(rs, commentForm.buildComment(now()));

    COMMENT_POSTED_MSG.appendToRedirect(redirectAtts);
    return "redirect:/rollingstocks/{slug}";
}

From source file:controllers.customer.ComplaintCustomerController.java

@RequestMapping(value = "/create", method = RequestMethod.POST, params = "save")
public ModelAndView save(@Valid ComplaintForm complaintForm, BindingResult binding) {
    ModelAndView result;//from www  .  j a  v  a2s .com

    if (binding.hasErrors()) {
        result = createEditModelAndView(complaintForm);
        System.out.println(binding.getAllErrors());
    } else {
        try {
            Complaint complaint;
            complaint = complaintService.reconstruct(complaintForm);
            complaintService.save(complaint);
            result = new ModelAndView("redirect:listComplaintCustomerIdNotSolved.do");
        } catch (Throwable oops) {

            result = createEditModelAndView(complaintForm, "complaint.error.operation");
            oops.printStackTrace();
        }
    }
    return result;
}

From source file:io.curly.artifact.web.ArtifactResourceController.java

/**
 * @param body          json body// w  ww.  j  ava  2s  . c o  m
 * @param user          authentication user
 * @param bindingResult errors results of artifact
 * @return no content if saved, bad request if binding results has errors
 */
@RequestMapping(method = { POST }, consumes = MediaType.APPLICATION_JSON_VALUE)
public DeferredResult<HttpEntity<?>> saveResource(@Valid @RequestBody ArtifactReqBody body,
        @GitHubAuthentication User user, BindingResult bindingResult) {

    if (bindingResult.hasErrors()) {
        return defer(Observable.just(new ResponseEntity<>(new ModelErrors(bindingResult), BAD_REQUEST)));
    }

    artifactService.save(body.toArtifact(user), user);
    return defer(Observable.just(new ResponseEntity<>(CREATED)));
}

From source file:nl.enovation.addressbook.cqrs.webui.controllers.ContactsController.java

@RequestMapping(value = "search", method = RequestMethod.POST)
public String search(@ModelAttribute("searchForm") SearchForm searchForm, Model model,
        BindingResult bindingResult) {
    if (bindingResult.hasErrors()) {
        return "contacts/list";
    }/*  w  w  w. j  a  va 2s  . c  om*/
    List<ContactEntry> contacts = contactRepositoryImpl.searchForNames(searchForm.getSearchValue());
    model.addAttribute("contacts", contacts);
    return "contacts/list";
}

From source file:fi.nls.fileservice.web.admin.controller.DatasetAdminController.java

@RequestMapping(value = "/tuotteet/", method = RequestMethod.POST)
public String saveDataset(@ModelAttribute("dataset") @Valid Dataset dataset, BindingResult result,
        Model model) {/*  ww  w .ja v a  2  s.co m*/
    if (result.hasErrors()) {
        return "admin/dataset_edit";
    }

    datasetService.saveDataset(dataset);
    return "redirect:/hallinta/tuotteet/" + dataset.getName();
}

From source file:forum.controller.UserController.java

@RequestMapping(value = "/connexion", method = RequestMethod.POST)
public String connexion(@Valid @ModelAttribute("util") Utilisateur u, BindingResult result, HttpSession session,
        Model model) {/*  w w w . j  a  va 2  s.co  m*/

    if (result.hasErrors()) {
        model.addAttribute("forums", forumService.findAll());
        return "forum/list_forum";
    }

    Utilisateur utilisateur = userService.findOneByLogin(u.getLogin());
    if (utilisateur != null && utilisateur.getMdp().equals(u.getMdp())) {
        session.setAttribute("isLogged", true);
        session.setAttribute("userLogin", utilisateur.getLogin());
    }

    return "redirect:/";
}

From source file:net.triptech.metahive.web.CategoryController.java

@RequestMapping(method = RequestMethod.PUT)
@PreAuthorize("hasRole('ROLE_ADMIN')")
public String update(@Valid Category category, BindingResult bindingResult, Model uiModel,
        HttpServletRequest request) {/*from  www.  j a  v  a  2s .c o m*/

    if (bindingResult.hasErrors()) {
        uiModel.addAttribute("category", category);

        FlashScope.appendMessage(getMessage("metahive_object_validation", Category.class), request);
        return "categories/update";
    }
    uiModel.asMap().clear();
    category.merge();

    FlashScope.appendMessage(getMessage("metahive_edit_complete", Category.class), request);

    return "redirect:/lists";
}

From source file:org.smigo.species.varieties.VarietyController.java

@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/rest/variety", method = RequestMethod.POST)
public Object addVariety(@Valid @RequestBody Variety variety, BindingResult result,
        HttpServletResponse response, @AuthenticationPrincipal AuthenticatedUser user) {
    if (result.hasErrors()) {
        response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
        return result.getAllErrors();
    }//  w w w .  ja  va 2 s .  c  o m
    variety.setUserId(user.getId());
    final int id = varietyDao.addVariety(variety);
    response.setStatus(HttpServletResponse.SC_CREATED);
    return id;
}

From source file:cherry.example.web.applied.ex50.AppliedEx52ControllerImpl.java

private boolean hasErrors(AppliedEx52Form form, BindingResult binding) {

    // ??//from ww  w  . ja v  a2 s. c o m
    if (binding.hasErrors()) {
        return true;
    }

    // ?
    if (form.getDt() == null && form.getTm() != null) {
        LogicalErrorUtil.rejectValue(binding, Prop.Dt.getName(), LogicalError.RequiredWhen, Prop.Dt.resolve(),
                Prop.Tm.resolve());
    }

    if (binding.hasErrors()) {
        return true;
    }

    // ??

    return false;
}