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:com.pw.ism.controllers.MessageController.java

@RequestMapping(value = "sendmail", method = RequestMethod.POST, headers = { "Content-type=application/json" })
public ResponseEntity<String> sendMail(@Valid @RequestBody MailMessage mail, BindingResult result) {
    if (result.hasErrors()) {
        for (ObjectError error : result.getAllErrors()) {
            LOGGER.info("not valid: " + error.toString());
        }/*from   w w  w.j av  a2  s.co  m*/
        return new ResponseEntity<>("NOK!", HttpStatus.BAD_REQUEST);
    } else {
        return new ResponseEntity<>("OK!", HttpStatus.OK);
    }
}

From source file:gxu.software_engineering.shen10.market.controller.CategoryController.java

@RequestMapping(value = "/categories/add", method = POST)
public String add(Model model, @Valid Category category, BindingResult result) {
    if (result.hasErrors()) {
        List<ObjectError> errors = result.getAllErrors();
        String msg = "??\n";
        for (int i = 0; i < errors.size(); i++) {
            msg += errors.get(i).getDefaultMessage() + "\n";
        }//from ww  w.  j  a v  a  2s. c om
        throw new IllegalArgumentException(msg);
    }
    categoryService.add(category);
    model.addAttribute(STATUS, STATUS_OK);
    return BAD_REQUEST;
}

From source file:com.mitchellbosecke.pebble.spring.extension.function.bindingresult.GetAllErrorsFunction.java

@Override
public Object execute(Map<String, Object> args) {
    List<String> results = new ArrayList<>();
    String formName = (String) args.get(PARAM_FORM_NAME);

    EvaluationContext context = (EvaluationContext) args.get("_context");
    Locale locale = context.getLocale();
    BindingResult bindingResult = this.getBindingResult(formName, context);

    if (bindingResult != null) {
        for (ObjectError error : bindingResult.getAllErrors()) {
            String msg = this.messageSource.getMessage(error.getCode(), error.getArguments(),
                    error.getDefaultMessage(), locale);
            if (msg != null) {
                results.add(msg);//from ww  w . j  a  v a 2  s. co m
            }
        }
    }
    return results;
}

From source file:com.jnj.b2b.storefront.controllers.misc.AddToCartController.java

protected String getViewWithBindingErrorMessages(final Model model, final BindingResult bindingErrors) {
    for (final ObjectError error : bindingErrors.getAllErrors()) {
        if (error.getCode().equals(TYPE_MISMATCH_ERROR_CODE)) {
            model.addAttribute(ERROR_MSG_TYPE, QUANTITY_INVALID_BINDING_MESSAGE_KEY);
        } else {//from w ww .j ava2s .c  om
            model.addAttribute(ERROR_MSG_TYPE, error.getDefaultMessage());
        }
    }
    return ControllerConstants.Views.Fragments.Cart.AddToCartPopup;
}

From source file:org.parancoe.basicWebApp.ValidationTest.java

public void testPersonValidationInError() {
    Person person = new Person("", "Benfante", new Date());
    BindingResult result = new BeanPropertyBindingResult(person, "person");
    validator.validate(person, result);/*ww  w .  j a  v  a2  s .  c o  m*/
    assertTrue(result.hasErrors());
    assertSize(2, result.getAllErrors());
}

From source file:com.mascova.caliga.controller.LoginController.java

@RequestMapping(value = "/forget", method = RequestMethod.POST)
public String sendResetPassword(Model model, @Valid ForgetForm forgetForm, BindingResult result) {
    if (result.hasErrors()) {
        model.addAttribute("forgetForm", forgetForm);
        model.addAttribute("errorValidate", result.getAllErrors());
        return "page-lost-password";
    }//from   w w w  .j av  a  2  s . c o  m
    User user = userService.findByEmail(forgetForm.getEmail());
    if (user == null) {
        model.addAttribute("errorMsg", "email not found");
        return "page-lost-password";
    }
    model.addAttribute("successMsg", "Please check your email to reset password");
    userService.resetPassword(user);

    return "page-lost-password";
}

From source file:com.example.mvc.MessageController.java

@RequestMapping(method = RequestMethod.POST)
public ModelAndView create(@Valid Message message, BindingResult result, RedirectAttributes redirect) {
    if (result.hasErrors()) {
        return new ModelAndView("messages/form", "formErrors", result.getAllErrors());
    }/*from   w w  w.  ja v a 2s .  c om*/
    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:de.dominikschadow.duke.encounters.controller.SearchController.java

/**
 * Search the encounters based on the given search filter.
 *
 * @param searchFilter The search filter identifying encounters
 * @param result       BindingResult/* w  w w.j  a  v a2 s .co m*/
 * @return ModelAndView with encounters URL and a model map
 */
@RequestMapping(value = "/encounters", method = POST)
public ModelAndView searchEncounters(@Valid SearchFilter searchFilter, BindingResult result) {
    if (result.hasErrors()) {
        return new ModelAndView("search", "formErrors", result.getAllErrors());
    }

    List<Encounter> encounters = encounterService.getEncounters(searchFilter);

    Map<String, Object> modelMap = new LinkedHashMap<>();
    modelMap.put("encounters", encounters);
    modelMap.put("searchFilter", searchFilter);

    return new ModelAndView("encounters", modelMap);
}

From source file:io.hedwig.petclinic.ui.web.MessageController.java

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

From source file:com.work.petclinic.web.MessageController.java

@RequestMapping(method = RequestMethod.POST)
public ModelAndView create(@Valid Message message, BindingResult result, RedirectAttributes redirect) {
    if (result.hasErrors()) {
        return new ModelAndView("messages/messageForm", "formErrors", result.getAllErrors());
    }//from w  w  w  .  j  av  a  2 s.c  o m
    message = this.messageRepository.save(message);
    redirect.addFlashAttribute("statusMessage", "Successfully created a new message");
    return new ModelAndView("redirect:/messages/{message.id}", "message.id", message.getId());
}