Example usage for org.springframework.validation BindingResult getFieldErrors

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

Introduction

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

Prototype

List<FieldError> getFieldErrors();

Source Link

Document

Get all errors associated with a field.

Usage

From source file:com.google.ie.web.controller.CommentController.java

/**
 * Handles request to add comment on Idea.
 * //from  w  w  w .j  a va  2  s .c  o m
 * @param ideaComment key of the Idea on which the comment is to be added.
 * @param user the User object
 * @throws IOException
 */
@RequestMapping(value = "/post", method = RequestMethod.POST)
public void postCommentOnIdea(HttpServletRequest request, HttpSession session,
        @ModelAttribute IdeaComment ideaComment, BindingResult result, Map<String, Object> map,
        @RequestParam String recaptchaChallengeField, @RequestParam String recaptchaResponseField)
        throws IOException {

    /*
     * get captcha fields from request call CommentValidator to validate
     * input IdeaComment object
     */
    ViewStatus viewStatus = new ViewStatus();
    Boolean captchaValidation = reCaptchaUtility.verifyCaptcha(request.getRemoteAddr(), recaptchaChallengeField,
            recaptchaResponseField);

    new CommentValidator().validate(ideaComment, result);
    /*
     * check for validation or captcha error and if error occured display
     * the error messages.
     */
    if (result.hasErrors() || !captchaValidation) {
        logger.warn("Comment object has " + result.getErrorCount() + " validation errors");
        viewStatus.setStatus(WebConstants.ERROR);
        if (!captchaValidation) {
            viewStatus.addMessage(WebConstants.CAPTCHA, WebConstants.CAPTCHA_MISMATCH);
        }
        for (Iterator<FieldError> iterator = result.getFieldErrors().iterator(); iterator.hasNext();) {
            FieldError fieldError = iterator.next();
            viewStatus.addMessage(fieldError.getField(), fieldError.getDefaultMessage());
            logger.warn("Error found in field: " + fieldError.getField() + " Message :"
                    + fieldError.getDefaultMessage());
        }

    } else {
        User user = (User) session.getAttribute(WebConstants.USER);
        /* Call comment service to add new comment */
        Comment comment = commentService.addComment(ideaComment, user);

        if (comment != null) {
            viewStatus.setStatus(WebConstants.SUCCESS);
            viewStatus.addMessage(WebConstants.COMMENTS, WebConstants.COMMENT_SUCCESSFULL);
        } else {
            viewStatus.setStatus(WebConstants.ERROR);
            viewStatus.addMessage(WebConstants.COMMENTS, WebConstants.COMMENT_FAILED);
        }
    }
    map.remove("ideaComment");
    map.put(WebConstants.VIEW_STATUS, viewStatus);
}

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

@RequestMapping(value = "addRank", method = RequestMethod.POST)
public String addRank(@Valid @ModelAttribute RankModel newRank, BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getField() + " ist falsch";
        }// w  w w .  j av  a  2  s.  co m
        model.addAttribute("errorMessage", errorMessage);
        return "forward:dienstgrade";
    }
    rankRepository.save(newRank);
    model.addAttribute("message", "Neuer Dienstgrad " + newRank.getDescription() + " hinzugefgt.");

    return "forward:dienstgrade";
}

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

@RequestMapping(value = "addFunction", method = RequestMethod.POST)
public String addFunction(@Valid @ModelAttribute FireEngineAbbreviationModel newFireEngineAbbreviation,
        BindingResult bindingResult, Model model) {
    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getField() + " ist falsch";
        }/* w  ww. j  a  v a  2s.c  om*/
        model.addAttribute("errorMessage", errorMessage);
        return "forward:funktionen";
    }
    fireEngineAbbreviationRepository.save(newFireEngineAbbreviation);
    model.addAttribute("message",
            "Neue Fahrzeugfunktion " + newFireEngineAbbreviation.getDescription() + " hinzugefgt.");

    return "forward:feuerwehr";
}

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

@RequestMapping(value = "addStatus", method = RequestMethod.POST)
public String addStatus(@Valid @ModelAttribute FireFighterStatusModel newStatus, BindingResult bindingResult,
        Model model) {/*from   ww w  .  ja v a  2 s  .co m*/
    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage += fieldError.getField() + " ist falsch";
        }
        model.addAttribute("errorMessage", errorMessage);
        return "forward:status";
    }
    statusRepository.save(newStatus);
    model.addAttribute("message", "Neuer Mitgliederstatus " + newStatus.getDescription() + " hinzugefgt.");

    return "forward:status";
}

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

@RequestMapping(value = "addFireFighter", method = RequestMethod.POST)
public String addFireFighter(@Valid @ModelAttribute FireFighterModel newFireFighter,
        BindingResult bindingResult, Model model, HttpServletRequest request) {

    if (bindingResult.hasErrors()) {
        String errorMessage = "";
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            errorMessage = fieldError.getField() + " ist falsch";
        }/*from w  ww . j a  v  a  2 s .c om*/
        model.addAttribute("errorMessage", errorMessage);
        return "forward:mitglieder";
    }
    if (!request.isUserInRole("ROLE_GLOBAL_ADMIN")) {
        newFireFighter.setFireBrigade(getSessionFireBrigade());
    }
    fireFighterRepository.save(newFireFighter);
    model.addAttribute("message",
            "Mitglied " + newFireFighter.getName() + " " + newFireFighter.getSurname() + " hinzugefgt.");

    return "forward:mitglieder";
}

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

/**
 * Protected method to Parse Result.//from w  w  w. j a  v  a  2  s  .  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: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!";
        }/*from   www  .  j a v a2s.  c  om*/
        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 = "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!";
        }//w ww  .  j a  v  a2 s .com
        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  w  w  w .j a  v  a2 s  .  c om
        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 = "editRank", method = RequestMethod.POST)
public String editRank(@Valid @ModelAttribute RankModel changedRank, BindingResult bindingResult, Model model,
        HttpServletRequest request) {/*from  www  . j a  va2s  .c o  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";
    }
}