Announcement

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

    FormItemValueFormatter is never called on ComboBoxItem

    The real title should be : FormItemValueFormatter.formatValue is never called on the ComboBoxItem/SelectItem if the item has optional Data source and Form has another Data Source
    Here's the changed example from the Showcase:
    Code:
    package com.smartgwt.sample.showcase.client.dataintegration.java.form;
    
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.data.Record;
    import com.smartgwt.client.widgets.Canvas;
    import com.smartgwt.client.widgets.events.ClickEvent;
    import com.smartgwt.client.widgets.events.ClickHandler;
    import com.smartgwt.client.widgets.form.DynamicForm;
    import com.smartgwt.client.widgets.form.FormItemValueFormatter;
    import com.smartgwt.client.widgets.form.fields.FormItem;
    import com.smartgwt.client.widgets.form.fields.HeaderItem;
    import com.smartgwt.client.widgets.form.fields.ComboBoxItem;
    import com.smartgwt.client.widgets.form.fields.IntegerItem;
    import com.smartgwt.client.widgets.form.fields.SelectItem;
    import com.smartgwt.client.widgets.form.fields.TextAreaItem;
    import com.smartgwt.client.widgets.form.fields.ButtonItem;
    import com.smartgwt.sample.showcase.client.PanelFactory;
    import com.smartgwt.sample.showcase.client.ShowcasePanel;
    
    
    public class InlineScriptValidationSample extends ShowcasePanel {
        private static final String DESCRIPTION = "Formatter is not called";
            
        public static class Factory implements PanelFactory {
    
            private String id;
    
            public Canvas create() {
                InlineScriptValidationSample panel = new InlineScriptValidationSample();
                id = panel.getID();
                return panel;
            }
    
            public String getID() {
                return id;
            }
    
            public String getDescription() {
                return DESCRIPTION;
            }
        }
    
        public Canvas getViewPanel() {
            final DynamicForm form = new DynamicForm();
            DataSource dataSource = DataSource.get("inlineScript_orderForm");
            form.setDataSource(dataSource);
    
            HeaderItem header = new HeaderItem("header");
            header.setDefaultValue("Add an item to your Order");
    
            SelectItem sitem = new SelectItem("itemId", "Item");
            sitem.setOptionDataSource(DataSource.get("StockItem"));
            sitem.setValueField("id");
            sitem.setDisplayField("description");
            sitem.setValueFormatter(new FormItemValueFormatter() {
                
                @Override
                public String formatValue(Object value, Record record, DynamicForm form,
                        FormItem item) {
                    System.out.println("SelectItem Formatter is called");
                    if ( value != null) {
                    String v = value.toString() + "456";
                    return v;
                    }
                    return (String)value;
                }
            });
            
            
            ComboBoxItem item = new ComboBoxItem("itemId", "Item");
            item.setOptionDataSource(DataSource.get("StockItem"));
            item.setValueField("id");
            item.setDisplayField("description");
            item.setValueFormatter(new FormItemValueFormatter() {
                
                @Override
                public String formatValue(Object value, Record record, DynamicForm form,
                        FormItem item) {
                    System.out.println("ComboBoxItem Formatter is called");
                    if ( value != null) {
                    String v = value.toString() + "123";
                    return v;
                    }
                    return (String)value;
                }
            });
            
        
            IntegerItem quantity = new IntegerItem("quantity");
            quantity.setValidateOnExit(true);
            
            TextAreaItem instructions = new TextAreaItem("instructions");
            
            ButtonItem submit = new ButtonItem("submit", "Submit Order");
            
            form.setFields(header, item, sitem, quantity, instructions, submit);
            
            return form;
        }
    
        public String getIntro() {
            return DESCRIPTION;
        }
    
    }
    Code:
    <DataSource ID="inlineScript_orderForm" serverType="sql">
        <fields>
            <field name="orderItem" type="sequence" primaryKey="true"/>
            <field name="itemId" foreignKey="StockItem.id"/>
            <field name="quantity" type="integer">
                <validators>
                    <validator type="serverCustom">
                        <serverCondition language="groovy"><![CDATA[
                            value < dataSources.StockItem.fetchById(record.itemId).quantity
                        ]]></serverCondition>
                        
                        <!-- serverCondition language="java"><![CDATA[
                            DataSource ds = (DataSource) dataSources.get("StockItem");
                            Map rec = ds.fetchById(record.get("itemId"));
                            Integer quantity = (Integer) rec.get("quantity");
                            return value < quantity;
                        ]]></serverCondition -->
                        
                        <!-- serverCondition language="javascript"><![CDATA[
                            value < dataSources.get("StockItem").fetchById(record.get("itemId")).get("quantity")
                        ]]></serverCondition -->
                        <errorMessage>Not enough in stock</errorMessage>
                    </validator>
                </validators>
            </field>
            <field name="instructions" type="text"/>
         </fields>
    </DataSource>
    Code:
    <DataSource
        ID="StockItem" serverType="sql"
    >
        <fields>
            <field name="id" type="integer" primaryKey="true" />
            <field name="stockCode"   title="Stock Code"  type="text" />
            <field name="description" title="Description" type="text" />
            <field name="price"       title="Unit Price"  type="float" />
            <field name="quantity"    title="Quantity"    type="integer" />
         </fields>
    </DataSource>
    obviously you can use the InlineScriptValidationSample provided as example.
    As you can see I added
    item.setValueFormatter(new FormItemValueFormatter() {...}) // for ComboBoxItem
    and
    sitem.setValueFormatter(new FormItemValueFormatter() {...}) // for SelectItem
    The Formatter on the ComboBoxItem was NEVER invoked.
    The Formatter on the SelectItem worked as expected:
    the "SelectItem Formatter is called" was printed out
    the displayed value have the "456" appendix in every line.
    used version is
    SmartClient Version: v8.3_2012-11-20/Enterprise Deployment (built 2012-11-20)
    I obviously tried other versions as well
    SmartClient Version: v10.0p_2015-08-28/PowerEdition Deployment (built 2015-08-28)
    In the later version the SelectItem.setValueFormatter(new FormItemValueFormatter() {...});
    public String formatValue Is never called at all.

    Please explain how I can format the display value (which is not date or time, but string) on the client side.
    My real problem here as the following: sometimes the displayedField is null, while the value field is a valid integer.
    In this case I want to display for such a value "The value is hidden" as displayed value.

    Thanks
    Last edited by aafros; 26 Nov 2015, 07:48.

    #2
    I tried 2 following versions:
    SmartClient Version: v9.0p_2014-04-23/PowerEdition Deployment (built 2014-04-23)
    SmartClient Version: v10.0p_2015-08-28/PowerEdition Deployment (built 2015-08-28)

    1. The ComboBoxItem - only the setEditorValueFormatter was invoked. The setValueFormatter never invoked.
    2, The SelectItem -none setValueFormatter or setEditorValueFormatter was invoked at all.
    here's the code i used:
    Code:
    final ComboBoxItem destination = new ComboBoxItem( "alert_policy_dispatch_dir_id",
                    "My Title" );
            destination.setOptionDataSource( NotificationDSLocalizeFactory.getDS(
                    NotificationDSLocalizeFactory.DSType.alert_policy_dispatch_directive ) );
            destination.setDisplayField( "alert_policy_dispatch_name" );
            destination.setValueField( "alert_policy_dispatch_dir_id" );
            destination.setRequired( true );
            destination.setAllowEmptyValue(true);
            destination.setEmptyPickListMessage("none");
            
            destination.setValueFormatter(new FormItemValueFormatter() {
                public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {
                    System.out.println("This ComboBoxItem is never called"); //why was it not called ?
                    if (value != null) {
                        return value.toString();
                    }
                    return (String)value;
                }
            });
            
            destination.setEditorValueFormatter(new FormItemValueFormatter() {
                public String formatValue(Object value, Record record, DynamicForm form, FormItem item) {
                    if (record != null && value instanceof Number) {                    
                        final String id = record.getAttributeAsString("alert_policy_dispatch_dir_id");
                        final String displayName = record.getAttributeAsString("alert_policy_dispatch_name");
                        //there is no display value and the id is the same as the value
                        if ( id != null && displayName == null && id.equals( value.toString() )) {
                            return "Hidden value";
                        }
                    }
                    if (value != null) {
                        return value.toString();
                    }
                    return (String)value;
                }
            });
    In the code the setEditorValueFormatter was called.
    Now if I change the
    final ComboBoxItem destination = new ComboBoxItem
    to the
    ​final SelectItem destination = new SelectItem
    none of the Formatters called at all.
    It happens obviously only if the form which contains this item has separate dataSource.
    Last edited by aafros; 26 Nov 2015, 11:04.

    Comment


      #3
      The EditorValueFormatter is only invoked on an editable text input, so your results with ComboBoxItem are expected, and likewise you should not expected the EditorValueFormatter to be invoked for SelectItem.

      We'll double-check on the result you're getting where the ValueFormatter is not invoked for SelectItem.

      Comment


        #4
        We're not seeing an issue with ValueFormatter on SelectItem either. Here's example code for how to use a ValueFormatter with a SelectItem.

        Comment

        Working...
        X