Announcement

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

    Adding Custom Validator to FilterBuilder.

    I was hoping to add a custom validator to DataSourceField so that errors shows up for the field when using FilterBuilder, unfortunately it does not work.

    I used the following for my sample: http://www.smartclient.com/smartgwt/...r_builder_grid and added a custom validator to DataSourceField. Basic DataType validation works i.e. if I enter string for float field the eror shows up. I am not sure why CustomValidator does not work.

    1. WorldXmlDS DataSource with CustomValidator for one of the field.

    Code:
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.data.fields.DataSourceBooleanField;
    import com.smartgwt.client.data.fields.DataSourceDateField;
    import com.smartgwt.client.data.fields.DataSourceFloatField;
    import com.smartgwt.client.data.fields.DataSourceIntegerField;
    import com.smartgwt.client.data.fields.DataSourceTextField;
    import com.smartgwt.client.widgets.form.validator.CustomValidator;
    
    public class WorldXmlDS extends DataSource {
    
        private static WorldXmlDS instance = null;
    
        public static WorldXmlDS getInstance() {
    
            if ( instance == null ) {
                instance = new WorldXmlDS( "worldDS" );
            }
            return instance;
        }
    
        public WorldXmlDS( String id ) {
    
            setID( id );
            setRecordXPath( "/List/country" );
            DataSourceIntegerField pkField = new DataSourceIntegerField( "pk" );
            pkField.setHidden( true );
            pkField.setPrimaryKey( true );
    
            DataSourceTextField countryCodeField = new DataSourceTextField( "countryCode", "Code" );
            countryCodeField.setValidators( new CustomValidator() {
    
                @Override
                protected boolean condition( Object value ) {
    
                    if ( value != null && value.toString().length() > 2 ) {
                        return false;
                    } else {
                        return true;
                    }
                }
            } );
            countryCodeField.setRequired( true );
    
            DataSourceTextField countryNameField = new DataSourceTextField( "countryName", "Country" );
            countryNameField.setRequired( true );
    
            DataSourceTextField capitalField = new DataSourceTextField( "capital", "Capital" );
            DataSourceTextField governmentField = new DataSourceTextField( "government", "Government", 500 );
    
            DataSourceBooleanField memberG8Field = new DataSourceBooleanField( "member_g8", "G8" );
    
            DataSourceTextField continentField = new DataSourceTextField( "continent", "Continent" );
            continentField.setValueMap( "Europe", "Asia", "North America", "Australia/Oceania", "South America", "Africa" );
    
            DataSourceDateField independenceField = new DataSourceDateField( "independence", "Nationhood" );
            DataSourceFloatField areaField = new DataSourceFloatField( "area", "Area (kmē)" );
            DataSourceIntegerField populationField = new DataSourceIntegerField( "population", "Population" );
            DataSourceFloatField gdpField = new DataSourceFloatField( "gdp", "GDP ($M)" );
    
            setFields( pkField, countryCodeField, countryNameField, capitalField, governmentField, memberG8Field, continentField, independenceField, areaField, populationField,
                    gdpField );
    
            setDataURL( "ds/world.data.xml" );
            setClientOnly( true );
        }
    }
    2. XML Data

    Code:
    <List>
    
    <country>
        <continent>North America</continent>
        <countryName>Bermuda</countryName>
        <countryCode>BD</countryCode>
        <area>50</area>
        <population>62099</population>
        <gdp>1700</gdp>
        <government>dependent territory of the UK</government>
        <capital>Hamilton</capital>
    </country>
    <country>
        <continent>North America</continent>
        <countryName>United States</countryName>
        <countryCode>US</countryCode>
        <area>9372610</area>
        <population>266476278</population>
        <gdp>7247700</gdp>
        <independence>1776-07-04</independence>
        <government>federal republic</government>
        <capital>Washington</capital>
    </country>
    <country>
        <continent>Europe</continent>
        <countryName>Monaco</countryName>
        <countryCode>MN</countryCode>
        <area>1.9</area>
        <population>31719</population>
        <gdp>788</gdp>
        <independence>1419-01-01</independence>
        <government>constitutional monarchy</government>
        <capital>Monaco</capital>
    </country>
    <country>
        <continent>Europe</continent>
        <countryName>Norway</countryName>
        <countryCode>NO</countryCode>
        <area>324220</area>
        <population>4383807</population>
        <gdp>106200</gdp>
        <independence>1905-10-26</independence>
        <government>constitutional monarchy</government>
        <capital>Oslo</capital>
    </country>
    <country>
        <continent>North America</continent>
        <countryName>Bahamas</countryName>
        <countryCode>BF</countryCode>
        <area>13940</area>
        <population>259367</population>
        <gdp>4800</gdp>
        <independence>1973-07-10</independence>
        <government>commonwealth</government>
        <capital>Nassau</capital>
    </country>
    <country>
        <continent>Africa</continent>
        <countryName>Zaire</countryName>
        <countryCode>CG</countryCode>
        <area>2345410</area>
        <population>46498539</population>
        <gdp>16500</gdp>
        <independence>1960-06-30</independence>
        <government>republic with a strong presidential system</government>
        <capital>Kinshasa</capital>
    </country>
    
    </List>
    3. Entry Point Module, I tried applying the CustomValidator once more here with or without it the validation does not happen

    Code:
    import com.google.gwt.i18n.client.NumberFormat;  
    import com.smartgwt.client.data.DataSource;  
    import com.smartgwt.client.data.DataSourceField;
    import com.smartgwt.client.types.ListGridFieldType;  
    import com.smartgwt.client.widgets.Canvas;  
    import com.smartgwt.client.widgets.IButton;  
    import com.smartgwt.client.widgets.events.ClickEvent;  
    import com.smartgwt.client.widgets.events.ClickHandler;  
    import com.smartgwt.client.widgets.form.FilterBuilder;  
    import com.smartgwt.client.widgets.grid.CellFormatter;  
    import com.smartgwt.client.widgets.grid.ListGrid;  
    import com.smartgwt.client.widgets.grid.ListGridField;  
    import com.smartgwt.client.widgets.grid.ListGridRecord;  
    import com.smartgwt.client.widgets.layout.VStack;  
    import com.smartgwt.sample.showcase.client.data.WorldXmlDS;  
      
    import com.google.gwt.core.client.EntryPoint;  
      
    public class GridNestedFilterBulderSample implements EntryPoint {  
      
        public void onModuleLoad() {  
      
            DataSource worldDS = WorldXmlDS.getInstance();
            
           // Start ---Try explicit custom validator on DS Fields once more -- Does not make any difference with or witrhout this code
            DataSourceField[] dsFields = worldDS.getFields();
            for ( final DataSourceField dataSourceField : dsFields ) {
                if ( dataSourceField.getName().equalsIgnoreCase( "countryCode" ) ) {
                    dataSourceField.setValidators( new CustomValidator() {
    
                        @Override
                        protected boolean condition( Object value ) {
    
                            if ( value != null && value.toString().length() > 2 ) {
                                return false;
                            } else {
                                return true;
                            }
                        }
                    } );
                }
            }        
           //End--- Try explicit custom validator on DS Fields once more -- Does not make any difference with or witrhout this code
      
            final FilterBuilder filterBuilder = new FilterBuilder();  
            filterBuilder.setDataSource(worldDS);  
            filterBuilder.setValidateOnChange( Boolean.TRUE );
      
            final ListGrid countryGrid = new ListGrid();  
            countryGrid.setWidth(550);  
            countryGrid.setHeight(224);  
            countryGrid.setDataSource(worldDS);  
            countryGrid.setAutoFetchData(true);  
      
            ListGridField nameField = new ListGridField("countryName", "Country");  
            ListGridField continentField = new ListGridField("continent", "Continent");  
            ListGridField memberG8Field = new ListGridField("member_g8", "Member G8");  
            memberG8Field.setCanEdit(false);  
      
            ListGridField populationField = new ListGridField("population", "Population");  
            populationField.setType(ListGridFieldType.INTEGER);  
            populationField.setCellFormatter(new CellFormatter() {  
                public String format(Object value, ListGridRecord record, int rowNum, int colNum) {  
                    if(value == null) return null;  
                    try {  
                        NumberFormat nf = NumberFormat.getFormat("0,000");  
                        return nf.format(((Number) value).longValue());  
                    } catch (Exception e) {  
                        return value.toString();  
                    }  
                }  
            });  
            ListGridField independenceField = new ListGridField("independence", "Independence");  
            countryGrid.setFields(nameField,continentField, memberG8Field, populationField, independenceField);  
      
            IButton filterButton = new IButton("Filter");  
            filterButton.addClickHandler(new ClickHandler() {  
                public void onClick(ClickEvent event) {  
                    filterBuilder.validate();
                    countryGrid.filterData(filterBuilder.getCriteria());  
                }  
            });  
      
            VStack vStack = new VStack(10);  
            vStack.addMember(filterBuilder);  
            vStack.addMember(filterButton);  
            vStack.addMember(countryGrid);  
      
            vStack.draw();  
        }  
      
    }
    4. I also tried overriding with custom formitem via - dataSourceField.setEditorProperties(...), stick no custom validation

    via Updated WorldXmlDS.java DataSoruce Field to include explicity setEditorProperties with custom validator
    Code:
            .....
            ...
           DataSourceTextField countryCodeField = new DataSourceTextField( "countryCode", "Code" );
            TextItem editor = new TextItem( "countryCode" );
            editor.setShowTitle( Boolean.FALSE );
            editor.setValidators( new CustomValidator() {
                @Override
                protected boolean condition( Object value ) {
    
                    if ( value != null && value.toString().length() > 2 ) {
                        return false;
                    } else {
                        return true;
                    }
                }
            } );
            countryCodeField.setEditorProperties( editor );
            ......
            ......
            .......


    5. Using SmartGWT Power Edition Build 5.1p-2016-08-06
    SGWT library to 5.1p - 2016-08-06
    Build fixes the bug for FF 48.0.
    http://forums.smartclient.com/forum/...ascript-errors



    6. Tested on FireFox ESR 45.4.0
    Last edited by malcolm.pereira; 13 Oct 2016, 07:33.

    #2
    Validators do not in general apply to search input, for example, a search form should not require that what has been entered in the search field constitutes a valid zip code, email address, phone number, etc.

    For the rare cases where you need to give feedback to the user that a certain search string is fundamentally invalid (as opposed to just not returning results) we recommend signaling a general failure from the "fetch" operation and explaining the problem in the error message. See the Error Handling overview of you are not familiar with how to do this.

    You could also just catch the error client side by examining the criteria.

    Comment

    Working...
    X