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:
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
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>
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
Comment