Announcement

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

    Validation not fired when calling grid.ValidateRow()

    I have created a custom Validator which checks for duplicate rows in a ListGrid. however, using the below code:

    1) If you press StartEditingNew button and try to put "Asia", the custom validator runs and you get the error msg "duplicate row.."
    2) If you press the Validate button, nothing happens although the grid has 2 rows with the Duplicate value "Asia"

    Any explanation?

    I am using smartgwt 3.0

    Code:
    public void onModuleLoad() {
        DataSourceTextField continentField = new DataSourceTextField("continent");
        continentField.setPrimaryKey(true);
    
        DataSource dataSource = new DataSource();
        dataSource.setClientOnly(true);
        dataSource.setFields(continentField);
        dataSource.setCacheData(new CountryData().getNewRecords());
    
        final ListGrid myGrid = new ListGrid();
        myGrid.setWidth(200);
        myGrid.setHeight(100);
        myGrid.setDataSource(dataSource);
        myGrid.fetchData();
    
        
        IButton validateButton = new IButton("Validate row 2");
         validateButton.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                myGrid.validateRow(2);
            }
        });
        
        IButton startButton = new IButton("StartEditingNew");
        startButton.addClickHandler(new ClickHandler() {
            
            @Override
            public void onClick(ClickEvent event) {
                myGrid.startEditingNew();
            }
        });
        
        HLayout layout = new HLayout(10);
        layout.addMember(myGrid);
        layout.addMember(startButton);
        layout.addMember(validateButton);
        layout.draw();
        
        myGrid.getFields()[0].setValidators(new GridUniqueValidator(myGrid));
    }
    
    class CountryData {
    
        public CountryRecord[] getNewRecords() {
            return new CountryRecord[] { new CountryRecord("North America"), new CountryRecord("Asia"), new CountryRecord("Asia") };
        }
    }
    
    class CountryRecord extends ListGridRecord {
        public CountryRecord(String continent) {
            setContinent(continent);
        }
    
        public void setContinent(String continent) {
            setAttribute("continent", continent);
        }
    
        public String getContinent() {
            return getAttributeAsString("continent");
        }
    }
    
    class GridUniqueValidator extends CustomValidator {
    
        ListGrid listGrid;
    
        public GridUniqueValidator(ListGrid listGrid) {
            this.listGrid = listGrid;
        }
    
        @Override
        protected boolean condition(Object value) {
            Record newRecord = getRecord();
            String attributeName = newRecord.getAttributes()[0];
            for (Record record : listGrid.getRecords()) {
                if (record.getAttribute(attributeName).equals(newRecord.getAttribute(attributeName))) {
                    setErrorMessage("Duplicate value at row: " + listGrid.getRecordIndex(record));
                    return false;
                }
            }
            return true;
        }
    
    }

    #2
    Bump Bump ..

    Comment


      #3
      Experiencing the same issue. Although thought it was a mistake on my end....
      Seems not.

      I worked around it by checking it on our custom server backend. But it would be nicer to have it clearly shown client side....

      Comment


        #4
        hopefully, one day we will get an answer :)

        Comment

        Working...
        X