Example usage for org.springframework.web.servlet.support BindStatus getErrors

List of usage examples for org.springframework.web.servlet.support BindStatus getErrors

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support BindStatus getErrors.

Prototype

@Nullable
public Errors getErrors() 

Source Link

Document

Return the Errors instance (typically a BindingResult) that this bind status is currently associated with.

Usage

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 ww  .j  a v  a  2  s. co  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);
                }
            }
        }

        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();
            }/*from  w  w  w  .  j a v a  2 s  .  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;

}