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.healthcit.analytics.servlet.admin.AdminController.java

@RequestMapping(value = "/admin/edit_user", method = RequestMethod.POST)
public String editUser(@ModelAttribute("user") @Valid UserModel user, BindingResult res, Model model) {
    this.userValidator.validate(user, res);
    if (res.hasErrors()) {
        model.addAllAttributes(res.getAllErrors());
        return "/admin/user";
    }//from  w  w w .j ava  2s .c o  m
    this.userManager.updateUser(this.buildUser(user, this.userManager.getRoles()));
    return "redirect:/admin";
}

From source file:com.healthcit.analytics.servlet.admin.AdminController.java

@RequestMapping(value = "/admin/add_user", method = RequestMethod.POST)
public String addUser(@ModelAttribute("user") @Valid UserModel user, BindingResult res, Model model) {
    this.userValidator.validate(user, res);
    if (res.hasErrors()) {
        model.addAllAttributes(res.getAllErrors());
        return "/admin/user";
    }//  www.  j  av  a  2s  . c  o  m
    this.userManager.createUser(this.buildUser(user, this.userManager.getRoles()));
    return "redirect:/admin";
}

From source file:org.openmrs.module.adminui.page.controller.metadata.locations.LocationPageController.java

private void sendErrorMessage(BindingResult errors, MessageSource messageSource, HttpServletRequest request) {
    List<ObjectError> allErrors = errors.getAllErrors();
    String message = getMessageErrors(messageSource, allErrors);
    request.getSession().setAttribute(UiCommonsConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, message);
}

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

/**
 * Updates the users password and stores it in the database.
 *
 * @param update The new password//from w  ww  . ja  va  2 s.com
 * @return Account page
 */
@RequestMapping(value = "/account/password", method = POST)
@PreAuthorize("hasAnyRole('USER','ADMIN')")
public ModelAndView updatePassword(@Valid PasswordChange update, BindingResult result,
        RedirectAttributes redirectAttributes) {
    if (result.hasErrors()) {
        return new ModelAndView("user/changePassword", "formErrors", result.getAllErrors());
    }

    DukeEncountersUser user = userService.getDukeEncountersUser();
    user.setPassword(userService.hashPassword(update.getNewPassword()));

    DukeEncountersUser storedUser = userService.updateUser(user);

    logger.warn(SecurityMarkers.SECURITY_AUDIT, "User {} changed his password", storedUser.getUsername());

    redirectAttributes.addFlashAttribute("dataUpdated", true);

    return new ModelAndView("redirect:/account");
}

From source file:org.openmrs.contrib.metadatarepository.webapp.controller.UserFormControllerTest.java

@Test
public void testAddWithMissingFields() throws Exception {
    request = newPost("/userform.html");
    user = new User();
    user.setFirstName("Jack");
    request.setRemoteUser("user");

    BindingResult errors = new DataBinder(user).getBindingResult();
    c.onSubmit(user, errors, request, new MockHttpServletResponse());

    assertTrue(errors.getAllErrors().size() == 6);
}

From source file:com.jnj.b2b.storefront.controllers.pages.CartPageController.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  w w  .ja va2 s.c  o  m
            model.addAttribute(ERROR_MSG_TYPE, error.getDefaultMessage());
        }
    }
    return Views.Fragments.Cart.AddToCartPopup;
}

From source file:org.openmrs.module.adminui.page.controller.account.AccountPageController.java

private void sendErrorMessage(BindingResult errors, MessageSource messageSource, HttpServletRequest request) {
    List<ObjectError> allErrors = errors.getAllErrors();
    String message = getMessageErrors(messageSource, allErrors);
    request.getSession().setAttribute(AdminUiConstants.SESSION_ATTRIBUTE_ERROR_MESSAGE, message);
}

From source file:controllers.customer.ComplaintCustomerController.java

@RequestMapping(value = "/create", method = RequestMethod.POST, params = "save")
public ModelAndView save(@Valid ComplaintForm complaintForm, BindingResult binding) {
    ModelAndView result;/*from   w  ww .  j a  va 2  s  .  c om*/

    if (binding.hasErrors()) {
        result = createEditModelAndView(complaintForm);
        System.out.println(binding.getAllErrors());
    } else {
        try {
            Complaint complaint;
            complaint = complaintService.reconstruct(complaintForm);
            complaintService.save(complaint);
            result = new ModelAndView("redirect:listComplaintCustomerIdNotSolved.do");
        } catch (Throwable oops) {

            result = createEditModelAndView(complaintForm, "complaint.error.operation");
            oops.printStackTrace();
        }
    }
    return result;
}

From source file:org.smigo.species.varieties.VarietyController.java

@PreAuthorize("isAuthenticated()")
@RequestMapping(value = "/rest/variety", method = RequestMethod.POST)
public Object addVariety(@Valid @RequestBody Variety variety, BindingResult result,
        HttpServletResponse response, @AuthenticationPrincipal AuthenticatedUser user) {
    if (result.hasErrors()) {
        response.setStatus(HttpStatus.UNPROCESSABLE_ENTITY.value());
        return result.getAllErrors();
    }/*from  w ww.  j  a  va 2  s .c  om*/
    variety.setUserId(user.getId());
    final int id = varietyDao.addVariety(variety);
    response.setStatus(HttpServletResponse.SC_CREATED);
    return id;
}

From source file:org.openmrs.module.billing.web.controller.main.TenderBillAddController.java

@RequestMapping(method = RequestMethod.POST)
public String onSubmit(Model model, @RequestParam("companyId") Integer companyId,
        @RequestParam("tenderIds") Integer[] tenderIds, HttpServletRequest request, Object command,
        BindingResult binding) {

    validateQty(tenderIds, binding, request);
    if (binding.hasErrors()) {
        model.addAttribute("errors", binding.getAllErrors());
        return "module/billing/main/tenderBillAdd";
    }/*from w ww. ja  v  a2  s . c o m*/

    BillingService billingService = (BillingService) Context.getService(BillingService.class);

    Company company = billingService.getCompanyById(companyId);

    TenderBill tenderBill = new TenderBill();
    tenderBill.setCompany(company);
    tenderBill.setCreatedDate(new Date());
    tenderBill.setCreator(Context.getAuthenticatedUser());

    Tender tender = null;
    int quantity = 0;
    Money itemAmount;
    Money totalAmount = new Money(BigDecimal.ZERO);
    for (Integer id : tenderIds) {

        tender = billingService.getTenderById(id);
        quantity = Integer.parseInt(request.getParameter(id + "_qty"));
        itemAmount = new Money(tender.getPrice());
        itemAmount = itemAmount.times(quantity);
        totalAmount = totalAmount.plus(itemAmount);

        TenderBillItem item = new TenderBillItem();
        item.setName(tender.getName() + "_" + tender.getNumber());
        item.setCreatedDate(new Date());
        item.setTender(tender);
        item.setUnitPrice(tender.getPrice());
        item.setQuantity(quantity);
        item.setTenderBill(tenderBill);
        item.setAmount(itemAmount.getAmount());
        tenderBill.addBillItem(item);
    }
    tenderBill.setAmount(totalAmount.getAmount());
    tenderBill.setReceipt(billingService.createReceipt());
    tenderBill = billingService.saveTenderBill(tenderBill);

    return "redirect:/module/billing/tenderBill.list?companyId=" + companyId + "&tenderBillId="
            + tenderBill.getTenderBillId();
}