Example usage for org.springframework.validation BindingResult getGlobalErrors

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

Introduction

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

Prototype

List<ObjectError> getGlobalErrors();

Source Link

Document

Get all global errors.

Usage

From source file:cz.muni.fi.pa165.mvc.controllers.PlayerController.java

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String edit(@Valid @ModelAttribute("playerEdit") PlayerCreateDTO formBean, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("edit(playerEdit={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        log.debug("some errror");
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);

        }// w  w w .  ja  va2s  .  c  o  m
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "player/edit";

    }

    if (formBean.getName().equals("")) {
        redirectAttributes.addFlashAttribute("alert_warning",
                "Player updating failed - Player name cannot be emty string!");
        return "redirect:" + uriBuilder.path("/player/edit").build().encode().toUriString();
    }

    if (formBean.getDateOfBirth().compareTo(new Date()) > 0) {
        redirectAttributes.addFlashAttribute("alert_warning",
                "Player updaeting failed - Player datee cannot be bigger than actuall date!");
        return "redirect:" + uriBuilder.path("/player/edit").build().encode().toUriString();
    }
    playerFacade.updatePlayer(convertPlayerCreateDTO(formBean));

    //report success
    redirectAttributes.addFlashAttribute("alert_success", "Player was edited");
    return "redirect:" + uriBuilder.path("/player/list").toUriString();

}

From source file:cz.muni.fi.pa165.mvc.controllers.PlayerController.java

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("playerCreate") PlayerCreateDTO formBean,
        BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
        UriComponentsBuilder uriBuilder) {
    log.debug("create(playerCreate={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        log.debug("some errror");
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);

        }//from  w  w w.j a  va 2s  .  c om
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "player/new";

    }

    //create player
    if (formBean.getName().equals("")) {
        redirectAttributes.addFlashAttribute("alert_warning",
                "Player creation failed - Player name cannot be emty string!");
        return "redirect:" + uriBuilder.path("/player/new").build().encode().toUriString();
    }

    if (formBean.getDateOfBirth().compareTo(new Date()) > 0) {
        redirectAttributes.addFlashAttribute("alert_warning",
                "Player creation failed - Player date cannot be bigger than actuall date!");
        return "redirect:" + uriBuilder.path("/player/new").build().encode().toUriString();
    }

    Long id = playerFacade.createPlayer(convertPlayerCreateDTO(formBean));

    //report success
    redirectAttributes.addFlashAttribute("alert_success",
            "Player " + playerFacade.findById(id).toString() + " was created");
    return "redirect:" + uriBuilder.path("/player/list").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.mvc.controllers.DestinationController.java

/**
 * Creates a new Destination/*from ww  w.  ja v  a 2 s  .c om*/
 * @param model display data
 * @return jsp page
 */
@RequestMapping(method = RequestMethod.POST, value = "/create")
public String create(@Valid @ModelAttribute("destinationCreate") DestinationCreationalDTO formBean,
        BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
        UriComponentsBuilder uriBuilder) {
    if (bindingResult.hasErrors()) {
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        return "destination/new";
    }
    try {
        if (destinationFacade.getDestinationWithLocation(formBean.getLocation()) == null) {
            bindingResult.addError(new FieldError("destinationCreate", "location",
                    "Destination was not created because it already exists."));
            model.addAttribute("location_error", true);
            return "destination/new";
        }

        Long id = destinationFacade.createDestination(formBean);
        redirectAttributes.addFlashAttribute("alert_info", "Destination with id: " + id + " was created");
    } catch (Exception ex) {
        model.addAttribute("alert_danger", "Destination was not created because of some unexpected error");
        redirectAttributes.addFlashAttribute("alert_danger",
                "Destination was not created because it already exists.");
    }
    return "redirect:" + uriBuilder.path("/destination").toUriString();
}

From source file:cz.muni.fi.mvc.controllers.DestinationController.java

/**
 * Updates destination/*from w w w .  ja v  a 2  s.  c om*/
 *
 * @param id, modelAttribute, bindingResult, model, redirectAttributes, uriBuilder
 * @return JSP page
 */
@RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
public String updateDestination(@PathVariable("id") long id,
        @Valid @ModelAttribute("destination") UpdateDestinationLocationDTO updatedDestination,
        BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
        UriComponentsBuilder uriBuilder) {

    if (bindingResult.hasErrors()) {
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        return "destination/edit";
    }

    if ((updatedDestination.getLocation()).equals("")) {
        redirectAttributes.addFlashAttribute("alert_danger", "Location of destination is empty");
        return "redirect:"
                + uriBuilder.path("/destination/edit/{id}").buildAndExpand(id).encode().toUriString();
    }

    try {
        destinationFacade.updateDestinationLocation(updatedDestination);
    } catch (Exception ex) {
        redirectAttributes.addFlashAttribute("alert_danger",
                "Destination " + id + " wasn't updated because location already exists");
        return "redirect:"
                + uriBuilder.path("/destination/edit/{id}").buildAndExpand(id).encode().toUriString();
    }
    redirectAttributes.addFlashAttribute("alert_success", "Destination " + id + " was updated");
    return "redirect:" + uriBuilder.path("/destination").toUriString();
}

From source file:cz.muni.fi.mvc.controllers.StewardController.java

/**
 * Creates a new Steward//ww  w  .j  ava  2 s  . c  o  m
 *
 * @param model display data
 * @return jsp page
 */
@RequestMapping(method = RequestMethod.POST, value = "/create")
public String create(@Valid @ModelAttribute("stewardCreate") StewardCreationalDTO formBean,
        BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
        UriComponentsBuilder uriBuilder, HttpServletRequest request) {
    if (bindingResult.hasErrors()) {
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        return "steward/new";
    }
    Long id = 0L;
    try {
        //            if (stewardFacade.getRelevantStewards(formBean.getPersonalIdentificator()) != null) {
        //                bindingResult.addError(new FieldError("stewardCreate", "personalIdentificator",
        //                        formBean.getPersonalIdentificator(), false, 
        //                        new String[]{"StewardCreationalDTOValidator.invalid.personalIdetificator"}, 
        //                        null, "Personal identificator already exists."));
        //                model.addAttribute("personalIdentificator_error", true);
        //                return "steward/new";
        //            }
        id = stewardFacade.createSteward(formBean);
        redirectAttributes.addFlashAttribute("alert_info", "Steward with id: " + id + " was created");
    } catch (Exception ex) {
        model.addAttribute("alert_danger", "Steward was not created because of some unexpected error");
        redirectAttributes.addFlashAttribute("alert_danger",
                "Steward was not created because of some unexpected error");
    }
    request.getSession().setAttribute("authenticated",
            stewardFacade.getStewardWithPersonalIdentificator(formBean.getPersonalIdentificator()));
    return "redirect:" + uriBuilder.path("/steward").toUriString();
}

From source file:org.woofenterprise.dogs.web.controllers.AppointmentsController.java

@RequestMapping(value = "/edit", method = RequestMethod.POST)
@RolesAllowed("ADMIN")
public String update(Model model, @Valid @ModelAttribute("appointment") AppointmentDTO appointmentDTO,
        BindingResult bindingResult, UriComponentsBuilder uriBuilder, RedirectAttributes redirectAttributes) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("proceduresOptions", Procedure.values());
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }//from w  w  w . j  a  v a  2s .c  om

        if (bindingResult.hasGlobalErrors()) {
            StringBuilder sb = new StringBuilder();
            for (ObjectError er : bindingResult.getGlobalErrors()) {
                sb.append(er.getDefaultMessage());
                sb.append('\n');
            }

            model.addAttribute("globalError", sb);
        }

        return "appointments/edit";
    }
    Long id = appointmentDTO.getId();
    try {
        facade.updateAppointment(appointmentDTO);

        redirectAttributes.addFlashAttribute("alert_success", "Appointment #" + id + " was edited.");
        return "redirect:"
                + uriBuilder.path("/appointments/view/{id}").buildAndExpand(id).encode().toUriString();
    } catch (Exception e) {
        log.warn("Exception wile editing: " + e.getMessage());
        redirectAttributes.addFlashAttribute("alert_danger", "Appointment #" + id + " was not edited.");
        return "redirect:/";
    }
}

From source file:org.woofenterprise.dogs.web.controllers.AppointmentsController.java

@RequestMapping(value = "/new", method = RequestMethod.POST)
@RolesAllowed("ADMIN")
public String createAppointment(Model model,
        @Valid @ModelAttribute("appointment") AppointmentDTO appointmentDTO, BindingResult bindingResult,
        UriComponentsBuilder uriBuilder, RedirectAttributes redirectAttributes) {
    if (bindingResult.hasErrors()) {
        model.addAttribute("proceduresOptions", Procedure.values());

        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }//from   w w w.  j  av  a 2  s . c  o  m

        if (bindingResult.hasGlobalErrors()) {
            StringBuilder sb = new StringBuilder();
            for (ObjectError er : bindingResult.getGlobalErrors()) {
                sb.append(er.getDefaultMessage());
                sb.append('\n');
            }

            model.addAttribute("globalError", sb);
        }

        return "appointments/create";
    }
    try {
        appointmentDTO = facade.createAppointment(appointmentDTO);
        Long id = appointmentDTO.getId();

        redirectAttributes.addFlashAttribute("alert_success", "Appointment #" + id + " was created.");
        return "redirect:"
                + uriBuilder.path("/appointments/view/{id}").buildAndExpand(id).encode().toUriString();
    } catch (Exception e) {
        log.warn("Exception wile creating: " + e.getMessage());
        redirectAttributes.addFlashAttribute("alert_danger", "Appointment was not created.");
        return "redirect:/";
    }
}

From source file:com.citrix.cpbm.portal.fragment.controllers.AbstractUsersController.java

private void parseResult(BindingResult result, ModelMap map) {

    if (result.getFieldErrors().size() > 0) {
        List<String> errorMsgList = new ArrayList<String>();
        for (FieldError fieldError : result.getFieldErrors()) {
            String fieldName = fieldError.getField();
            if (fieldName.contains(".")) {
                fieldName = fieldName.substring(fieldName.lastIndexOf(".") + 1);
            }//from w w  w. j a va2  s  .  c o  m
            errorMsgList.add(fieldName + " field value '" + fieldError.getRejectedValue() + "' is not valid.");
        }

        map.addAttribute("errorMsgList", errorMsgList);
        map.addAttribute("errormsg", "true");
    }
    if (result.hasGlobalErrors()) {
        List<ObjectError> errorList = result.getGlobalErrors();
        if (errorList.size() > 0) {
            List<String> globalErrors = new ArrayList<String>();
            for (ObjectError error : errorList) {
                globalErrors.add(error.getCode());

            }
            map.addAttribute("globalErrors", globalErrors);
            map.addAttribute("errormsg", "true");
        }

    }
}