Announcement

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

    Bug with ListGrid startEditNew with defaultValue-LGF after validation error

    Hi Isomorphic,

    please see this builtInDS-based testcase using 5.1d SNAPSHOT_v10.1d_2015-10-30 (I have it in my application using 5.0p as well):
    BuiltInDS.java:
    Code:
    package com.smartgwt.sample.client;
    
    import com.google.gwt.core.client.EntryPoint;
    import com.smartgwt.client.core.KeyIdentifier;
    import com.smartgwt.client.data.AdvancedCriteria;
    import com.smartgwt.client.data.Criterion;
    import com.smartgwt.client.data.DataSource;
    import com.smartgwt.client.types.OperatorId;
    import com.smartgwt.client.util.Page;
    import com.smartgwt.client.util.PageKeyHandler;
    import com.smartgwt.client.util.SC;
    import com.smartgwt.client.widgets.IButton;
    import com.smartgwt.client.widgets.Label;
    import com.smartgwt.client.widgets.Window;
    import com.smartgwt.client.widgets.events.ClickEvent;
    import com.smartgwt.client.widgets.events.ClickHandler;
    import com.smartgwt.client.widgets.form.fields.SelectItem;
    import com.smartgwt.client.widgets.form.fields.SpinnerItem;
    import com.smartgwt.client.widgets.grid.ListGrid;
    import com.smartgwt.client.widgets.grid.ListGridField;
    import com.smartgwt.client.widgets.layout.VLayout;
    
    public class BuiltInDS implements EntryPoint {
        private VLayout mainLayout;
        private IButton recreateBtn;
    
        public void onModuleLoad() {
            KeyIdentifier debugKey = new KeyIdentifier();
            debugKey.setCtrlKey(true);
            debugKey.setKeyName("D");
    
            Page.registerKey(debugKey, new PageKeyHandler() {
                public void execute(String keyName) {
                    SC.showConsole();
                }
            });
    
            mainLayout = new VLayout(20);
            mainLayout.setWidth100();
            mainLayout.setHeight100();
    
            recreateBtn = new IButton("Recreate");
            recreateBtn.addClickHandler(new ClickHandler() {
                @Override
                public void onClick(ClickEvent event) {
                    recreate();
                }
            });
            mainLayout.addMember(recreateBtn);
            recreate();
            mainLayout.draw();
        }
    
        private void recreate() {
            Window w = new Window();
            w.setWidth("95%");
            w.setHeight("95%");
            w.setMembersMargin(0);
            w.setModalMaskOpacity(70);
            w.setTitle("Import data");
            w.setShowMinimizeButton(false);
            w.setIsModal(true);
            w.setShowModalMask(true);
            w.centerInPage();
    
            Label label = new Label("Try to create a new entry. Save will fail. See how the manager field switches to a integer value afterwards.");
            w.addItem(label);
            {
                final ListGrid testGrid = new ListGrid(DataSource.get("employees"));
                testGrid.setCanEdit(true);
                IButton startEditingNew = new IButton("Start editing new");
                startEditingNew.addClickHandler(new ClickHandler() {
                    @Override
                    public void onClick(ClickEvent event) {
                        testGrid.startEditingNew();
                    }
                });
                w.addItem(startEditingNew);
    
                ListGridField idLGF = new ListGridField("EmployeeId");
    
                ListGridField nameLGF = new ListGridField("Name");
    
                ListGridField managerLGF = new ListGridField("ReportsTo");
                SelectItem managerEditItem = new SelectItem("ReportsTo");
                managerEditItem.setOptionDataSource(DataSource.get("employees"));
                managerEditItem.setValueField("EmployeeId");
                managerEditItem.setDisplayField("Name");
                managerLGF.setEditorProperties(managerEditItem);
                // 192 is employee "Ralph Brogan"
                managerLGF.setDefaultValue(192);
                // Following row would not be correct. Value must be a whole number.
                // managerLGF.setDefaultValue("Ralph Brogan");
    
                ListGridField jobLGF = new ListGridField("Job");
    
                ListGridField genderLGF = new ListGridField("Gender");
    
                ListGridField salaryLGF = new ListGridField("Salary");
                salaryLGF.setEditorProperties(new SpinnerItem());
    
                testGrid.setFields(idLGF, managerLGF, nameLGF, genderLGF, jobLGF, salaryLGF);
                AdvancedCriteria ac = new AdvancedCriteria(new Criterion("Name", OperatorId.STARTS_WITH, "A"));
                testGrid.fetchData(ac);
                w.addItem(testGrid);
            }
            w.show();
        }
    }
    employees.ds.xml:
    Code:
    <DataSource ID="employees" serverType="sql" tableName="employeeTable" recordName="employee" testFileName="/examples/shared/ds/test_data/employees.data.xml"
        titleField="Name">
        <fields>
            <field name="userOrder" title="userOrder" type="integer" canEdit="false" hidden="true" />
            <field name="Name" title="Name" type="text" length="128">
                <validators>
                    <validator type="serverCustom" errorMessage="$errorMessage">
                        <serverObject lookupStyle="new" className="com.smartgwt.sample.server.listener.FalseValidator" />
                    </validator>
                </validators>
            </field>
            <field name="EmployeeId" title="Employee ID" type="integer" primaryKey="true" required="true" />
            <field name="ReportsTo" displayField="ReportsToName" title="Manager" type="integer" required="true" foreignKey="employees.EmployeeId" />
            <field name="ReportsToName" includeFrom="employees.Name" includeVia="ReportsTo" />
    
            <field name="Job" title="Title" type="text" length="128" />
            <field name="Email" title="Email" type="text" length="128" />
            <field name="EmployeeType" title="Employee Type" type="text" length="40" />
            <field name="EmployeeStatus" title="Status" type="text" length="40" />
            <field name="Salary" title="Salary" type="float" />
            <field name="OrgUnit" title="Org Unit" type="text" length="128" />
            <field name="Gender" title="Gender" type="text" length="7">
                <valueMap>
                    <value>male</value>
                    <value>female</value>
                </valueMap>
            </field>
            <field name="MaritalStatus" title="Marital Status" type="text" length="10">
                <valueMap>
                    <value>married</value>
                    <value>single</value>
                </valueMap>
            </field>
        </fields>
    </DataSource>
    FalseValidator.java:
    Code:
    package com.smartgwt.sample.server.listener;
    
    import java.util.Map;
    
    import com.isomorphic.datasource.DataSource;
    import com.isomorphic.datasource.Validator;
    import com.isomorphic.log.Logger;
    
    public class FalseValidator {
    
        public boolean condition(Object value, Validator validator, String fieldName, @SuppressWarnings("rawtypes") Map record, DataSource ds)
                throws Exception {
            Logger log = new Logger("com.isomorphic." + FalseValidator.class.getName());
            log.info("Called for field \"" + fieldName + "\", value \"" + (value == null ? "NULL" : value.toString()) + "\"");
            validator.addErrorMessageVariable("errorMessage", "FalseValidator");
            return false;
        }
    }
    When you start a new row, it will always have errors (see FalseValidator). The displayed value for the unchanged default mentor of "Ralph Brogan"(=192) will change to "192". This does not happen if I select an entry from the list. The selected name then stays also when the validation error is displayed. Also when I change away from "Ralph Brogan" and then back to "Ralph Brogan", it works. Only an unchanged default value triggers the bug.

    Best regards
    Blama

    #2
    We see the issue you describe. It is a result of the way form item values are populated when a user selects a record. Basically - when a user chooses a value and there's a displayField in a configuration like this, there is built in logic to store out the display field as well as the value field item on the form.
    This is failing to fire in this case because the value was supplied programmatically. We'll take a look at whether we can modify the framework to handle this case better, but this may not be a trivial change.

    In the meantime you could get the same behavior by taking your new field value, explicitly querying the option DataSource with it to also pick up the display field value and passing both to 'startEditingNew' -- essentially pre-populate both fields explicitly.

    Regards
    Isomorphic Software

    Comment

    Working...
    X