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
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; } }
Comment