2

I have a simple ExtJS 4 (xtype form) inside a window. It has two fields and a save button. One of the fields is a string. That works fine. If it's blank, getValues() gives me the field blank. If it's set, I get the value. However, the check box is different. If it's checked, I get the value of '1'. HOWEVER, if I un-check the box (or do not check it), I get no value - the field is not listed in the result of getValues()! WHY??

items: [
                    {
                        xtype: 'checkbox',
                        name : 'insuranceVerified',
                        fieldLabel: 'insuranceVerified',
                        inputValue:'1',
                        value:'1'  //does not make a difference
                    },
                    {
                        xtype: 'textfield',
                        name : 'ru',
                        fieldLabel: 'ru'
                    }
]

I am catching the save button click event and calling this function in my controller:

updateEncounter: function(button) {
        console.log('clicked the Save button');
        var win    = button.up('window'),
            form   = win.down('form'),
            record = form.getRecord(),
            values = form.getValues();

// values properly shows the ru field on the form, but only shows insuranceVerified if it's checked - is not in values if it's unchecked... ??!

            debugger;
        record.set(values);
        this.getEncountersStore().sync();
        win.close();
    }

I just figured out something else, though it's not a complete answer for me: If I change the code above so it get's the form (I assume from the form panel):

 form   = win.down('form').getForm(),   // instead of win.down('form')

Then, use getFieldValues() on that new object instead of the getValues(), I now have the check box value even if it's un-checked.

values = form.getFieldValues();  // instead of getValues()

HOWEVER, the value I get back is 'false' (or true), not '0' or '1' like I specified with inputValue:'1'.

Also, I even tried setting a 'value' for the field '1', no difference whether I do that or not for getValues or getFieldValues().

Scott Szretter
  • 3,938
  • 11
  • 57
  • 76

3 Answers3

9

This works for me. Hope to help with

{
   xtype: 'checkbox',
   boxLabel: modelFields[i].label,
   name: modelFields[i].name,
   //magic!
   inputValue: 1,
   uncheckedValue: 0
}
Alex Dzeiko
  • 866
  • 11
  • 12
0

Submit an HTML form with empty checkboxes

What do you expect to receive if the checkbox isn't selected? You specified the value being sent if it is checked..

Here another solution: How come checkbox state is not always passed along to PHP script?

Community
  • 1
  • 1
duedl0r
  • 9,289
  • 3
  • 30
  • 45
  • Well, in the case of getFieldValues() I receive true if it's checked, false if it's not checked. What I really want is '1' if it's checked and 0 if it's not checked. So according to the links you provided, on my PHP back end, i would have to assume the field is '0' unless it is sent? I understand the HTML/PHP issues in those links, but it seems like ExtJS should be marking the field dirty and shouldn't unchecked be a value in ExtJS, a value of false, 0, NO, etc. that I can then send to the server to update the DB? – Scott Szretter Nov 28 '11 at 15:16
  • 2
    OK, after posting my last comment, I found the answer, simply add a property of: uncheckedValue:'0' to the check box definition! – Scott Szretter Nov 28 '11 at 15:27
-1

you can use

var answer1IsCorrect = Ext.getCmp('Answer1RadioButton').checked ? 1 : 0;

insted of true or false

akjoshi
  • 15,374
  • 13
  • 103
  • 121