Example usage for org.springframework.validation FieldError getCode

List of usage examples for org.springframework.validation FieldError getCode

Introduction

In this page you can find the example usage for org.springframework.validation FieldError getCode.

Prototype

@Nullable
public String getCode() 

Source Link

Document

Return the default code of this resolvable, that is, the last one in the codes array.

Usage

From source file:org.broadleafcommerce.openadmin.web.controller.entity.AdminBasicEntityController.java

/**
 * 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
 *///from  w ww  .j av  a2s. co m
protected JsonResponse populateJsonValidationErrors(EntityForm form, BindingResult result, JsonResponse json) {
    List<Map<String, Object>> errors = new ArrayList<Map<String, Object>>();
    for (FieldError e : result.getFieldErrors()) {
        Map<String, Object> errorMap = new HashMap<String, Object>();
        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<String, Object>();
        errorMap.put("errorType", "global");
        errorMap.put("code", e.getCode());
        errorMap.put("message", translateErrorMessage(e));
        errors.add(errorMap);
    }
    json.with("errors", errors);

    return json;
}

From source file:org.broadleafcommerce.openadmin.web.processor.ErrorsProcessor.java

@Override
protected ProcessorResult processAttribute(Arguments arguments, Element element, String attributeName) {
    String attributeValue = element.getAttributeValue(attributeName);

    BindStatus bindStatus = FieldUtils.getBindStatus(arguments.getConfiguration(), arguments, attributeValue);

    if (bindStatus.isError()) {
        EntityForm form = (EntityForm) ((BindingResult) bindStatus.getErrors()).getTarget();

        // Map of tab name -> (Map field Name -> list of error messages)
        Map<String, Map<String, List<String>>> result = new HashMap<String, Map<String, List<String>>>();
        for (FieldError err : bindStatus.getErrors().getFieldErrors()) {
            //attempt to look up which tab the field error is on. If it can't be found, just use
            //the default tab for the group
            String tabName = EntityForm.DEFAULT_TAB_NAME;
            Tab tab = form.findTabForField(err.getField());
            if (tab != null) {
                tabName = tab.getTitle();
            }/*  w  w  w . j  a v a  2s . c  om*/

            Map<String, List<String>> tabErrors = result.get(tabName);
            if (tabErrors == null) {
                tabErrors = new HashMap<String, List<String>>();
                result.put(tabName, tabErrors);
            }
            if (err.getField().contains(DynamicEntityFormInfo.FIELD_SEPARATOR)) {
                //at this point the field name actually occurs within some array syntax
                String fieldName = err.getField().substring(err.getField().indexOf('[') + 1,
                        err.getField().lastIndexOf(']'));
                String[] fieldInfo = fieldName.split("\\" + DynamicEntityFormInfo.FIELD_SEPARATOR);
                Field formField = form.getDynamicForm(fieldInfo[0]).getFields().get(fieldName);

                if (formField != null) {
                    addFieldError(formField.getFriendlyName(), err.getCode(), tabErrors);
                } else {
                    LOG.warn("Could not find field " + fieldName + " within the dynamic form " + fieldInfo[0]);
                    addFieldError(fieldName, err.getCode(), tabErrors);
                }
            } else {
                Field formField = form.findField(err.getField());
                if (formField != null) {
                    addFieldError(formField.getFriendlyName(), err.getCode(), tabErrors);
                } else {
                    LOG.warn("Could not field field " + err.getField() + " within the main form");
                    addFieldError(err.getField(), err.getCode(), tabErrors);
                }
            }
        }

        String translatedGeneralTab = GENERAL_ERRORS_TAB_KEY;
        BroadleafRequestContext context = BroadleafRequestContext.getBroadleafRequestContext();
        if (context != null && context.getMessageSource() != null) {
            translatedGeneralTab = context.getMessageSource().getMessage(translatedGeneralTab, null,
                    translatedGeneralTab, context.getJavaLocale());
        }

        for (ObjectError err : bindStatus.getErrors().getGlobalErrors()) {
            Map<String, List<String>> tabErrors = result.get(GENERAL_ERRORS_TAB_KEY);
            if (tabErrors == null) {
                tabErrors = new HashMap<String, List<String>>();
                result.put(translatedGeneralTab, tabErrors);
            }
            addFieldError(GENERAL_ERROR_FIELD_KEY, err.getCode(), tabErrors);
        }

        Map<String, Object> localVariables = new HashMap<String, Object>();
        localVariables.put("tabErrors", result);
        return ProcessorResult.setLocalVariables(localVariables);
    }
    return ProcessorResult.OK;

}

From source file:org.sparkcommerce.openadmin.web.processor.ErrorsProcessor.java

@Override
protected ProcessorResult processAttribute(Arguments arguments, Element element, String attributeName) {
    String attributeValue = element.getAttributeValue(attributeName);

    BindStatus bindStatus = FieldUtils.getBindStatus(arguments.getConfiguration(), arguments, attributeValue);

    if (bindStatus.isError()) {
        EntityForm form = (EntityForm) ((BindingResult) bindStatus.getErrors()).getTarget();

        // Map of tab name -> (Map field Name -> list of error messages)
        Map<String, Map<String, List<String>>> result = new HashMap<String, Map<String, List<String>>>();
        for (FieldError err : bindStatus.getErrors().getFieldErrors()) {
            //attempt to look up which tab the field error is on. If it can't be found, just use
            //the default tab for the group
            String tabName = EntityForm.DEFAULT_TAB_NAME;
            Tab tab = form.findTabForField(err.getField());
            if (tab != null) {
                tabName = tab.getTitle();
            }/* ww w  .j av  a  2s  .  c o m*/

            Map<String, List<String>> tabErrors = result.get(tabName);
            if (tabErrors == null) {
                tabErrors = new HashMap<String, List<String>>();
                result.put(tabName, tabErrors);
            }
            if (err.getField().contains(DynamicEntityFormInfo.FIELD_SEPARATOR)) {
                //at this point the field name actually occurs within some array syntax
                String fieldName = err.getField().substring(err.getField().indexOf('[') + 1,
                        err.getField().lastIndexOf(']'));
                String[] fieldInfo = fieldName.split("\\" + DynamicEntityFormInfo.FIELD_SEPARATOR);
                Field formField = form.getDynamicForm(fieldInfo[0]).getFields().get(fieldName);

                if (formField != null) {
                    addFieldError(formField.getFriendlyName(), err.getCode(), tabErrors);
                } else {
                    LOG.warn("Could not find field " + fieldName + " within the dynamic form " + fieldInfo[0]);
                    addFieldError(fieldName, err.getCode(), tabErrors);
                }
            } else {
                Field formField = form.findField(err.getField());
                if (formField != null) {
                    addFieldError(formField.getFriendlyName(), err.getCode(), tabErrors);
                } else {
                    LOG.warn("Could not field field " + err.getField() + " within the main form");
                    addFieldError(err.getField(), err.getCode(), tabErrors);
                }
            }
        }

        for (ObjectError err : bindStatus.getErrors().getGlobalErrors()) {
            Map<String, List<String>> tabErrors = result.get(GENERAL_ERRORS_TAB_KEY);
            if (tabErrors == null) {
                tabErrors = new HashMap<String, List<String>>();
                result.put(GENERAL_ERRORS_TAB_KEY, tabErrors);
            }
            addFieldError(GENERAL_ERROR_FIELD_KEY, err.getCode(), tabErrors);
        }

        Map<String, Object> localVariables = new HashMap<String, Object>();
        localVariables.put("tabErrors", result);
        return ProcessorResult.setLocalVariables(localVariables);
    }
    return ProcessorResult.OK;

}