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:mvc.PaymentController.java

private String getErrors(BindingResult result, HttpServletRequest req) {
    String errorString = "";
    ApplicationContext applicationContext = WebApplicationContextUtils
            .getWebApplicationContext(req.getServletContext());
    ResourceBundleMessageSource rbms = applicationContext.getBean(ResourceBundleMessageSource.class);
    for (FieldError error : result.getFieldErrors()) {
        errorString += rbms.getMessage(error.getCode(), null, Locale.US);
    }/* w ww .  j ava2s  . c o  m*/
    return errorString;
}

From source file:com.biblio.web.rest.UserResource.java

/**
 * POST /account : update the current user information.
 *
 * @param userDTO the current user information
 * @param bindingResult/*from w w w  .  j av  a2s .co  m*/
 * @return the ResponseEntity with status 200 (OK), or status 400 (Bad
 * Request) or 500 (Internal Server Error) if the user couldn't be updated
 */
@RequestMapping(value = "/account", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public Object saveAccount(@Valid @RequestBody UserDTO userDTO, BindingResult bindingResult) {
    Map<String, Object> modele = new HashMap<>();
    Optional<User> existingUser = userRepository.findOneByEmail(userDTO.getEmail());
    if (bindingResult.hasErrors()) {

        modele.put(Constants.ERROR, true);
        modele.put(Constants.MESSAGE, "Enregistrement chou");
        bindingResult.getFieldErrors().stream().forEach((f) -> {
            modele.put(f.getField(), f.getDefaultMessage());
        });

        return modele;
    }

    if (existingUser.isPresent() && (!existingUser.get().getLogin().equalsIgnoreCase(userDTO.getLogin()))) {
        modele.put(Constants.ERROR, "true");
        modele.put(Constants.MESSAGE, "Email existe");
        modele.put(Constants.RESULTAT, "Opration echoue");

        return modele;
    }
    return userRepository.findOneByLogin(SecurityUtils.getCurrentUserLogin()).map(u -> {
        userService.updateUser(userDTO.getNom(), userDTO.getPrenom(), userDTO.getDateNaissance(),
                userDTO.getEmail(), userDTO.getTel());
        modele.put(Constants.RESULTAT, "Opration russi");
        return modele;
    });

}

From source file:cz.muni.fi.dndtroopsweb.controllers.TroopController.java

/**
 * Creates troop based on form provided//from   w w  w  .j  ava  2  s.  com
 *
 * @param formBean troop to be created
 * @return details of newly created troop
 */
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("troopCreate") TroopCreateDTO formBean, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("create(formBean={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "troop/new";
    }

    Long id = troopFacade.createTroop(formBean);

    redirectAttributes.addFlashAttribute("alert_success", "Troop " + id + " was created");
    return "redirect:" + uriBuilder.path("/troop/list").toUriString();
}

From source file:cz.muni.fi.dndtroopsweb.controllers.TroopController.java

/**
 * Changes money based on form for chosen troop
 *
 * @param formBean troop to be modified/* w ww  . j a  v a  2  s  .c om*/
 * @return details of modified troop
 */
@RequestMapping(value = "/cash/{id}", method = RequestMethod.POST)
public String cash(@PathVariable long id, @Valid @ModelAttribute("troop") TroopDTO formBean,
        BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
        UriComponentsBuilder uriBuilder) {
    log.debug("update money(troop={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "troop/money";
    }
    troopFacade.changeMoney(id, formBean.getMoney());
    //Long id = formBean.getId();
    redirectAttributes.addFlashAttribute("alert_success", "Money updated");
    return "redirect:" + uriBuilder.path("/troop/details/{id}").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.dndtroopsweb.controllers.TroopController.java

/**
 * Changes mission based on form for chosen troop
 *
 * @param formBean troop to be modified//from  ww  w . ja  va 2  s  . c  o m
 * @return details of modified troop
 */
@RequestMapping(value = "/job/{id}", method = RequestMethod.POST)
public String job(@Valid @ModelAttribute("hero") TroopDTO formBean, BindingResult bindingResult, Model model,
        RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("update mission(troop={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "troop/mission";
    }
    troopFacade.changeMission(formBean.getId(), formBean.getMission());
    Long id = formBean.getId();
    redirectAttributes.addFlashAttribute("alert_success", "Mission updated");
    return "redirect:" + uriBuilder.path("/troop/details/{id}").buildAndExpand(id).encode().toUriString();
}

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

@RequestMapping(value = "/edit", method = RequestMethod.POST)
public String edit(@Valid @ModelAttribute("gameEdit") GameCreateDTO formBean, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("edit(gameEdit={})", 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 ava2 s . com
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "game/edit";

    }

    if (formBean.getGuestTeam().equals(formBean.getHomeTeam())) {
        redirectAttributes.addFlashAttribute("alert_warning",
                "Game editing failed - two same teams were selected!");
        return "redirect:" + uriBuilder.path("/game/list").build().encode().toUriString();
    }

    GameDTO g = gameFacade.findById(formBean.getId());
    g.setDateOfGame(formBean.getDateOfGame());
    g.setGuestTeam(teamFacade.getTeamById(formBean.getGuestTeam()));
    g.setHomeTeam(teamFacade.getTeamById(formBean.getHomeTeam()));

    gameFacade.update(g);

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

}

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

@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("playerCreate") GameCreateDTO 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  .  ja  va2s. c om*/
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "game/new";

    }

    //create game

    if (formBean.getGuestTeam().equals(formBean.getHomeTeam())) {
        redirectAttributes.addFlashAttribute("alert_warning",
                "Game creation failed - two same teams were selected!");
        return "redirect:" + uriBuilder.path("/game/list").build().encode().toUriString();
    }

    Long id = gameFacade.create(formBean);

    //report success
    redirectAttributes.addFlashAttribute("alert_success", "Match " + id + " was created");
    return "redirect:" + uriBuilder.path("/game/list").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.dndtroopsweb.controllers.HeroController.java

/**
 * Changes hero's role/*from  w w w  . ja  va 2 s.co m*/
 *
 * @param formBean hero whose role will be changed
 * @return Details of hero whose role has been changed
 */
@RequestMapping(value = "/change/{id}", method = RequestMethod.POST)
public String change(@PathVariable long id, @Valid @ModelAttribute("hero") HeroDTO formBean,
        BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
        UriComponentsBuilder uriBuilder) {
    log.debug("change role(hero={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "hero/role";
    }
    heroFacade.changeRole(id, formBean.getRole());
    redirectAttributes.addFlashAttribute("alert_success", "Role was changed");
    return "redirect:" + uriBuilder.path("/hero/details/{id}").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.dndtroopsweb.controllers.HeroController.java

/**
 * Creates hero based on filled form/*from w  w  w . j  av a  2  s.co m*/
 *
 * @param formBean hero to be created
 * @return Details of created hero
 */
@RequestMapping(value = "/create", method = RequestMethod.POST)
public String create(@Valid @ModelAttribute("heroCreate") HeroCreateDTO formBean, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {
    log.debug("create(heroCreate={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "hero/new";
    }
    Long id = heroFacade.createHero(formBean);
    redirectAttributes.addFlashAttribute("alert_success", "Hero " + id + " was created");
    return "redirect:" + uriBuilder.path("/hero/details/{id}").buildAndExpand(id).encode().toUriString();
}

From source file:cz.muni.fi.dndtroopsweb.controllers.HeroController.java

/**
 * Insert hero into troop/*from   w  ww .j  av a  2 s .  c om*/
 *
 * @param formBean hero to be added into troop
 * @return Details of hero who has been inserted
 */
@RequestMapping(value = "/add/{id}", method = RequestMethod.POST)
public String add(@PathVariable long id, @Valid @ModelAttribute("hero") HeroDTO formBean,
        BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes,
        UriComponentsBuilder uriBuilder) {
    log.debug("add troop(hero={})", formBean);
    //in case of validation error forward back to the the form
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.trace("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.trace("FieldError: {}", fe);
        }
        return "hero/troop";
    }
    if (heroFacade.getHeroWithId(id).getTroop() != null) {
        redirectAttributes.addFlashAttribute("alert_warning",
                "Hero \"" + heroFacade.getHeroWithId(id).getName() + "\" is already in troop");
        return "redirect:" + uriBuilder.path("/hero/details/" + id).toUriString();
    }
    heroFacade.addTroop(id, formBean.getId());
    redirectAttributes.addFlashAttribute("alert_success", "Hero added into Troop");
    return "redirect:" + uriBuilder.path("/hero/details/{id}").buildAndExpand(id).encode().toUriString();
}