Example usage for org.springframework.validation BindingResult addError

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

Introduction

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

Prototype

void addError(ObjectError error);

Source Link

Document

Add a custom ObjectError or FieldError to the errors list.

Usage

From source file:mx.edu.um.mateo.general.web.AsociacionController.java

@RequestMapping(value = "/elimina", method = RequestMethod.POST)
public String elimina(HttpServletRequest request, @RequestParam Long id, Model modelo,
        @ModelAttribute Asociacion Asociacion, BindingResult bindingResult,
        RedirectAttributes redirectAttributes) {
    log.debug("Elimina Asociacion");
    try {//from  w ww.j a  v  a2s. c  o  m
        Long unionId = (Long) request.getSession().getAttribute("unionId");
        String nombre = asociacionDao.elimina(id, unionId);
        ambiente.actualizaSesion(request);
        redirectAttributes.addFlashAttribute(Constantes.CONTAINSKEY_MESSAGE, "asociacion.eliminada.message");
        redirectAttributes.addFlashAttribute(Constantes.CONTAINSKEY_MESSAGE_ATTRS, new String[] { nombre });
    } catch (Exception e) {
        log.error("No se pudo eliminar la Asociacion " + id, e);
        bindingResult.addError(new ObjectError(Constantes.ADDATTRIBUTE_ASOCIACION,
                new String[] { "asociacion.no.eliminada.message" }, null, null));
        return Constantes.PATH_ASOCIACION_VER;
    }
    return "redirect:" + Constantes.PATH_ASOCIACION;
}

From source file:mx.edu.um.mateo.rh.web.CategoriaController.java

@Transactional
@RequestMapping(value = "/elimina", method = RequestMethod.POST)
public String elimina(HttpServletRequest request, @RequestParam Long id, Model modelo,
        @ModelAttribute Categoria categoria, BindingResult bindingResult,
        RedirectAttributes redirectAttributes) {
    log.debug("Elimina cuenta de categoria");
    try {//from w  ww  .  j ava2s .  c om
        categoriaManager.elimina(id);

        redirectAttributes.addFlashAttribute(Constantes.CONTAINSKEY_MESSAGE, "categoria.elimina.message");
        redirectAttributes.addFlashAttribute(Constantes.CONTAINSKEY_MESSAGE_ATTRS,
                new String[] { categoria.getNombre() });
    } catch (Exception e) {
        log.error("No se pudo eliminar categoria " + id, e);
        bindingResult.addError(new ObjectError(Constantes.ADDATTRIBUTE_CATEGORIA,
                new String[] { "categoria.no.elimina.message" }, null, null));
        return Constantes.PATH_CATEGORIA_VER;
    }

    return "redirect:" + Constantes.PATH_CATEGORIA_LISTA;
}

From source file:com.virtusa.akura.common.controller.ChangePasswordController.java

/**
 * The validateUserSecurityQuestions method validates the user security question answers provided by the
 * user with the answers from the database.
 * //from   w  ww  .  j ava 2s. co m
 * @param userQuestionsSubmitted The userSecurity questions submitted by the user.
 * @param userQuestionsFromDB The security questions saved in the database.
 * @param bindingResult - to bind errors.
 * @return true if the validation of security questions answers are success.
 */
private boolean validateUserSecurityQuestions(List<UserSecurityQuestions> userQuestionsSubmitted,
        List<UserSecurityQuestions> userQuestionsFromDB, BindingResult bindingResult) {

    boolean success = true;

    if (!userQuestionsSubmitted.get(0).getAnswer().trim().equals(userQuestionsFromDB.get(0).getAnswer())
            || !userQuestionsSubmitted.get(1).getAnswer().trim()
                    .equals(userQuestionsFromDB.get(1).getAnswer())) {
        bindingResult.addError(new ObjectError(AkuraWebConstant.USER_INCORRECT_ANSWERS,
                new ErrorMsgLoader().getErrorMessage(AkuraWebConstant.USER_INCORRECT_ANSWERS)));
        success = false;
    }
    return success;

}

From source file:mx.edu.um.mateo.rh.web.EstudiosEmpleadoController.java

@Transactional
@RequestMapping(value = "/elimina", method = RequestMethod.POST)
public String elimina(HttpServletRequest request, @RequestParam Long id, Model modelo,
        @ModelAttribute EstudiosEmpleado estudiosEmpleado, BindingResult bindingResult,
        RedirectAttributes redirectAttributes) {
    log.debug("Elimina cuenta de estudiosEmpleado");
    try {//from  ww  w  .  j  a v a 2s  . com
        String nombre = estudiosEmpleadoDao.elimina(id);

        redirectAttributes.addFlashAttribute(Constantes.CONTAINSKEY_MESSAGE,
                "estudiosEmpleado.eliminado.message");
        redirectAttributes.addFlashAttribute(Constantes.CONTAINSKEY_MESSAGE_ATTRS, new String[] { nombre });
    } catch (Exception e) {
        log.error("No se pudo eliminar estudiosEmpleado " + id, e);
        bindingResult.addError(new ObjectError(Constantes.ADDATTRIBUTE_ESTUDIOSEMPLEADO,
                new String[] { "estudiosEmpleado.no.eliminado.message" }, null, null));
        return Constantes.PATH_ESTUDIOSEMPLEADO_VER;
    }

    return "redirect:" + Constantes.PATH_ESTUDIOSEMPLEADO;
}

From source file:mx.edu.um.mateo.inventario.web.SalidaController.java

@Transactional
@RequestMapping(value = "/elimina", method = RequestMethod.POST)
public String elimina(HttpServletRequest request, @RequestParam Long id, Model modelo,
        @ModelAttribute Salida salida, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
    log.debug("Elimina salida");
    try {//from w  ww  . ja  v a 2 s. c  o m
        String nombre = salidaDao.elimina(id);

        redirectAttributes.addFlashAttribute("message", "salida.eliminada.message");
        redirectAttributes.addFlashAttribute("messageAttrs", new String[] { nombre });
    } catch (Exception e) {
        log.error("No se pudo eliminar la salida " + id, e);
        bindingResult.addError(
                new ObjectError("salida", new String[] { "salida.no.eliminada.message" }, null, null));
        return "inventario/salida/ver";
    }

    return "redirect:/inventario/salida";
}

From source file:org.duracloud.account.app.controller.AccountUsersController.java

@Transactional
@RequestMapping(value = USERS_EDIT_MAPPING, method = RequestMethod.POST)
public ModelAndView editUser(@PathVariable Long accountId, @PathVariable Long userId,
        @ModelAttribute(EDIT_ACCOUNT_USERS_FORM_KEY) AccountUserEditForm accountUserEditForm,
        BindingResult result, Model model, RedirectAttributes redirectAttributes) throws Exception {
    log.debug("editUser account {}", accountId);

    boolean hasErrors = result.hasErrors();
    if (!hasErrors) {
        Role role = Role.valueOf(accountUserEditForm.getRole());
        log.info("New role: {}", role);
        try {/*from w  w w  .j a  v a2 s  .com*/
            setUserRights(userService, accountId, userId, role);
            setSuccessFeedback("Successfully changed user role.", redirectAttributes);
        } catch (AccessDeniedException e) {
            result.addError(new ObjectError("role", "You are unauthorized to set the role for this user"));
            hasErrors = true;
        }
    }

    if (hasErrors) {
        addUserToModel(model);
        return new ModelAndView(ACCOUNT_USERS_VIEW_ID, model.asMap());
    }

    return createAccountRedirectModelAndView(accountId, ACCOUNT_USERS_PATH);
}

From source file:mx.edu.um.mateo.inventario.web.EntradaController.java

@Transactional
@RequestMapping(value = "/elimina", method = RequestMethod.POST)
public String elimina(HttpServletRequest request, @RequestParam Long id, Model modelo,
        @ModelAttribute Entrada entrada, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
    log.debug("Elimina entrada");
    try {//from   w ww  .j  a v a  2s. c om
        String nombre = entradaDao.elimina(id, ambiente.obtieneUsuario());

        redirectAttributes.addFlashAttribute("message", "entrada.eliminada.message");
        redirectAttributes.addFlashAttribute("messageAttrs", new String[] { nombre });
    } catch (Exception e) {
        log.error("No se pudo eliminar la entrada " + id, e);
        bindingResult.addError(
                new ObjectError("entrada", new String[] { "entrada.no.eliminada.message" }, null, null));
        return "inventario/entrada/ver";
    }

    return "redirect:/inventario/entrada";
}

From source file:com.stormpath.tooter.controller.PasswordController.java

@RequestMapping(value = "/password/reset", method = RequestMethod.POST)
public String processChangePassword(@ModelAttribute("customer") User user, BindingResult result,
        HttpSession session) {/* w  ww .  j a v a2 s .  c  om*/

    changePasswordValidator.validate(user, result);

    if (result.hasErrors()) {
        return "changePassword";
    }

    String sptoken = user.getSptoken();

    if (!StringUtils.hasText(sptoken)) {
        //invalid page access - should have an sptoken from the setup form.
        return "redirect:/password/forgot";
    }

    try {
        //New password was specified - verify the reset token and apply the new password:
        Account account = stormpath.getApplication().verifyPasswordResetToken(sptoken);

        //token is valid, set the password and sync to Stormpath:
        account.setPassword(user.getPassword());
        account.save();

        //remove any stale value:
        session.removeAttribute("stormpathAccount");

    } catch (ResourceException re) {
        result.addError(new ObjectError("password",
                re.getCode() == 404 ? "The provided password reset token is invalid." : re.getMessage()));
        re.printStackTrace();
        return "changePassword";
    }

    //form success
    return "redirect:/login/message?loginMsg=passChanged";
}