@Override public <C extends AbstractConfig> void bind(String prefix, C dubboConfig) { DataBinder dataBinder = new DataBinder(dubboConfig); // Set ignored* dataBinder.setIgnoreInvalidFields(isIgnoreInvalidFields()); dataBinder.setIgnoreUnknownFields(isIgnoreUnknownFields()); // Get properties under specified prefix from PropertySources Map<String, Object> properties = getSubProperties(getPropertySources(), prefix); // Convert Map to MutablePropertyValues MutablePropertyValues propertyValues = new MutablePropertyValues(properties); // Bind dataBinder.bind(propertyValues); }
@Test // SPR-15009 public void testSetMessageCodesResolverIsNullAfterInitializeBindingResult() { TestBean testBean = new TestBean(); DataBinder binder = new DataBinder(testBean, "testBean"); binder.initBeanPropertyAccess(); binder.setMessageCodesResolver(null); MutablePropertyValues mpv = new MutablePropertyValues(); mpv.add("age", "invalid"); binder.bind(mpv); assertEquals("typeMismatch", binder.getBindingResult().getFieldError("age").getCode()); // Keep a default MessageCodesResolver }
/** * Invoke the specified Validators, if any. * @see #setValidator(Validator) * @see #getBindingResult() */ public void validate() { Object target = getTarget(); Assert.state(target != null, "No target to validate"); BindingResult bindingResult = getBindingResult(); // Call each validator with the same binding result for (Validator validator : getValidators()) { validator.validate(target, bindingResult); } }
/** * Return the underlying TypeConverter of this binder's BindingResult. */ protected TypeConverter getTypeConverter() { if (getTarget() != null) { return getInternalBindingResult().getPropertyAccessor(); } else { return getSimpleTypeConverter(); } }
/** * Create the {@link AbstractPropertyBindingResult} instance using standard * JavaBean property access. * @since 4.2.1 */ protected AbstractPropertyBindingResult createBeanPropertyBindingResult() { BeanPropertyBindingResult result = new BeanPropertyBindingResult(getTarget(), getObjectName(), isAutoGrowNestedPaths(), getAutoGrowCollectionLimit()); if (this.conversionService != null) { result.initConversion(this.conversionService); } if (this.messageCodesResolver != null) { result.setMessageCodesResolver(this.messageCodesResolver); } return result; }
@Test public void testAmountWithNumberFormat2() { FormattedMoneyHolder2 bean = new FormattedMoneyHolder2(); DataBinder binder = new DataBinder(bean); binder.setConversionService(conversionService); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("amount", "10.50"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); assertEquals("10.5", binder.getBindingResult().getFieldValue("amount")); assertTrue(bean.getAmount().getNumber().doubleValue() == 10.5d); assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode()); }
@Test public void testCustomEditorForPrimitiveProperty() { TestBean tb = new TestBean(); DataBinder binder = new DataBinder(tb, "tb"); binder.registerCustomEditor(int.class, "age", new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new Integer(99)); } @Override public String getAsText() { return "argh"; } }); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("age", ""); binder.bind(pvs); assertEquals("argh", binder.getBindingResult().getFieldValue("age")); assertEquals(99, tb.getAge()); }
@Test public void testBindDateTime() { MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("dateTime", new DateTime(2009, 10, 31, 12, 0, ISOChronology.getInstanceUTC())); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); String value = binder.getBindingResult().getFieldValue("dateTime").toString(); assertTrue(value.startsWith("10/31/09")); }
Map<String, String> objectData = data; if(rootName == null) { dataBinder = new DataBinder(blankInstance()); } else { dataBinder = new DataBinder(blankInstance(), rootName); objectData = new HashMap<String,String>(); for(String key: data.keySet()) { dataBinder.setAllowedFields(allowedFields); dataBinder.setValidator(validator); dataBinder.setConversionService(play.data.format.Formatters.conversion); dataBinder.setAutoGrowNestedPaths(true); dataBinder.bind(new MutablePropertyValues(objectData)); Set<ConstraintViolation<Object>> validationErrors; if (groups != null) { validationErrors = validator.validate(dataBinder.getTarget(), groups); } else { validationErrors = validator.validate(dataBinder.getTarget()); BindingResult result = dataBinder.getBindingResult();
private void validate(Object properties) { DataBinder dataBinder = new DataBinder(properties); dataBinder.setValidator(validator); dataBinder.validate(); if (dataBinder.getBindingResult().hasErrors()) { throw new IllegalStateException(dataBinder.getBindingResult().toString()); } }
@Test public void testBindingWithCustomEditorOnObjectField() { BeanWithObjectProperty tb = new BeanWithObjectProperty(); DataBinder binder = new DataBinder(tb); binder.registerCustomEditor(Integer.class, "object", new CustomNumberEditor(Integer.class, true)); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("object", "1"); binder.bind(pvs); assertEquals(new Integer(1), tb.getObject()); }
@SuppressWarnings("unchecked") private void bindAndValidate(Map<String, String> raw) throws BindException { DataBinder dataBinder = new DataBinder(beanWrapper.getWrappedInstance()); dataBinder.setIgnoreUnknownFields(false); dataBinder.setConversionService(conversionService); MutablePropertySources mps = new MutablePropertySources(); mps.addFirst(new MapPropertySource("options", (Map) raw)); try { dataBinder.bind(new PropertySourcesPropertyValues(mps)); } catch (InvalidPropertyException e) { dataBinder.getBindingResult().addError(new FieldError("options", e.getPropertyName(), e.getMessage())); } CustomValidatorBean validator = new CustomValidatorBean(); validator.afterPropertiesSet(); dataBinder.setValidator(validator); Class<?>[] groups = determineGroupsToUse(beanWrapper.getWrappedInstance()); dataBinder.validate((Object[]) groups); if (dataBinder.getBindingResult().hasErrors()) { throw new BindException(dataBinder.getBindingResult()); } }
private DataBinder dataBinder(String... allowedFields) { DataBinder dataBinder; if (rootName == null) { dataBinder = new DataBinder(blankInstance()); } else { dataBinder = new DataBinder(blankInstance(), rootName); } if (allowedFields.length > 0) { dataBinder.setAllowedFields(allowedFields); } SpringValidatorAdapter validator = new SpringValidatorAdapter(this.validatorFactory.getValidator()); dataBinder.setValidator(validator); dataBinder.setConversionService(formatters.conversion); dataBinder.setAutoGrowNestedPaths(true); if(this.directFieldAccess) { // FYI: initBeanPropertyAccess() is the default, let's switch to direct field access instead dataBinder.initDirectFieldAccess(); // this should happen last, when everything else was set on the dataBinder already } return dataBinder; }
@Test // SPR-14888 public void testSetAutoGrowCollectionLimit() { BeanWithIntegerList tb = new BeanWithIntegerList(); DataBinder binder = new DataBinder(tb); binder.setAutoGrowCollectionLimit(257); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("integerList[256]", "1"); binder.bind(pvs); assertEquals(257, tb.getIntegerList().size()); assertEquals(Integer.valueOf(1), tb.getIntegerList().get(256)); assertEquals(Integer.valueOf(1), binder.getBindingResult().getFieldValue("integerList[256]")); }
@Test public void testBindDateWithoutErrorFallingBackToDateConstructor() { DataBinder binder = new DataBinder(new JodaTimeBean()); MutablePropertyValues propertyValues = new MutablePropertyValues(); propertyValues.add("date", "Sat, 12 Aug 1995 13:30:00 GMT"); binder.bind(propertyValues); assertEquals(0, binder.getBindingResult().getErrorCount()); }
@Test public void testBindingWithSystemFieldError() { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("class.classLoader.URLs[0]", "http://myserver"); binder.setIgnoreUnknownFields(false); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { assertTrue(ex.getMessage().contains("classLoader")); } }
@Test public void bindingNoErrorsNotIgnoreUnknown() throws Exception { FieldAccessBean rod = new FieldAccessBean(); DataBinder binder = new DataBinder(rod, "person"); binder.initDirectFieldAccess(); binder.setIgnoreUnknownFields(false); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.addPropertyValue(new PropertyValue("name", "Rod")); pvs.addPropertyValue(new PropertyValue("age", new Integer(32))); pvs.addPropertyValue(new PropertyValue("nonExisting", "someValue")); try { binder.bind(pvs); fail("Should have thrown NotWritablePropertyException"); } catch (NotWritablePropertyException ex) { // expected } }
binder.setIgnoreUnknownFields(false); LocalValidatorFactoryBean validatorFactoryBean = new LocalValidatorFactoryBean(); validatorFactoryBean.afterPropertiesSet(); binder.setValidator(validatorFactoryBean); binder.bind(new MutablePropertyValues(map)); binder.validate(); return binder.getBindingResult();
@Test public void testConversionWithInappropriateStringEditor() { DataBinder dataBinder = new DataBinder(null); DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(); dataBinder.setConversionService(conversionService); dataBinder.registerCustomEditor(String.class, new StringTrimmerEditor(true)); NameBean bean = new NameBean("Fred"); assertEquals("ConversionService should have invoked toString()", "Fred", dataBinder.convertIfNecessary(bean, String.class)); conversionService.addConverter(new NameBeanConverter()); assertEquals("Type converter should have been used", "[Fred]", dataBinder.convertIfNecessary(bean, String.class)); }
@Test public void testBindingNoErrorsWithIgnoreInvalid() { TestBean rod = new TestBean(); DataBinder binder = new DataBinder(rod, "person"); binder.setIgnoreInvalidFields(true); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("name", "Rod"); pvs.add("spouse.age", 32); binder.bind(pvs); }