Example usage for org.springframework.validation BindingResult getAllErrors

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

Introduction

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

Prototype

List<ObjectError> getAllErrors();

Source Link

Document

Get all errors, both global and field ones.

Usage

From source file:controller.Crud.java

@RequestMapping(value = "/tambahdata", method = RequestMethod.POST)
public ModelAndView simpan(@Valid @ModelAttribute("hallo") Hallo hallo, BindingResult result) {
    if (result.hasErrors()) {
        ModelAndView modelAndView = new ModelAndView("crud/tambah");
        modelAndView.addObject("command", new Hallo());
        modelAndView.addObject("error", result.getAllErrors());
        return modelAndView;
    } else {// www. j av  a2 s  .c  om
        config.tambahData(hallo);
        return new ModelAndView("redirect:show");
    }
}

From source file:de.dominikschadow.duke.encounters.controller.AccountController.java

/**
 * Creates the new user and stores it in the database.
 *
 * @param dukeEncountersUser The new user to register
 * @return Login URL//from   ww w.j  ava2 s .  c  o m
 */
@RequestMapping(value = "/register", method = POST)
public ModelAndView createUser(@Valid DukeEncountersUser dukeEncountersUser, BindingResult result) {
    if (result.hasErrors()) {
        return new ModelAndView("register", "formErrors", result.getAllErrors());
    }

    DukeEncountersUser newUser = userService.createUser(dukeEncountersUser);

    logger.warn(SecurityMarkers.SECURITY_AUDIT, "User {} created", newUser);

    ModelAndView modelAndView = new ModelAndView("login");
    modelAndView.addObject("userCreated", newUser.getUsername());

    return modelAndView;
}

From source file:comsat.sample.ui.mvc.MessageController.java

@RequestMapping(method = RequestMethod.POST)
public ModelAndView create(@Valid Message message, BindingResult result, RedirectAttributes redirect)
        throws InterruptedException, SuspendExecution {
    Fiber.sleep(10);//from ww w . j  a  va  2 s .c o m
    if (result.hasErrors()) {
        return new ModelAndView("messages/form", "formErrors", result.getAllErrors());
    }
    message = this.messageRepository.save(message);
    redirect.addFlashAttribute("globalMessage", "Successfully created a new message");
    return new ModelAndView("redirect:/{message.id}", "message.id", message.getId());
}

From source file:org.easyj.rest.view.JSONView.java

public <T> JSONObject getJSONErrors(BindingResult result) {
    JSONObject errors = new JSONObject();
    JSONObject jsonError;//from  w  w  w .jav  a  2  s  . co m
    String errorName;
    if (result != null) {
        for (ObjectError error : result.getAllErrors()) {
            errorName = error.getObjectName();
            jsonError = new JSONObject().element("message", error.getDefaultMessage());
            if (error instanceof FieldError) {
                jsonError.element("rejectedValue", ((FieldError) error).getRejectedValue());
                errorName = ((FieldError) error).getField();
            }

            errors.element(errorName, jsonError);
        }
    }
    return errors;
}

From source file:fr.univrouen.poste.web.admin.CommissionExcelController.java

@RequestMapping(value = "/addFile", method = RequestMethod.POST, produces = "text/html")
public String addFile(@Valid CommissionExcel commissionExcel, BindingResult bindingResult, Model uiModel,
        HttpServletRequest httpServletRequest) throws IOException, SQLException {
    if (bindingResult.hasErrors()) {
        logger.warn(bindingResult.getAllErrors());
        return "redirect:/admin/commissionexcels";
    }/* w w  w  . j  av  a  2 s. c o  m*/
    uiModel.asMap().clear();

    // upload file
    MultipartFile file = commissionExcel.getFile();
    String filename = file.getOriginalFilename();
    InputStream inputStream = file.getInputStream();
    byte[] bytes = IOUtils.toByteArray(inputStream);

    commissionExcel.setFilename(filename);
    commissionExcel.getBigFile().setBinaryFile(new SerialBlob(bytes));
    commissionExcel.getBigFile().persist();

    // set current date 
    Calendar cal = Calendar.getInstance();
    commissionExcel.setCreation(cal.getTime());

    // persist
    commissionExcel.persist();

    // process : generate CommissionEntries
    commissionExcelParser.process(commissionExcel);

    return "redirect:/admin/commissionexcels";
}

From source file:fr.univrouen.poste.web.admin.GalaxieExcelController.java

@RequestMapping(value = "/addFile", method = RequestMethod.POST, produces = "text/html")
public String addFile(@Valid GalaxieExcel galaxieExcel, BindingResult bindingResult, Model uiModel,
        HttpServletRequest httpServletRequest) throws IOException, SQLException {
    if (bindingResult.hasErrors()) {
        logger.warn(bindingResult.getAllErrors());
        return "redirect:/admin/galaxieexcels";
    }/*from  ww  w . jav  a  2 s  .com*/
    uiModel.asMap().clear();

    // upload file
    MultipartFile file = galaxieExcel.getFile();
    String filename = file.getOriginalFilename();
    InputStream inputStream = file.getInputStream();
    byte[] bytes = IOUtils.toByteArray(inputStream);

    galaxieExcel.setFilename(filename);
    galaxieExcel.getBigFile().setBinaryFile(new SerialBlob(bytes));
    galaxieExcel.getBigFile().persist();

    // set current date 
    Calendar cal = Calendar.getInstance();
    galaxieExcel.setCreation(cal.getTime());

    // persist
    galaxieExcel.persist();

    // process : generate GalaxieEntries
    galaxieExcelParser.process(galaxieExcel);

    return "redirect:/admin/galaxieexcels";
}

From source file:technology.tikal.gae.service.template.RestControllerTemplate.java

@ExceptionHandler(NotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)//  w w  w. ja  v  a2s. co  m
public BasicErrorMessage handleException(NotValidException ex, HttpServletRequest request,
        HttpServletResponse response) {
    response.setHeader("Content-Type", "application/json;charset=UTF-8");
    BasicErrorMessage result = new BasicErrorMessage();
    BindingResult detail = ex.getDetails();
    String[] msg = new String[detail.getAllErrors().size()];
    String[][] code = new String[detail.getAllErrors().size()][];
    String[][] args = new String[detail.getAllErrors().size()][];
    int index = 0;
    Locale locale = LocaleContextHolder.getLocale();
    for (ObjectError x : detail.getAllErrors()) {
        Object[] tmpArgs = resolveArgumentMessage(x.getCodes(), x.getArguments());
        msg[index] = getValidationMessage(x.getDefaultMessage(), x.getCodes(), tmpArgs, locale);
        code[index] = x.getCodes();
        String[] tmp = new String[tmpArgs.length];
        args[index] = tmp;
        int j = 0;
        for (Object y : tmpArgs) {
            args[index][j] = y.toString();
            j = j + 1;
        }
        index = index + 1;
    }
    result.setType(ex.getClass().getSimpleName());
    result.setMessage(msg);
    result.setCode(code);
    result.setArguments(args);
    return result;
}

From source file:net.solarnetwork.util.BindingResultSerializer.java

@Override
public Object serialize(Object data, String propertyName, Object propertyValue) {
    if (propertyValue == null) {
        return null;
    }/*from ww w .  j a  va 2s .  c om*/
    if (!(propertyValue instanceof BindingResult)) {
        throw new IllegalArgumentException("Not a BindingResult: " + propertyValue.getClass());
    }
    BindingResult br = (BindingResult) propertyValue;
    if (!br.hasErrors()) {
        return null;
    }
    Map<String, Object> map = new LinkedHashMap<String, Object>();
    map.put("objectName", br.getObjectName());
    map.put("errors", br.getAllErrors());
    map.put("globalErrors", br.getGlobalErrors());
    return map;
}

From source file:org.smigo.comment.CommentController.java

@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/rest/comment", produces = "application/json", method = RequestMethod.POST)
@ResponseBody//  w w  w .  j av a 2s  .c om
public Object addMessage(@Valid @RequestBody Comment comment, BindingResult result,
        @AuthenticationPrincipal AuthenticatedUser user, HttpServletResponse response) {
    if (result.hasErrors()) {
        response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
        return result.getAllErrors();
    }
    return commentHandler.addComment(comment, user.getId());
}

From source file:org.smigo.comment.CommentController.java

@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/rest/comment", produces = "application/json", method = RequestMethod.PUT)
@ResponseBody/* w w  w .  j  a va2  s .  co m*/
public Object updateComment(@Valid @RequestBody Comment comment, BindingResult result,
        @AuthenticationPrincipal AuthenticatedUser user, HttpServletResponse response) {
    if (result.hasErrors()) {
        response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
        return result.getAllErrors();
    }
    commentHandler.updateComment(comment, user);
    return Collections.emptyList();
}