0
votes

I have a search form (ExtJS 4.2) with a few fields:

xtype: 'fieldcontainer',
layout: 'hbox',

    items: [{

         xtype: 'combobox',
         itemId: 'cboEmployee',
         width: 400,
         store: cboEmployeeStore,
         cls: 'arigth',
         displayField: 'FullName',
         valueField: 'EmployeeId',
         queryMode: 'remote',
         fieldLabel: 'Employee',
         editable: true,
         hideTrigger: true,
         queryParam: 'searchStr',
         name: 'EmployeeId',
         minChars: 3,
         listConfig: {
             loadingText: 'Searching...',
             // Custom rendering template for each item
             getInnerTpl: function() {
                 return '<b>{EmployeeNumber}</b> / {FullName}';
             }
         }


     }, {
         xtype: 'datefield',
         fieldLabel: 'From',
         name: 'FromDate',
         format: 'd/m/Y',
         submitFormat: 'Y-m-d H:i:s',
         width: 180,
         labelWidth: 50,
         cls: 'arigth',
         value: new Date()
     }, {
         xtype: 'datefield',
         fieldLabel: 'To',
         name: 'ToDate',
         format: 'd/m/Y',
         submitFormat: 'Y-m-d H:i:s',
         width: 165,
         labelWidth: 25,
         cls: 'arigth',
         value: new Date()

     }, {
         xtype: 'button',
         action: 'search',
         text: 'Search',
         margin: '0 0 0 20'
     }]

Now in my controller when my button "search" is clicked I have the following code:

var values = {};
        form.items.each(function(f) {
            var field = f.getName();
            var value = f.getValue();

            if (field == "EmployeeId" && value == "") value = 0;
            values[field] = value;
        });

I can't do: form.getValues() because I will EmployeeId = "" and EmployeeId must be integer.

That's why I implement the each statement but...

I get an error when the item is fieldset type because f.getName() is undefined so I don't know if this is the best way to do this in order to set EmployeeId = 0 when EmployeeId field value is "" (empty string).

1

1 Answers

1
votes

If you need EmployeeId to be 0 if it is an empty string, then just do that check after you get all the form values. Also, you can get the field values as their actual types, instead of all as strings, by using the getFieldValues function of the form.

// this assumes form is the FormPanel
var formValues = form.getForm().getFieldValues();
// check if the EmployeeId value is null/undefined/""
if(!formValues['EmployeeId']) {
    formValues['EmployeeId'] = 0;
}