3 Replies Latest reply on Jan 30, 2014 12:01 PM by csa

    Standart Errai bind doesn't support empty values for numeric types

    arumad

      Errai DataBinder doesn't translate empty textbox values into null values in binded model.
      For example, suppose there are bindable class and autobound template

       

      @Bindable

      public class ValueHolder {

        private String comment; //get+set

        private BigDecimal amount; //get+set

      }

       

      @Templated

      public class ValueEditor {

        @Inject

        @AutoBound

        private DataBinder<T> binder;

        @Bound

        @DataField

        private TextArea comment = new TextArea();

        @Bound

        @DataField

        private TextBox amount = new TextBox();

      }

       

      It works perfectly fine with numeric values, but cleaning input would not clean model value,
      and change in comment input would cause amount to restore previously entered value.

      It's caused by NumberFormatException in BigDecimal constructor called from

      org.jboss.errai.databinding.client.api.Convert.to(BigDecimal.class, "")

       

      Is it designed to reject empty numeric inputs?

      If not, following patch to Convert allows to clean numeric inputs.

       

      public class Convert {

      + private static final HashSet<Class<?>> SUPPORTED_CLASSES = new HashSet<Class<?>>(Arrays.asList(

      + Integer.class, Long.class, Float.class, Double.class, Boolean.class, Date.class, BigDecimal.class, BigInteger.class));

      private static final Map<ConverterRegistrationKey, Converter> defaultConverters =

        new HashMap<ConverterRegistrationKey, Converter>();

      ===

        return o.toString();

        }

        else if (o.getClass().equals(String.class)) {

      + String string = (String) o;

      + if (string.length() == 0 && SUPPORTED_CLASSES.contains(toType)) {

      + return null;

      + }

        if (toType.equals(Integer.class)) {

        • 1. Re: Standart Errai bind doesn't support empty values for numeric types
          csa

          Hi Olexandr,

           

          Yes the built-in converters for BigDecimal and BigInteger didn't handle that case. They do now in the latest 3.0-SNAPSHOTs. If you can't upgrade you can always define your own binding specific converter (@Bound(converter=MyBigDecimalConverter.class)) or register a default converter for that type. Then you don't have to provide it at each binding.

           

          So, you could simply add this to a client package of your app:

           

          @DefaultConverter
          public class BigDecimalConverter implements Converter<BigDecimal, String> {
          
            @Override
            public BigDecimal toModelValue(String widgetValue) {
              return (widgetValue == null || widgetValue.isEmpty()) ? null : new BigDecimal(widgetValue);
            }
          
            @Override
            public String toWidgetValue(BigDecimal modelValue) {
              return (modelValue == null) ? "" : modelValue.toString();
            }
          }
          

           

          Cheers,

          Christian

          1 of 1 people found this helpful
          • 2. Re: Standart Errai bind doesn't support empty values for numeric types
            arumad

            Thanks.

            Why only BigInteger and BigDecimal are made nullable in 3.0-SNAPSHOT?

            Empty values for other types would still fail to reach model.

            • 3. Re: Standart Errai bind doesn't support empty values for numeric types
              csa

              I actually thought I did but you're right I didn't. It should be OK now. However, Boolean.parseBoolean("") is OK as it returns false. Everything else should default to null now.

               

              Thanks!