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.TipoClienteController.java

@RequestMapping(value = "/elimina", method = RequestMethod.POST)
public String elimina(HttpServletRequest request, @RequestParam Long id, Model modelo,
        @ModelAttribute TipoCliente tipoCliente, BindingResult bindingResult,
        RedirectAttributes redirectAttributes) {
    log.debug("Elimina tipoCliente");
    try {//  w  w  w .j a  va 2s .  c o m
        String nombre = tipoClienteDao.elimina(id);

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

    return "redirect:/admin/tipoCliente";
}

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

@RequestMapping(value = "/elimina", method = RequestMethod.POST)
public String elimina(HttpServletRequest request, @RequestParam Long id, Model modelo,
        @ModelAttribute TipoProducto tipoProducto, BindingResult bindingResult,
        RedirectAttributes redirectAttributes) {
    log.debug("Elimina tipoProducto");
    try {//from w w w . j ava  2  s  .  co  m
        String nombre = tipoProductoDao.elimina(id);

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

    return "redirect:/inventario/tipoProducto";
}

From source file:com.devnexus.ting.web.controller.admin.AdminScheduleController.java

private List<ScheduleItem> processScheduleCsv(Event event, MultipartFile scheduleCsv,
        BindingResult bindingResult) {
    List<ScheduleItem> scheduleItems = new ArrayList<>();
    byte[] scheduleCsvData = null;

    try {/*from ww w.  j a  v  a 2 s .  c  om*/
        scheduleCsvData = scheduleCsv.getBytes();
    } catch (IOException e) {
        LOGGER.error("Error processing Schedule CSV File.", e);
        bindingResult.addError(
                new FieldError("uploadScheduleForm", "scheduleFile", "Error processing Schedule CSV File."));
        return null;
    }

    if (scheduleCsvData != null && scheduleCsv.getSize() > 0) {

        final ByteArrayInputStream bais = new ByteArrayInputStream(scheduleCsvData);

        ICsvBeanReader beanReader = null;
        try {
            final Reader reader = new InputStreamReader(bais);
            beanReader = new CsvBeanReader(reader, CsvPreference.STANDARD_PREFERENCE);

            // the header elements are used to map the values to the bean (names must match)
            final String[] header = beanReader.getHeader(true);
            final CellProcessor[] processors = CsvScheduleItemBean.getImportProcessors();

            CsvScheduleItemBean scheduleItemBean;
            while ((scheduleItemBean = beanReader.read(CsvScheduleItemBean.class, header,
                    processors)) != null) {
                final ScheduleItem scheduleItem;

                if (scheduleItemBean.getId() != null) {
                    scheduleItem = businessService.getScheduleItem(scheduleItemBean.getId());
                    if (scheduleItem == null) {
                        LOGGER.error(String.format("Schedule Item with Id '%s' does not exist.",
                                scheduleItemBean.getId()));
                        bindingResult.addError(new FieldError("uploadScheduleForm", "scheduleFile",
                                String.format("Schedule Item with Id '%s' does not exist.",
                                        scheduleItemBean.getId())));
                        continue;
                    }
                } else {
                    scheduleItem = new ScheduleItem();
                }

                if (scheduleItemBean.getEventId() != null) {
                    if (event.getId().equals(scheduleItemBean.getEventId())) {
                        scheduleItem.setEvent(event);
                    } else {
                        throw new IllegalArgumentException("Event ID did not match.");
                    }
                } else {
                    scheduleItem.setEvent(event);
                }

                scheduleItem.setFromTime(scheduleItemBean.getFromTime());
                scheduleItem.setToTime(scheduleItemBean.getToTime());
                scheduleItem.setScheduleItemType(scheduleItemBean.getType());
                scheduleItem.setTitle(scheduleItemBean.getTitle());

                if (scheduleItemBean.getPresentationId() != null) {
                    final Presentation presentation = businessService
                            .getPresentation(scheduleItemBean.getPresentationId());
                    scheduleItem.setPresentation(presentation);
                } else {
                    scheduleItem.setPresentation(null);
                }

                if (scheduleItemBean.getRoomId() != null) {
                    final Room room = businessService.getRoom(scheduleItemBean.getRoomId());
                    scheduleItem.setRoom(room);
                } else {
                    scheduleItem.setRoom(null);
                }

                scheduleItems.add(scheduleItem);

            }
        } catch (IOException e) {
            LOGGER.error("Error processing CSV File.", e);
            bindingResult.addError(new FieldError("uploadScheduleForm", "scheduleFile",
                    "Error processing Schedule CSV File."));
            return null;
        } finally {
            if (beanReader != null) {
                try {
                    beanReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    return scheduleItems;
}

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

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

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

    return "redirect:/admin/cliente";
}

From source file:com.test.springmvc.springmvcproject.IndexController.java

@RequestMapping(value = "", method = RequestMethod.POST)
public String connexion(@Valid LoginBean loginbean, BindingResult result, HttpSession session, ModelMap model) {
    if (result.hasErrors()) {
        return "index";
    }//from  www . j  a v  a 2s.c  om
    try {
        final UtilisateurBean utilisateur = utilisateurService.get(loginbean);
        if (utilisateur != null) {
            session.setAttribute("IsConnected", true);
            session.setAttribute("utilisateur", utilisateur);
            return "index";
        } else {
            throw new NoDataFoundException("connexion.utilisateur.non.trouve");
        }
    } catch (NoDataFoundException e) {
        result.addError(new FieldError("LoginBean", "email", e.getMessage()));
        return "index";
    }
}

From source file:mx.edu.um.mateo.activos.web.TipoActivoController.java

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

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

    return "redirect:/activoFijo/tipoActivo";
}

From source file:br.edu.ifpb.controllers.TopicController.java

@RequestMapping(value = "/novoTopico", method = RequestMethod.POST)
public ModelAndView cadastrarNovoTopico(@Valid TopicValidator topicValidator, BindingResult result, Model model,
        @RequestParam("photo") MultipartFile file) {

    String name = topicValidator.getName();
    String description = topicValidator.getDescription();
    UserProfile userProfile = (UserProfile) httpSession.getAttribute("user");

    model.addAttribute("name", name);
    model.addAttribute("description", description);

    ModelAndView modelAndView = new ModelAndView("novoTopico");

    if (file.getSize() == 0) {
        model.addAttribute("erro", "Insira uma imagem para o tpico");
        result.addError(new ObjectError("erroImagem", "faltou inserir imagem"));
    }/*from   w ww .j  a  v  a  2  s.  c  o m*/

    if (result.hasErrors()) {
        return new ModelAndView("novoTopico");
    } else {

        Topic topic = topicoService.saveTopic(topicValidator.getName(), topicValidator.getDescription(),
                userProfile, file);
        if (topic != null) {

            //                modelAndView.getModel().put("succes", true);
            modelAndView = new ModelAndView("novoTopicoSuccess");
            modelAndView.addObject("id", topic.getId());

        }

    }

    return modelAndView;
}

From source file:mx.edu.um.mateo.contabilidad.web.CuentaMayorController.java

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

        redirectAttributes.addFlashAttribute(Constantes.CONTAINSKEY_MESSAGE, "mayores.eliminada.message");
        redirectAttributes.addFlashAttribute(Constantes.CONTAINSKEY_MESSAGE_ATTRS, new String[] { nombre });
    } catch (Exception e) {
        log.error("No se pudo eliminar la cuenta de mayor " + id, e);
        bindingResult.addError(new ObjectError(Constantes.ADDATTRIBUTE_MAYOR,
                new String[] { "mayores.no.eliminada.message" }, null, null));
        return Constantes.PATH_CUENTA_MAYOR_VER;
    }

    return "redirect:" + Constantes.PATH_CUENTA_MAYOR;
}

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

@RequestMapping(value = "/elimina", method = RequestMethod.POST)
public String elimina(HttpServletRequest request, @RequestParam Long id, Model modelo,
        @ModelAttribute Union union, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
    log.debug("Elimina union");
    try {//from  www .  ja  va  2  s.co m
        String nombre = unionDao.elimina(id);
        ambiente.actualizaSesion(request);

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

    return "redirect:" + Constantes.PATH_UNION;
}