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);
}
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;
}
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();
}
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);
}
}
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;
}
Aggregations