Search in sources :

Example 6 with DataBinder

use of org.springframework.validation.DataBinder in project spring-framework by spring-projects.

the class MoneyFormattingTests method testAmountWithNumberFormat3.

@Test
public void testAmountWithNumberFormat3() {
    FormattedMoneyHolder3 bean = new FormattedMoneyHolder3();
    DataBinder binder = new DataBinder(bean);
    binder.setConversionService(conversionService);
    MutablePropertyValues propertyValues = new MutablePropertyValues();
    propertyValues.add("amount", "10%");
    binder.bind(propertyValues);
    assertEquals(0, binder.getBindingResult().getErrorCount());
    assertEquals("10%", binder.getBindingResult().getFieldValue("amount"));
    assertTrue(bean.getAmount().getNumber().doubleValue() == 0.1d);
    assertEquals("USD", bean.getAmount().getCurrency().getCurrencyCode());
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DataBinder(org.springframework.validation.DataBinder) Test(org.junit.Test)

Example 7 with DataBinder

use of org.springframework.validation.DataBinder in project spring-framework by spring-projects.

the class JodaTimeFormattingTests method testBindDateWithoutErrorFallingBackToDateConstructor.

@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());
}
Also used : MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DataBinder(org.springframework.validation.DataBinder) Test(org.junit.Test)

Example 8 with DataBinder

use of org.springframework.validation.DataBinder in project spring-framework by spring-projects.

the class JodaTimeFormattingTests method setUp.

private void setUp(JodaTimeFormatterRegistrar registrar) {
    conversionService = new FormattingConversionService();
    DefaultConversionService.addDefaultConverters(conversionService);
    registrar.registerFormatters(conversionService);
    JodaTimeBean bean = new JodaTimeBean();
    bean.getChildren().add(new JodaTimeBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);
    LocaleContextHolder.setLocale(Locale.US);
    JodaTimeContext context = new JodaTimeContext();
    context.setTimeZone(DateTimeZone.forID("-05:00"));
    JodaTimeContextHolder.setJodaTimeContext(context);
}
Also used : DataBinder(org.springframework.validation.DataBinder) FormattingConversionService(org.springframework.format.support.FormattingConversionService)

Example 9 with DataBinder

use of org.springframework.validation.DataBinder in project spring-framework by spring-projects.

the class DateFormattingTests method setUp.

private void setUp(DateFormatterRegistrar registrar) {
    DefaultConversionService.addDefaultConverters(conversionService);
    registrar.registerFormatters(conversionService);
    SimpleDateBean bean = new SimpleDateBean();
    bean.getChildren().add(new SimpleDateBean());
    binder = new DataBinder(bean);
    binder.setConversionService(conversionService);
    LocaleContextHolder.setLocale(Locale.US);
}
Also used : DataBinder(org.springframework.validation.DataBinder)

Example 10 with DataBinder

use of org.springframework.validation.DataBinder in project spring-framework by spring-projects.

the class ServletModelAttributeMethodProcessor method createAttributeFromRequestValue.

/**
	 * Create a model attribute from a String request value (e.g. URI template
	 * variable, request parameter) using type conversion.
	 * <p>The default implementation converts only if there a registered
	 * {@link Converter} that can perform the conversion.
	 * @param sourceValue the source value to create the model attribute from
	 * @param attributeName the name of the attribute, never {@code null}
	 * @param methodParam the method parameter
	 * @param binderFactory for creating WebDataBinder instance
	 * @param request the current request
	 * @return the created model attribute, or {@code null}
	 * @throws Exception
	 */
protected Object createAttributeFromRequestValue(String sourceValue, String attributeName, MethodParameter methodParam, WebDataBinderFactory binderFactory, NativeWebRequest request) throws Exception {
    DataBinder binder = binderFactory.createBinder(request, null, attributeName);
    ConversionService conversionService = binder.getConversionService();
    if (conversionService != null) {
        TypeDescriptor source = TypeDescriptor.valueOf(String.class);
        TypeDescriptor target = new TypeDescriptor(methodParam);
        if (conversionService.canConvert(source, target)) {
            return binder.convertIfNecessary(sourceValue, methodParam.getParameterType(), methodParam);
        }
    }
    return null;
}
Also used : TypeDescriptor(org.springframework.core.convert.TypeDescriptor) ConversionService(org.springframework.core.convert.ConversionService) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) DataBinder(org.springframework.validation.DataBinder) WebDataBinder(org.springframework.web.bind.WebDataBinder)

Aggregations

DataBinder (org.springframework.validation.DataBinder)35 Test (org.junit.Test)15 Errors (org.springframework.validation.Errors)14 Locale (java.util.Locale)8 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)7 ArrayList (java.util.ArrayList)6 DOMSource (javax.xml.transform.dom.DOMSource)6 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)5 Element (org.w3c.dom.Element)5 HashMap (java.util.HashMap)4 FileItem (org.apache.commons.fileupload.FileItem)4 DiskFileItemFactory (org.apache.commons.fileupload.disk.DiskFileItemFactory)4 ServletFileUpload (org.apache.commons.fileupload.servlet.ServletFileUpload)4 MapPropertySource (org.springframework.core.env.MapPropertySource)4 PayloadRoot (org.springframework.ws.server.endpoint.annotation.PayloadRoot)4 File (java.io.File)3 LinkedHashMap (java.util.LinkedHashMap)3 FileProperties (org.akaza.openclinica.bean.rule.FileProperties)3 Study (org.akaza.openclinica.domain.datamap.Study)3 PFormCache (org.akaza.openclinica.web.pform.PFormCache)3