Example usage for org.springframework.validation DataBinder setIgnoreUnknownFields

List of usage examples for org.springframework.validation DataBinder setIgnoreUnknownFields

Introduction

In this page you can find the example usage for org.springframework.validation DataBinder setIgnoreUnknownFields.

Prototype

public void setIgnoreUnknownFields(boolean ignoreUnknownFields) 

Source Link

Document

Set whether to ignore unknown fields, that is, whether to ignore bind parameters that do not have corresponding fields in the target object.

Usage

From source file:org.agatom.springatom.cmp.wizards.core.AbstractWizardProcessor.java

/**
 * Creates {@link org.springframework.validation.DataBinder} without paying attention on {@link org.springframework.validation.DataBinder#setAllowedFields(String...)} or
 * {@link org.springframework.validation.DataBinder#setRequiredFields(String...)}
 *
 * @param contextObject     context object
 * @param contextObjectName context object name
 *
 * @return the binder/* w w w.j a v a  2 s.c o m*/
 */
private DataBinder createBinder(final Object contextObject, final String contextObjectName) {
    LOGGER.debug(String.format("createGlobalBinder(contextObject=%s,contextObjectName=%s)", contextObject,
            contextObjectName));

    Assert.notNull(contextObject, "contextObject must not be null");
    Assert.notNull(contextObjectName, "contextObjectName must not be null");

    final DataBinder binder = new WizardDataBinder(contextObject, contextObjectName);

    binder.setIgnoreUnknownFields(true);
    binder.setAutoGrowNestedPaths(true);
    binder.setConversionService(this.conversionService);
    binder.setValidator(this.delegatedValidator);
    binder.setMessageCodesResolver(new DefaultMessageCodesResolver());

    return binder;
}

From source file:org.cloudfoundry.identity.uaa.config.YamlBindingTests.java

private BindingResult bind(Object target, String values) {
    YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
    factory.setResources(new ByteArrayResource[] { new ByteArrayResource(values.getBytes()) });
    Map<Object, Object> map = factory.getObject();
    DataBinder binder = new DataBinder(target) {

        @Override//ww w  .j  av  a 2 s .  c o m
        protected void doBind(MutablePropertyValues mpvs) {
            modifyProperties(mpvs, getTarget());
            super.doBind(mpvs);
        }

        private void modifyProperties(MutablePropertyValues mpvs, Object target) {

            List<PropertyValue> list = mpvs.getPropertyValueList();
            BeanWrapperImpl bw = new BeanWrapperImpl(target);

            for (int i = 0; i < list.size(); i++) {
                PropertyValue pv = list.get(i);

                String name = pv.getName();
                StringBuilder builder = new StringBuilder();

                for (String key : StringUtils.delimitedListToStringArray(name, ".")) {
                    if (builder.length() != 0) {
                        builder.append(".");
                    }
                    builder.append(key);
                    String base = builder.toString();
                    Class<?> type = bw.getPropertyType(base);
                    if (type != null && Map.class.isAssignableFrom(type)) {
                        String suffix = name.substring(base.length());
                        Map<String, Object> nested = new LinkedHashMap<String, Object>();
                        if (bw.getPropertyValue(base) != null) {
                            @SuppressWarnings("unchecked")
                            Map<String, Object> existing = (Map<String, Object>) bw.getPropertyValue(base);
                            nested = existing;
                        } else {
                            bw.setPropertyValue(base, nested);
                        }
                        Map<String, Object> value = nested;
                        String[] tree = StringUtils.delimitedListToStringArray(suffix, ".");
                        for (int j = 1; j < tree.length - 1; j++) {
                            String subtree = tree[j];
                            value.put(subtree, nested);
                            value = nested;
                        }
                        String refName = base + suffix.replaceAll("\\.([a-zA-Z0-9]*)", "[$1]");
                        mpvs.setPropertyValueAt(new PropertyValue(refName, pv.getValue()), i);
                        break;
                    }
                }

            }

        }

    };
    binder.setIgnoreUnknownFields(false);
    LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean();
    validatorFactoryBean.afterPropertiesSet();
    binder.setValidator(validatorFactoryBean);
    binder.bind(new MutablePropertyValues(map));
    binder.validate();

    return binder.getBindingResult();
}