Search in sources :

Example 36 with ObjectError

use of org.springframework.validation.ObjectError in project spring-framework by spring-projects.

the class EscapedErrors method escapeObjectError.

@SuppressWarnings("unchecked")
private <T extends ObjectError> T escapeObjectError(T source) {
    if (source == null) {
        return null;
    }
    if (source instanceof FieldError) {
        FieldError fieldError = (FieldError) source;
        Object value = fieldError.getRejectedValue();
        if (value instanceof String) {
            value = HtmlUtils.htmlEscape((String) value);
        }
        return (T) new FieldError(fieldError.getObjectName(), fieldError.getField(), value, fieldError.isBindingFailure(), fieldError.getCodes(), fieldError.getArguments(), HtmlUtils.htmlEscape(fieldError.getDefaultMessage()));
    } else {
        return (T) new ObjectError(source.getObjectName(), source.getCodes(), source.getArguments(), HtmlUtils.htmlEscape(source.getDefaultMessage()));
    }
}
Also used : ObjectError(org.springframework.validation.ObjectError) FieldError(org.springframework.validation.FieldError)

Example 37 with ObjectError

use of org.springframework.validation.ObjectError in project spring-framework by spring-projects.

the class WebExchangeBindException method getMessage.

/**
	 * Returns diagnostic information about the errors held in this object.
	 */
@Override
@SuppressWarnings("OptionalGetWithoutIsPresent")
public String getMessage() {
    MethodParameter parameter = getMethodParameter().get();
    StringBuilder sb = new StringBuilder("Validation failed for argument at index ").append(parameter.getParameterIndex()).append(" in method: ").append(parameter.getMethod().toGenericString()).append(", with ").append(this.bindingResult.getErrorCount()).append(" error(s): ");
    for (ObjectError error : this.bindingResult.getAllErrors()) {
        sb.append("[").append(error).append("] ");
    }
    return sb.toString();
}
Also used : ObjectError(org.springframework.validation.ObjectError) MethodParameter(org.springframework.core.MethodParameter)

Example 38 with ObjectError

use of org.springframework.validation.ObjectError in project head by mifos.

the class DefineNewOfficeController method processFormSubmit.

@RequestMapping(method = RequestMethod.POST)
public ModelAndView processFormSubmit(@RequestParam(value = CANCEL_PARAM, required = false) String cancel, @RequestParam(value = PREVIEW_PARAM, required = false) String preview, @ModelAttribute("officeFormBean") @Valid OfficeFormBean officeFormBean, BindingResult result, SessionStatus status) {
    ModelAndView modelAndView = new ModelAndView(REDIRECT_TO_ADMIN_SCREEN);
    EditOfficeInformationController editOfficeInformationController = new EditOfficeInformationController(officeServiceFacade);
    if (StringUtils.isNotBlank(cancel)) {
        modelAndView.setViewName(REDIRECT_TO_ADMIN_SCREEN);
        status.setComplete();
    } else if (StringUtils.isNotBlank(preview)) {
        if (result.hasErrors()) {
            modelAndView.setViewName("defineNewOffice");
            modelAndView.addObject("showError", "true");
            if (!((!officeFormBean.getLevelId().equals("1") && StringUtils.isNotBlank(officeFormBean.getParentId())) || (officeFormBean.getLevelId().equals("1") && !StringUtils.isNotBlank(officeFormBean.getParentId())))) {
                result.addError(new ObjectError("parentLevelId", "Please specify Parent Level name"));
            }
        } else {
            if (!((!officeFormBean.getLevelId().equals("1") && StringUtils.isNotBlank(officeFormBean.getParentId())) || (officeFormBean.getLevelId().equals("1") && !StringUtils.isNotBlank(officeFormBean.getParentId())))) {
                result.addError(new ObjectError("parentLevelId", "Please specify Parent Level name"));
                modelAndView.addObject("showError", "true");
                modelAndView.setViewName("defineNewOffice");
            } else {
                modelAndView.setViewName("previewOfficeDetails");
                switch(Integer.parseInt(officeFormBean.getLevelId())) {
                    case 1:
                        officeFormBean.setOfficeLevelName("Head Office");
                        break;
                    case 2:
                        officeFormBean.setOfficeLevelName("Regional Office");
                        break;
                    case 3:
                        officeFormBean.setOfficeLevelName("Divisional Office");
                        break;
                    case 4:
                        officeFormBean.setOfficeLevelName("Area Office");
                        break;
                    default:
                        officeFormBean.setOfficeLevelName("Branch Office");
                        break;
                }
            }
        /*modelAndView.addObject("officeTypes", editOfficeInformationController.getOfficeTypes("new"));
            if(!officeFormBean.getLevelId().equals("1")){
                modelAndView.addObject("parentOffices", editOfficeInformationController.getParentDetails(officeFormBean.getLevelId()));
                officeFormBean.setParentOfficeName(officeServiceFacade.retrieveOfficeById(Short.parseShort(officeFormBean.getParentId())).getName());
            }
            modelAndView.addObject("officeFormBean", officeFormBean);*/
        }
        modelAndView.addObject("officeTypes", editOfficeInformationController.getOfficeTypes("new"));
        if ((!officeFormBean.getLevelId().equals("1") && StringUtils.isNotBlank(officeFormBean.getLevelId()))) {
            modelAndView.addObject("parentOffices", editOfficeInformationController.getParentDetails(officeFormBean.getLevelId()));
            if (StringUtils.isNotBlank(officeFormBean.getParentId())) {
                officeFormBean.setParentOfficeName(officeServiceFacade.retrieveOfficeById(Short.parseShort(officeFormBean.getParentId())).getName());
            }
        }
        modelAndView.addObject("officeFormBean", officeFormBean);
    } else {
        modelAndView.setViewName("defineNewOffice");
        modelAndView.addObject("officeTypes", editOfficeInformationController.getOfficeTypes("new"));
        if (!officeFormBean.getLevelId().equals("1")) {
            modelAndView.addObject("parentOffices", editOfficeInformationController.getParentDetails(officeFormBean.getLevelId()));
        }
        modelAndView.addObject("showError", "false");
    }
    return modelAndView;
}
Also used : ObjectError(org.springframework.validation.ObjectError) ModelAndView(org.springframework.web.servlet.ModelAndView) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 39 with ObjectError

use of org.springframework.validation.ObjectError in project head by mifos.

the class UploadLogoController method uploadNewLogo.

@RequestMapping(value = "/uploadNewLogo.ftl", method = RequestMethod.POST)
public String uploadNewLogo(@ModelAttribute LogoUpload logoUpload, BindingResult result, Model model, HttpServletRequest request) {
    String[] availableContentTypes = { "image/png", "image/gif", "image/jpeg", "image/pjpeg" };
    if (Arrays.asList(availableContentTypes).contains(logoUpload.getFile().getContentType())) {
        try {
            logoServiceFacade.uploadNewLogo(logoUpload.getFile());
            model.addAttribute("success", true);
        } catch (IOException e) {
            result.addError(new ObjectError("logoUpload", messageSource.getMessage("admin.uploadLogo.ioexception", null, RequestContextUtils.getLocale(request))));
        }
    } else {
        result.addError(new ObjectError("logoUpload", messageSource.getMessage("admin.uploadLogo.badType", null, RequestContextUtils.getLocale(request))));
    }
    return "uploadNewLogo";
}
Also used : ObjectError(org.springframework.validation.ObjectError) IOException(java.io.IOException) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 40 with ObjectError

use of org.springframework.validation.ObjectError in project head by mifos.

the class PentahoReportingController method addErrorToBindingResult.

private void addErrorToBindingResult(PentahoValidationError validationError, BindingResult bindingResult) {
    ObjectError error;
    if (validationError.isFieldError()) {
        error = new FieldError("pentahoReportFormBean", validationError.getParamName(), validationError.getParamName() + ": " + validationError.getErrorMessage());
    } else {
        error = new ObjectError("pentahoReportFormBean", validationError.getErrorMessage());
    }
    bindingResult.addError(error);
}
Also used : ObjectError(org.springframework.validation.ObjectError) FieldError(org.springframework.validation.FieldError)

Aggregations

ObjectError (org.springframework.validation.ObjectError)57 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)11 BindException (org.springframework.validation.BindException)10 FieldError (org.springframework.validation.FieldError)8 BusinessRuleException (org.mifos.service.BusinessRuleException)7 ModelAndView (org.springframework.web.servlet.ModelAndView)7 BindingResult (org.springframework.validation.BindingResult)6 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)5 Test (org.junit.Test)5 OnmsLocationMonitor (org.opennms.netmgt.model.OnmsLocationMonitor)5 LocationMonitorIdCommand (org.opennms.web.svclayer.model.LocationMonitorIdCommand)5 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)5 MapBindingResult (org.springframework.validation.MapBindingResult)5 Document (org.w3c.dom.Document)5 Element (org.w3c.dom.Element)5 ArrayList (java.util.ArrayList)3 Errors (org.springframework.validation.Errors)3 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)3 ServletException (javax.servlet.ServletException)2