Announcement

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

    Dependent fields custom validator gets value=null while grid in-line editing

    Hi,
    There is custom validator attached to DataSource field 'dateTo'. This validator depends on 'validFrom' field.
    Edit enabled ListGrid uses this DataSource:
    Code:
    import com.google.gwt.core.client.EntryPoint;
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.data.DataSourceField;
    import com.smartgwt.client.data.Record;
    import com.smartgwt.client.data.fields.DataSourceDateField;
    import com.smartgwt.client.data.fields.DataSourceTextField;
    import com.smartgwt.client.types.FieldType;
    import com.smartgwt.client.util.SC;
    import com.smartgwt.client.widgets.Button;
    import com.smartgwt.client.widgets.events.ClickEvent;
    import com.smartgwt.client.widgets.events.ClickHandler;
    import com.smartgwt.client.widgets.events.DrawEvent;
    import com.smartgwt.client.widgets.events.DrawHandler;
    import com.smartgwt.client.widgets.form.validator.CustomValidator;
    import com.smartgwt.client.widgets.grid.ListGrid;
    import com.smartgwt.client.widgets.layout.VLayout;
    import java.util.Date;
    import java.util.HashMap;
    
    public class MainEntryPoint implements EntryPoint {
     
        public void onModuleLoad() {
            layout();
        }
     
        private void layout() {
    
            DataSource projectDS = new DataSource();
            projectDS.setClientOnly(true);
          
            DataSourceField idField = new DataSourceField();
            idField.setType(FieldType.SEQUENCE);
            idField.setName("id");
            idField.setPrimaryKey(true);
            idField.setHidden(true);
            DataSourceTextField codeField = new DataSourceTextField();
            codeField.setName("code");
            DataSourceDateField validFromField = new DataSourceDateField();
            validFromField.setName("validFrom");
            DataSourceDateField validToField = new DataSourceDateField();
            validToField.setName("validTo");
            DateRangeFieldValidator dateRangeFieldValidator = new DateRangeFieldValidator();
            dateRangeFieldValidator.setDependentFields(new String[] {"validFrom"});
            validToField.setValidators(dateRangeFieldValidator);
          
            projectDS.setFields(idField, codeField, validFromField, validToField);
          
            final ListGrid lg = new ListGrid();
            lg.setDataSource(projectDS);
            lg.setCanEdit(true);
            lg.setAutoSaveEdits(false);
            lg.setWidth(300);
            lg.addDrawHandler(new DrawHandler() {
    
                public void onDraw(DrawEvent event) {
                    HashMap map = new HashMap();
                    map.put("id", "0");
                    map.put("validFrom", new Date());
                    map.put("validTo", new Date());
                    //lg.startEditingNew(map);
                    Record[] records = new Record[1] ;
                    records[0] = new Record(map);
                    lg.setData(records);
                }
            });
            
            Button validateButton = new Button("Validate");
            validateButton.addClickHandler(new ClickHandler() {
    
                public void onClick(ClickEvent event) {
                    lg.validateRow(0);
                }
            });
            
            VLayout layout = new VLayout();
            layout.addMember(lg);
            layout.addMember(validateButton);
            
            layout.draw();
        }
        
        public class DateRangeFieldValidator extends CustomValidator {
    
            @Override
            protected boolean condition(Object value) {
    
                SC.logWarn("value=" + value);
                SC.logWarn("record=" + getRecord());
                return true;
            }
        }
     
    }
    If you run this test case, enter grid's edit mode and change 'validTo' field value (the one validator is attached), then console shows this:
    Code:
    14:06:17.422:MDN6:WARN:Log:value=Wed Jul 13 12:00:00 GMT+200 2016
    14:06:17.423:MDN6:WARN:Log:record=com.smartgwt.client.data.Record@5
    14:06:17.526:MUP5:WARN:Log:value=Wed Jul 13 12:00:00 GMT+200 2016
    14:06:17.527:MUP5:WARN:Log:record=com.smartgwt.client.data.Record@6
    But if you run test case again and this time change 'validFrom' field value (dependent field), then console shows:
    Code:
    14:43:23.937:MDN4:WARN:Log:value=null
    14:43:23.938:MDN4:WARN:Log:record=com.smartgwt.client.data.Record@5
    14:43:24.024:MUP2:WARN:Log:value=null
    14:43:24.025:MUP2:WARN:Log:record=com.smartgwt.client.data.Record@6
    Value in condition() method is not set (null), so validator is not able to work properly in real application.
    Thanks,
    MichalG
    SmartClient Version: v11.0p_2016-07-07/LGPL Development Only (built 2016-07-07)
    FF 24.8.0 and Chrominium 38.0.2125.101
    Last edited by michalg; 11 Jul 2016, 04:53.

    #2
    If you have no value for validTo, and you edit validFrom and enter a value there, then when the validator is triggered on validTo, you should expect null as the value, because that's the current value of validTo.

    It seems like you might be expecting the framework to pass you the value from validFrom, but that definitely doesn't make sense - there would be no way for you to know whether you were working with the value from the field where the validator is defined, vs a value from any of the dependent fields. Not to mention that the dependent fields might be of different type.

    Comment


      #3
      Both validFrom and validTo have values because they are initially set by this code:
      Code:
                  public void onDraw(DrawEvent event) {
                      HashMap map = new HashMap();
                      map.put("id", "0");
                      map.put("validFrom", new Date());
                      map.put("validTo", new Date());
                      //lg.startEditingNew(map);
                      Record[] records = new Record[1] ;
                      records[0] = new Record(map);
                      lg.setData(records);
                  }
              });
      Click image for larger version

Name:	depvalid0.png
Views:	117
Size:	5.7 KB
ID:	239097
      Now, I am entering edit mode and change value of validFrom field. So, both date fields have values. And no, I am not expecting validFrom value in validTo field validator. I am expecting validator field value which is validTo in this case. Thank you, MichalG

      Comment


        #4
        We've made a fix to address the problem in SGWT 6.0p/SC 11.0p and newer that will be in the nightly builds dated 2016-07-14 and beyond.

        Comment


          #5
          Verified with build 2016-07-14 as fixed.
          Thank you,
          MichalG

          Comment

          Working...
          X