Example usage for org.springframework.validation FieldError getField

List of usage examples for org.springframework.validation FieldError getField

Introduction

In this page you can find the example usage for org.springframework.validation FieldError getField.

Prototype

public String getField() 

Source Link

Document

Return the affected field of the object.

Usage

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

@RequestMapping(value = "addFireEngine", method = RequestMethod.POST)
public String addFireEngine(@Valid @ModelAttribute FireEngineModel newFireEngine, BindingResult bindingResult,
        Model model, HttpServletRequest request) {
    System.out.println(newFireEngine);
    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage = fieldError.getField() + " ist falsch";
        }/*from   w  ww  .java  2s .  co m*/
        model.addAttribute("errorMessage", errorMessage);
        return "forward:fahrzeuge";
    }
    if (newFireEngine.getActive() == null) {
        newFireEngine.setActive(false);
    }

    if (!request.isUserInRole("ROLE_GLOBAL_ADMIN")) {
        newFireEngine.setFireBrigade(getSessionFireBrigade());
    }
    fireEngineRepository.save(newFireEngine);
    model.addAttribute("message", "Fahrzeug " + newFireEngine.getModel() + " hinzugefgt.");

    return "forward:fahrzeuge";
}

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

@RequestMapping(value = "editStatus", method = RequestMethod.POST)
public String editStatus(@Valid @ModelAttribute FireFighterStatusModel changedStatus,
        BindingResult bindingResult, Model model, HttpServletRequest request) {
    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getField() + " ist ungltig!";
        }// w  w  w .  ja v a2  s.co m
        model.addAttribute("errorMessage", errorMessage);
        return "forward:status";
    }

    if (request.isUserInRole("ROLE_GLOBAL_ADMIN")) {
        FireFighterStatusModel status = statusRepository.findById(changedStatus.getId());

        if (status == null) {
            model.addAttribute("errorMessage", "Diese Feuerwehr kann nicht bearbeitet werden!");
        } else {
            status.setDescription(changedStatus.getDescription());
            statusRepository.save(status);
            model.addAttribute("message",
                    "Mitgliederstatus " + changedStatus.getDescription() + " wurde erfolgreich gendert.");
        }
        return "forward:status";

    } else {
        FireFighterStatusModel status = statusRepository.findById(changedStatus.getId());

        if (status == null) {
            model.addAttribute("errorMessage", "Dieser Mitgliederstatus kann nicht bearbeitet werden!");
            System.out.println("Falscher Mitgliederstatus!");
        } else {
            status.setDescription(changedStatus.getDescription());
            statusRepository.save(status);
            model.addAttribute("message",
                    "Mitgliederstatus " + changedStatus.getDescription() + " wurde erfolgreich gendert!");
        }
        return "forward:status";
    }
}

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

@RequestMapping(value = "editRank", method = RequestMethod.POST)
public String editRank(@Valid @ModelAttribute RankModel changedRank, BindingResult bindingResult, Model model,
        HttpServletRequest request) {/*  w w  w  . ja v a  2 s . co m*/
    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getField() + " ist ungltig!";
        }
        model.addAttribute("errorMessage", errorMessage);
        return "forward:dienstgrade";
    }

    if (request.isUserInRole("ROLE_GLOBAL_ADMIN")) {
        RankModel rank = rankRepository.findById(changedRank.getId());

        if (rank == null) {
            model.addAttribute("errorMessage", "Dieser Dienstgrad kann nicht bearbeitet werden!");
        } else {
            rank.setDescription(changedRank.getDescription());
            rank.setAbbreviation(changedRank.getAbbreviation());
            rankRepository.save(rank);
            model.addAttribute("message",
                    "Dienstgrad " + changedRank.getDescription() + " wurde erfolgreich gendert.");
        }
        return "forward:dienstgrade";

    } else {
        RankModel rank = rankRepository.findById(changedRank.getId());

        if (rank == null) {
            model.addAttribute("errorMessage", "Dieser Dienstgrad kann nicht bearbeitet werden!");
            System.out.println("Falscher Dienstgrad!");
        } else {
            rank.setDescription(changedRank.getDescription());
            rank.setAbbreviation(changedRank.getAbbreviation());
            rankRepository.save(rank);
            model.addAttribute("message",
                    "Dienstgrad " + changedRank.getDescription() + " wurde erfolgreich gendert!");
        }
        return "forward:dienstgrade";
    }
}

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

@RequestMapping(value = "editFunction", method = RequestMethod.POST)
public String editFunction(@Valid @ModelAttribute FireEngineAbbreviationModel changedFireEngineAbbreviation,
        BindingResult bindingResult, Model model, HttpServletRequest request) {
    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getField() + " ist ungltig!";
        }/*from  ww  w .  j  ava 2 s.  c o m*/
        model.addAttribute("errorMessage", errorMessage);
        return "forward:funktionen";
    }

    if (request.isUserInRole("ROLE_GLOBAL_ADMIN")) {
        FireEngineAbbreviationModel fireEngineAbbreviation = fireEngineAbbreviationRepository
                .findById(changedFireEngineAbbreviation.getId());

        if (fireEngineAbbreviation == null) {
            model.addAttribute("errorMessage", "Diese Fahrzeugfunktion kann nicht bearbeitet werden!");
        } else {
            fireEngineAbbreviation.setDescription(changedFireEngineAbbreviation.getDescription());
            fireEngineAbbreviation.setAbbreviation(changedFireEngineAbbreviation.getAbbreviation());
            fireEngineAbbreviation.setOperatingLife(changedFireEngineAbbreviation.getOperatingLife());
            fireEngineAbbreviationRepository.save(fireEngineAbbreviation);
            model.addAttribute("message", "Fahrzeugfunktion " + changedFireEngineAbbreviation.getDescription()
                    + " wurde erfolgreich gendert.");
        }
        return "forward:funktionen";

    } else {
        FireEngineAbbreviationModel fireEngineAbbreviation = fireEngineAbbreviationRepository
                .findById(changedFireEngineAbbreviation.getId());

        if (fireEngineAbbreviation == null) {
            model.addAttribute("errorMessage", "Diese Fahrzeugfunktion kann nicht bearbeitet werden!");
            System.out.println("Falsche Fahrzeugfunktion!");
        } else {
            fireEngineAbbreviation.setDescription(changedFireEngineAbbreviation.getDescription());
            fireEngineAbbreviation.setAbbreviation(changedFireEngineAbbreviation.getAbbreviation());
            fireEngineAbbreviation.setOperatingLife(changedFireEngineAbbreviation.getOperatingLife());
            fireEngineAbbreviationRepository.save(fireEngineAbbreviation);
            model.addAttribute("message", "Fahrzeugfunktion " + changedFireEngineAbbreviation.getDescription()
                    + " wurde erfolgreich gendert!");
        }
        return "forward:funktionen";
    }
}

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

@RequestMapping(value = "editFireBrigade", method = RequestMethod.POST)
public String editFireBrigade(@Valid @ModelAttribute FireBrigadeModel changedFireBrigade,
        BindingResult bindingResult, Model model, HttpServletRequest request) {
    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getField() + " ist ungltig!";
        }//from  ww  w.j  a  v  a 2s.  c o m
        model.addAttribute("errorMessage", errorMessage);
        return "forward:feuerwehr";
    }

    if (request.isUserInRole("ROLE_GLOBAL_ADMIN")) {
        FireBrigadeModel fireBrigade = fireBrigadeRepository.findById(changedFireBrigade.getId());

        if (fireBrigade == null) {
            model.addAttribute("errorMessage", "Diese Feuerwehr kann nicht bearbeitet werden!");
        } else {
            fireBrigade.setName(changedFireBrigade.getName());
            fireBrigade.setPostalCode(changedFireBrigade.getPostalCode());
            fireBrigade.setPostTown(changedFireBrigade.getPostTown());
            fireBrigade.setStreetName(changedFireBrigade.getStreetName());
            fireBrigadeRepository.save(fireBrigade);
            model.addAttribute("message",
                    "Feuerwehr " + changedFireBrigade.getName() + " wurde erfolgreich gendert.");
        }
        return "forward:feuerwehr";

    } else {
        FireBrigadeModel fireBrigade = fireBrigadeRepository.findByIdAndName(changedFireBrigade.getId(),
                getSessionFireBrigade().getName());

        if (fireBrigade == null) {
            model.addAttribute("errorMessage", "Diese Feuerwehr kann nicht bearbeitet werden!");
            System.out.println("Falsche Feuerwehr!");
        } else {
            fireBrigade.setName(changedFireBrigade.getName());
            fireBrigade.setPostalCode(changedFireBrigade.getPostalCode());
            fireBrigade.setPostTown(changedFireBrigade.getPostTown());
            fireBrigade.setStreetName(changedFireBrigade.getStreetName());
            fireBrigadeRepository.save(fireBrigade);
            model.addAttribute("message",
                    "Feuerwehr " + changedFireBrigade.getName() + " wurde erfolgreich gendert!");
            new SecurityContextLogoutHandler().logout(request, null, null);
            return "redirect:login";
        }
        return "forward:feuerwehr";
    }
}

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

@RequestMapping(value = "editFireEngine", method = RequestMethod.POST)
public String editFireEngine(@Valid @ModelAttribute FireEngineModel changedFireEngine,
        BindingResult bindingResult, Model model, HttpServletRequest request) {
    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage = fieldError.getField() + " is invalid";
        }//from w w  w.ja  va2  s  .c  o m
        model.addAttribute("errorMessage", errorMessage);
        return "forward:fahrzeuge";
    }

    if (changedFireEngine.getActive() == null) {
        changedFireEngine.setActive(false);
    }

    if (request.isUserInRole("ROLE_GLOBAL_ADMIN")) {
        FireEngineModel fireEngine = fireEngineRepository.findById(changedFireEngine.getId());

        if (fireEngine == null) {
            model.addAttribute("errorMessage", "Diese Fahrzeug kann nicht bearbeitet werden!");
        } else {
            fireEngine.setModel(changedFireEngine.getModel());
            fireEngine.setLicensePlate(changedFireEngine.getLicensePlate());
            fireEngine.setPerformance(changedFireEngine.getPerformance());
            fireEngine.setBuildYear(changedFireEngine.getBuildYear());
            fireEngine.setActive(changedFireEngine.getActive());
            fireEngine.setAbbreviation(changedFireEngine.getAbbreviation());
            fireEngine.setFireBrigade(changedFireEngine.getFireBrigade());
            fireEngineRepository.save(fireEngine);
            model.addAttribute("message", "Fahrzeug " + changedFireEngine.getModel() + " " + "wurde gendert.");
        }
        return "forward:fahrzeuge";

    } else {
        FireEngineModel fireEngine = fireEngineRepository.findByFireBrigadeAndId(getSessionFireBrigade(),
                changedFireEngine.getId());

        if (fireEngine == null) {
            model.addAttribute("errorMessage", "Dieses Fahrzeug kann nicht bearbeitet werden!");
            System.out.println("Falsches Fahrzeug!");
        } else {
            fireEngine.setModel(changedFireEngine.getModel());
            fireEngine.setLicensePlate(changedFireEngine.getLicensePlate());
            fireEngine.setPerformance(changedFireEngine.getPerformance());
            fireEngine.setBuildYear(changedFireEngine.getBuildYear());
            fireEngine.setActive(changedFireEngine.getActive());
            fireEngine.setAbbreviation(changedFireEngine.getAbbreviation());
            fireEngine.setFireBrigade(getSessionFireBrigade());
            fireEngineRepository.save(fireEngine);
            model.addAttribute("message", "Fahrzeug " + changedFireEngine.getModel() + " " + "wurde gendert.");
        }
        return "forward:fahrzeuge";
    }
}

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

@RequestMapping(value = "editFireFighter", method = RequestMethod.POST)
public String editFireFighter(@Valid @ModelAttribute FireFighterModel changedFireFighter,
        BindingResult bindingResult, Model model, HttpServletRequest request) {
    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage = fieldError.getField() + " ist ungltig!";
        }//  www .j  a  va 2  s  .c  o  m
        model.addAttribute("errorMessage", errorMessage);
        return "forward:mitglieder";
    }

    if (request.isUserInRole("ROLE_GLOBAL_ADMIN")) {
        FireFighterModel fireFighter = fireFighterRepository.findById(changedFireFighter.getId());

        if (fireFighter == null) {
            model.addAttribute("errorMessage", "Dieses Mitglied kann nicht bearbeitet werden!");
        } else {
            fireFighter.setTitle(changedFireFighter.getTitle());
            fireFighter.setName(changedFireFighter.getName());
            fireFighter.setSurname(changedFireFighter.getSurname());
            fireFighter.setRank(changedFireFighter.getRank());
            fireFighter.setFireBrigade(changedFireFighter.getFireBrigade());
            fireFighter.setFireFighterStatus(changedFireFighter.getFireFighterStatus());
            fireFighter.setStreetName(changedFireFighter.getStreetName());
            fireFighter.setPostalCode(changedFireFighter.getPostalCode());
            fireFighter.setPostTown(changedFireFighter.getPostTown());
            fireFighter.setGender(changedFireFighter.getGender());
            fireFighter.setDayOfBirth(changedFireFighter.getDayOfBirth());
            fireFighterRepository.save(fireFighter);
            model.addAttribute("message", "Mitglied " + changedFireFighter.getName() + " "
                    + changedFireFighter.getSurname() + " " + "wurde gendert.");
        }
        return "forward:mitglieder";

    } else {
        FireFighterModel fireFighter = fireFighterRepository.findByFireBrigadeAndId(getSessionFireBrigade(),
                changedFireFighter.getId());

        if (fireFighter == null) {
            model.addAttribute("errorMessage", "Dieses Mitglied kann nicht bearbeitet werden!");
            System.out.println("Falsches Mitglied!");
        } else {
            fireFighter.setTitle(changedFireFighter.getTitle());
            fireFighter.setName(changedFireFighter.getName());
            fireFighter.setSurname(changedFireFighter.getSurname());
            fireFighter.setRank(changedFireFighter.getRank());
            fireFighter.setFireBrigade(getSessionFireBrigade());
            fireFighter.setFireFighterStatus(changedFireFighter.getFireFighterStatus());
            fireFighter.setStreetName(changedFireFighter.getStreetName());
            fireFighter.setPostalCode(changedFireFighter.getPostalCode());
            fireFighter.setPostTown(changedFireFighter.getPostTown());
            fireFighter.setGender(changedFireFighter.getGender());
            fireFighter.setDayOfBirth(changedFireFighter.getDayOfBirth());
            fireFighterRepository.save(fireFighter);
            model.addAttribute("message", "Mitglied " + changedFireFighter.getName() + " "
                    + changedFireFighter.getSurname() + " " + "wurde gendert.");
        }
        return "forward:mitglieder";
    }
}

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

/**
 * Protected method to Parse Result./*  w w w  . ja v a2s.  c  o  m*/
 * 
 * @param result
 * @param map
 */
protected 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);
            }
            errorMsgList.add(fieldName + " field value '" + fieldError.getRejectedValue() + "' is not valid.");
        }

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

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);
            }/* w ww. j a  va 2  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");
        }

    }
}

From source file:com.asual.summer.core.ErrorResolver.java

public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
        Exception e) {//from   w  ww. jav  a2 s.  c om

    if (e instanceof BindException) {

        BindException be = (BindException) e;
        Map<String, Map<String, Object>> errors = new HashMap<String, Map<String, Object>>();

        for (FieldError fe : (List<FieldError>) be.getFieldErrors()) {
            Map<String, Object> error = new HashMap<String, Object>();
            Object[] args = fe.getArguments();
            String key = fe.isBindingFailure() ? fe.getCodes()[2].replaceFirst("typeMismatch", "conversion")
                    : "validation." + fe.getCodes()[2];
            String message = ResourceUtils.getMessage(key, args);
            if (message == null) {
                if (!fe.isBindingFailure()) {
                    if (key.split("\\.").length > 3) {
                        message = ResourceUtils
                                .getMessage(key.substring(0, key.indexOf(".", key.indexOf(".") + 1))
                                        + key.substring(key.lastIndexOf(".")), args);
                    }
                    if (message == null && key.split("\\.").length > 2) {
                        message = ResourceUtils
                                .getMessage(key.substring(0, key.indexOf(".", key.indexOf(".") + 1)), args);
                    }
                } else if (fe.isBindingFailure() && key.split("\\.").length > 2) {
                    message = ResourceUtils.getMessage(
                            key.substring(0, key.indexOf(".")) + key.substring(key.lastIndexOf(".")), args);
                } else {
                    message = fe.getDefaultMessage();
                }
            }
            error.put("message", message != null ? message : "Error (" + key + ")");
            error.put("value", fe.getRejectedValue());
            errors.put(fe.getField(), error);
        }

        for (ObjectError oe : (List<ObjectError>) be.getGlobalErrors()) {
            Map<String, Object> error = new HashMap<String, Object>();
            Object[] args = oe.getArguments();
            String key = "global" + (oe.getCodes() != null ? "." + oe.getCodes()[2] : "");
            String message = ResourceUtils.getMessage(key, args);
            if (message == null) {
                if (key.split("\\.").length > 3) {
                    message = ResourceUtils.getMessage(key.substring(0, key.indexOf(".", key.indexOf(".") + 1))
                            + key.substring(key.lastIndexOf(".")), args);
                }
                if (message == null && key.split("\\.").length > 2) {
                    message = ResourceUtils.getMessage(key.substring(0, key.indexOf(".", key.indexOf(".") + 1)),
                            args);
                }
                if (message == null) {
                    message = oe.getDefaultMessage();
                }
            }
            error.put("message", message != null ? message : "Error (" + key + ")");
            error.put("value", oe.getObjectName());
            errors.put(oe.getObjectName(), error);
        }

        String form = (String) RequestUtils.getParameter("_form");
        if (form != null) {
            if (request.getAttribute(ERRORS) == null) {
                request.setAttribute(ERRORS, errors);
                request.setAttribute(be.getObjectName(), be.getTarget());
                return new ModelAndView(new InternalResourceView(
                        form.concat(form.contains("?") ? "&" : "?").concat("_error=true")));
            }
        } else {
            List<String> pairs = new ArrayList<String>();
            for (String key : errors.keySet()) {
                pairs.add(key + "=" + errors.get(key).get("message"));
            }
            try {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST, StringUtils.join(pairs, ";"));
            } catch (IOException ioe) {
                logger.error(ioe.getMessage(), ioe);
            }
        }
    }

    return null;
}