Search in sources :

Example 91 with ObjectError

use of org.springframework.validation.ObjectError in project tutorials by eugenp.

the class CustomRestExceptionHandler method handleBindException.

@Override
protected ResponseEntity<Object> handleBindException(final BindException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
    logger.info(ex.getClass().getName());
    // 
    final List<String> errors = new ArrayList<String>();
    for (final FieldError error : ex.getBindingResult().getFieldErrors()) {
        errors.add(error.getField() + ": " + error.getDefaultMessage());
    }
    for (final ObjectError error : ex.getBindingResult().getGlobalErrors()) {
        errors.add(error.getObjectName() + ": " + error.getDefaultMessage());
    }
    final ApiError apiError = new ApiError(HttpStatus.BAD_REQUEST, ex.getLocalizedMessage(), errors);
    return handleExceptionInternal(ex, apiError, headers, apiError.getStatus(), request);
}
Also used : ObjectError(org.springframework.validation.ObjectError) ArrayList(java.util.ArrayList) FieldError(org.springframework.validation.FieldError)

Example 92 with ObjectError

use of org.springframework.validation.ObjectError in project BroadleafCommerce by BroadleafCommerce.

the class AdminAbstractController method populateJsonValidationErrors.

/**
 * Populates the given <b>json</b> response object based on the given <b>form</b> and <b>result</b>
 * @return the same <b>result</b> that was passed in
 */
protected JsonResponse populateJsonValidationErrors(EntityForm form, BindingResult result, JsonResponse json) {
    List<Map<String, Object>> errors = new ArrayList<>();
    for (FieldError e : result.getFieldErrors()) {
        Map<String, Object> errorMap = new HashMap<>();
        errorMap.put("errorType", "field");
        String fieldName = e.getField().substring(e.getField().indexOf("[") + 1, e.getField().indexOf("]")).replace("_", "-");
        errorMap.put("field", fieldName);
        errorMap.put("message", translateErrorMessage(e));
        errorMap.put("code", e.getCode());
        String tabFieldName = fieldName.replaceAll("-+", ".");
        Tab errorTab = form.findTabForField(tabFieldName);
        if (errorTab != null) {
            errorMap.put("tab", errorTab.getTitle());
        }
        errors.add(errorMap);
    }
    for (ObjectError e : result.getGlobalErrors()) {
        Map<String, Object> errorMap = new HashMap<>();
        errorMap.put("errorType", "global");
        errorMap.put("code", e.getCode());
        errorMap.put("message", translateErrorMessage(e));
        errors.add(errorMap);
    }
    json.with("errors", errors);
    return json;
}
Also used : ObjectError(org.springframework.validation.ObjectError) Tab(org.broadleafcommerce.openadmin.web.form.entity.Tab) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) FieldError(org.springframework.validation.FieldError) HashMap(java.util.HashMap) Map(java.util.Map) MultiValueMap(org.springframework.util.MultiValueMap)

Example 93 with ObjectError

use of org.springframework.validation.ObjectError in project BroadleafCommerce by BroadleafCommerce.

the class AdminTranslationController method resultToJS.

/**
 * analyzes the error information, and converts it into a Javascript object  string, which can be passed to to the HTML form through the entityForm
 * @param result
 * @return
 */
private String resultToJS(BindingResult result) {
    StringBuffer sb = new StringBuffer("[");
    List<ObjectError> errors = result.getAllErrors();
    for (ObjectError objectError : errors) {
        if (objectError instanceof FieldError) {
            FieldError ferr = (FieldError) objectError;
            sb.append("{");
            String fieldOnly = StringUtil.extractFieldNameFromExpression(ferr.getField());
            sb.append("\"").append(fieldOnly).append("\":");
            String localizedMessage = BLCMessageUtils.getMessage(ferr.getDefaultMessage());
            sb.append("\"").append(localizedMessage).append("\"");
            sb.append("},");
        }
    }
    if (sb.length() > 1) {
        // the last comma
        sb.deleteCharAt(sb.length() - 1);
    }
    sb.append("]");
    return sb.toString();
}
Also used : ObjectError(org.springframework.validation.ObjectError) FieldError(org.springframework.validation.FieldError)

Example 94 with ObjectError

use of org.springframework.validation.ObjectError in project collect by openforis.

the class DataQueryController method startTest.

@RequestMapping(value = "start-test.json", method = RequestMethod.POST)
@ResponseBody
public Response startTest(@Validated DataQueryForm form, @RequestParam Step recordStep, BindingResult result) {
    List<ObjectError> errors = result.getAllErrors();
    if (errors.isEmpty()) {
        CollectSurvey survey = sessionManager.getActiveSurvey();
        DataQuery query = new DataQuery(survey);
        form.copyTo(query);
        testJob = collectJobManager.createJob(DataQueryExecutorJob.class);
        testJob.setInput(new DataQueryExecutorJobInput(query, recordStep, new MemoryStoreDataQueryResultItemProcessor(query), TEST_MAX_RECORDS));
        collectJobManager.start(testJob);
        return new Response();
    } else {
        return new SimpleFormUpdateResponse(errors);
    }
}
Also used : DataQueryExecutorJobInput(org.openforis.collect.datacleansing.DataQueryExecutorJob.DataQueryExecutorJobInput) Response(org.openforis.commons.web.Response) HttpServletResponse(javax.servlet.http.HttpServletResponse) ObjectError(org.springframework.validation.ObjectError) DataQueryExecutorJob(org.openforis.collect.datacleansing.DataQueryExecutorJob) DataQuery(org.openforis.collect.datacleansing.DataQuery) CollectSurvey(org.openforis.collect.model.CollectSurvey) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 95 with ObjectError

use of org.springframework.validation.ObjectError in project collect by openforis.

the class AbstractPersistedObjectEditFormController method validate.

@RequestMapping(value = "validate", method = POST)
@ResponseBody
public Response validate(@Validated F form, BindingResult result) {
    List<ObjectError> errors = result.getAllErrors();
    Response response = new SimpleFormUpdateResponse(errors);
    return response;
}
Also used : AbstractFormUpdateValidationResponse(org.openforis.commons.web.AbstractFormUpdateValidationResponse) Response(org.openforis.commons.web.Response) ObjectError(org.springframework.validation.ObjectError) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

ObjectError (org.springframework.validation.ObjectError)106 FieldError (org.springframework.validation.FieldError)19 BindingResult (org.springframework.validation.BindingResult)17 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)17 BindException (org.springframework.validation.BindException)14 ArrayList (java.util.ArrayList)13 MapBindingResult (org.springframework.validation.MapBindingResult)13 Test (org.junit.jupiter.api.Test)12 ModelAndView (org.springframework.web.servlet.ModelAndView)10 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)7 Test (org.junit.Test)6 BusinessRuleException (org.mifos.service.BusinessRuleException)6 Errors (org.springframework.validation.Errors)6 MethodArgumentNotValidException (org.springframework.web.bind.MethodArgumentNotValidException)6 DocumentBuilder (javax.xml.parsers.DocumentBuilder)5 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)5 OnmsLocationMonitor (org.opennms.netmgt.model.OnmsLocationMonitor)5 LocationMonitorIdCommand (org.opennms.web.svclayer.model.LocationMonitorIdCommand)5 Document (org.w3c.dom.Document)5 Element (org.w3c.dom.Element)5