Example usage for org.springframework.validation BindingResult getGlobalErrors

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

Introduction

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

Prototype

List<ObjectError> getGlobalErrors();

Source Link

Document

Get all global errors.

Usage

From source file:es.jffa.tsc.sip04.web.AccountController.java

/**
 *
 * @param result/*  w ww .  j a v a 2  s  . c  o m*/
 */
private static void _convertPasswordError(final BindingResult result) {
    for (ObjectError error : result.getGlobalErrors()) {
        String msg = error.getDefaultMessage();

        if ("account.password.mismatch.message".equals(msg) == true) {
            if (result.hasFieldErrors("password") == false)
                result.rejectValue("password", "error.mismatch");
        }
    }
}

From source file:org.wallride.web.support.RestValidationErrorModel.java

public static RestValidationErrorModel fromBindingResult(BindingResult result,
        MessageSourceAccessor messageSourceAccessor) {
    RestValidationErrorModel restResult = new RestValidationErrorModel();
    restResult.globalErrors = new ArrayList<>();
    for (ObjectError error : result.getGlobalErrors()) {
        restResult.globalErrors.add(/* w w w.j  a va 2 s .c o m*/
                messageSourceAccessor.getMessage(error.getDefaultMessage(), LocaleContextHolder.getLocale()));
    }
    restResult.fieldErrors = new LinkedHashMap<>();
    for (FieldError error : result.getFieldErrors()) {
        restResult.fieldErrors.put(error.getField(),
                messageSourceAccessor.getMessage(error, LocaleContextHolder.getLocale()));
    }
    return restResult;
}

From source file:cz.jirutka.spring.exhandler.handlers.MethodArgumentNotValidExceptionHandler.java

@Override
public ValidationErrorMessage createBody(MethodArgumentNotValidException ex, HttpServletRequest req) {

    ErrorMessage tmpl = super.createBody(ex, req);
    ValidationErrorMessage msg = new ValidationErrorMessage(tmpl);

    BindingResult result = ex.getBindingResult();

    for (ObjectError err : result.getGlobalErrors()) {
        msg.addError(err.getDefaultMessage());
    }/*  www.  j  a va2 s.  c o m*/
    for (FieldError err : result.getFieldErrors()) {
        msg.addError(err.getField(), err.getRejectedValue(), err.getDefaultMessage());
    }
    return msg;
}

From source file:com.tkmtwo.exhandler.handlers.BindExceptionHandler.java

@Override
public ValidationErrorMessage createBody(BindException ex, HttpServletRequest req) {

    ErrorMessage tmpl = super.createBody(ex, req);
    ValidationErrorMessage msg = new ValidationErrorMessage(tmpl);

    BindingResult result = ex.getBindingResult();

    for (ObjectError err : result.getGlobalErrors()) {
        msg.addError(err.getDefaultMessage());
    }/* w  w  w .j av  a  2 s .co m*/
    for (FieldError err : result.getFieldErrors()) {
        msg.addError(err.getField(), err.getRejectedValue(), err.getDefaultMessage());
    }
    return msg;
}

From source file:com.senfino.yodo.controller.AccontController.java

private void convertPasswordError(BindingResult bindingResult) {
    for (ObjectError objectError : bindingResult.getGlobalErrors()) {
        String msg = objectError.getDefaultMessage();
        if ("account.password.mismatch.message".equals(msg)) {
            if (bindingResult.hasFieldErrors("password")) {
                bindingResult.rejectValue("password", "error.mismatch");
            }//from  ww w  . j  av  a 2s  .com
        }
    }
}

From source file:com.mitchellbosecke.pebble.spring.extension.function.bindingresult.GetGlobalErrorsFunction.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.getGlobalErrors()) {
            String msg = this.messageSource.getMessage(error.getCode(), error.getArguments(),
                    error.getDefaultMessage(), locale);
            if (msg != null) {
                results.add(msg);/*  ww w  .java2s . c om*/
            }
        }
    }
    return results;
}

From source file:org.jtalks.common.web.dto.ValidationResults.java

/**
 * This method populates global (not related to particular field) errors from the specified {@link BindingResult}
 * to the {@code globalErrors} structure
 *
 * @param bindingResult Binding result to populate global errors from
 *///from   ww w. j  a va2 s.  com
private void populateGlobalErrors(BindingResult bindingResult) {
    globalErrors = new ArrayList<String>(bindingResult.getGlobalErrorCount());
    for (ObjectError error : bindingResult.getGlobalErrors()) {
        globalErrors.add(error.getDefaultMessage());
    }
}

From source file:cz.swi2.mendeluis.app.web.controllers.RegistrationController.java

/**
 * Handles registration form submission. 
 *//*from www  .j a va 2 s.c om*/
@RequestMapping(method = RequestMethod.POST)
public String create(@ModelAttribute("user") @Validated UserRegistrationDTO user, BindingResult bindingResult,
        Model model, RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder) {

    log.info("submitted registration: {}", user);
    if (bindingResult.hasErrors()) {
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.info("ObjectError: {}", ge);
        }
        for (FieldError fe : bindingResult.getFieldErrors()) {
            model.addAttribute(fe.getField() + "_error", true);
            log.info("FieldError: {}", fe);
        }
        model.addAttribute("alert_failure", "There are some errors with submitted data.");
        return "user/registration";
    } else {
        try {
            userFacade.createNewUser(user.getName(), user.getUsername(), user.getPassword());
            redirectAttributes.addFlashAttribute("alert_success", "You sign up. You can log in now!");
            return "redirect:/login";
        } catch (UniqueViolationException e) {
            model.addAttribute("alert_failure", "The username already exists!");
            return "user/registration";
        }
    }
}

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

@Override
public Object serialize(Object data, String propertyName, Object propertyValue) {
    if (propertyValue == null) {
        return null;
    }/*from w  ww .ja v a  2 s .  co m*/
    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:cz.muni.pa165.carparkapp.web.PersonalInfoController.java

@RequestMapping(value = "/update", method = RequestMethod.POST)
public String update(@Valid @ModelAttribute EmployeeDTO employee, BindingResult bindingResult,
        RedirectAttributes redirectAttributes, UriComponentsBuilder uriBuilder, Locale locale) {
    log.debug("update(locale={}, customer={})", locale, employee);
    if (bindingResult.hasErrors()) {
        log.debug("binding errors");
        for (ObjectError ge : bindingResult.getGlobalErrors()) {
            log.debug("ObjectError: {}", ge);
        }/*from w w w .ja  v a  2  s.  c  o m*/
        for (FieldError fe : bindingResult.getFieldErrors()) {
            log.debug("FieldError: {}", fe);
        }
        return employee.getId() < 1 ? "employee" : "employee/update";
    }
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    String name = auth.getName();
    EmployeeDTO emp = null;

    for (EmployeeDTO employee1 : employeeService.getAllEmployees()) {
        if (employee1.getUserName().equals(name))
            emp = employee1;
    }
    employee.setUserName(name);
    employee.setPassword(emp.getPassword());
    employeeService.updateEmployee(employee);
    redirectAttributes.addFlashAttribute("message", messageSource.getMessage("employee.updated",
            new Object[] { employee.getFirstName(), employee.getLastName(), employee.getId() }, locale));
    return "redirect:" + uriBuilder.path("/employee").build();
}