Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Type of Object Passed to condition() in CustomValidator

    Hi Isomorphic. I am trying to set a CustomValidator for a ListGridField, but when I try to use the 'value' Object that's passed to the condition() method, I keep getting exceptions no matter the approach.

    I've tried:
    Code:
    (String) value
    String.valueOf(value)
    Double.valueOf(value)
    NumberFormat.getDecimalFormat().parse(value)
    JSOHelper.convertToString(value)
    An example of the exception that I get (when the value I put in the ListGridField is 5.55) is "java.lang.IllegalArgumentException: Object 5.55 is not of type String".

    Can you please help me figuring out how to parse/cast and use this value?

    I am using SmartGWT 6.0p 2016-09-27 and Chrome Version 53.0.2785.116 m.
    Last edited by carlossierra; 27 Sep 2016, 11:12.

    #2
    Those approaches should be working for a normal float field at least.

    This looks like GWT has become internally confused about types. You should try clearing the GWT unitCache, browser cache, and restarting your browser and IDE.

    If that doesn't work: does this happen in compiled mode too? Is an exception reported in the Developer Console?

    We will probably need a way to reproduce the problem to look further.

    Comment


      #3
      Hi Isomorphic. No exceptions are reported in the Developer Console.

      Please take a look at this simple test case:

      Code:
      [B]TestCases.java[/B]
      public class TestCases implements EntryPoint {  
          
          public void onModuleLoad() {       
              ListGrid grid = new ListGrid(DataSource.get("buffer_history"));
              grid.setAutoFetchData(true);
              grid.setCanEdit(true);
              grid.setUseAllDataSourceFields(true);
              
              ListGridField bufferSize = new ListGridField("buffer_size");
              bufferSize.setValidators(new CustomValidator() {
                  @Override
                  protected boolean condition(Object value) {
                      try {
                          String stringValue = JSOHelper.convertToString(value);  // <-- throws exception
                          double parsedValue = NumberFormat.getDecimalFormat().parse(stringValue);
                          if (parsedValue > 0) { 
                              return true;
                          } else {
                              return false;
                          }
                      } catch (Exception e) {
                          // getting the value failed, so the validation fails.
                          return false;
                      }
                  }    
              });
              
              grid.setFields(bufferSize);    
              
              HLayout layout = new HLayout();
              layout.setWidth100();
              layout.setHeight100();
              layout.addMember(grid);
              layout.show();
          }
      }
      Code:
      [B]buffer_history.ds.xml[/B]
      <?xml version="1.0" encoding="utf-8"?>
      <DataSource 
          ID="buffer_history" 
          serverType="sql" 
          tableName="buffer_history" 
          xmlns:fmt="focuss/fmt">    
      
          <fields>
              <field name="id" primaryKey="true" hidden="true"/>
              <field name="sku"/>
              <field name="location"/>
              <field name="buffer_size" type="localeFloat"/>
              <field name="unit_price" type="localeCurrency"/>
              <field name="movements_update_date" format="dd/MM/yyyy"/>        
          </fields>
      </DataSource>
      I am using SmartGWT 6.0p 2016-09-27 and Version 53.0.2785.116 m

      Comment


        #4
        Hi,

        please, take into account that the Object passed in as an argument to the CustomValidator.condition() method is in fact a java native object, not a javascript object. The object will be a java.lang.Double or java.lang.Integer when double or integer values are found by the internal parser, or java.lang.String when the values cannot be parsed as Numbers (i.e. "a", "+", etc), so you can always use the toString() method to obtain the parsed value as a java.lang.String.

        In other words, if you replace
        Code:
        String stringValue = JSOHelper.convertToString(value);  // <-- throws exception
        with
        Code:
        String stringValue = value.toString();
        in your sample, it will work as expected.

        Regarding the usage of JSOHelper.convertToString(), this is an internal helper method with a different behavior of the one expected from its name, in general you shouldn't call methods where the documentation have no description at all of the behavior, like this one.

        Comment


          #5
          Thanks Isomorphic. This works as expected. I didn't try it before, but in retrospective, toString() is the easiest way to do it.
          Regarding the other suggestion, I'll take it into consideration so that I don't end up using unsupported methods.

          As always, thanks for your valuable help!

          Comment

          Working...
          X