I just figured out that the form fields in Magento, while using checkbox, it sends on as its value when it is checked and nothing when unchecked. Now if it is on once it is never set as off in Db core_config_data table. Why has Magento made design like that in its JS?

  • This is actually standard HTML behaviour. Try it yourself with some test script. The bad design in Magento is when nothing is sent then Magento should assume it was unchecked and store this in the DB. – 7ochem May 10 '15 at 6:13
  • @7ochem- So you are saying is that when a person uncheck a checked checkbox and save it,nothing happens and still its value in core_config_data would be on,right?So how you will detect now if the checkbox is unchecked or not from code? Please explain? – Rahul Kathuria May 10 '15 at 8:04
  • You need to know what values could be posted. If you have textarea A, input field B and multiple checkboxes C, but you only receive POST vars for A and B, then you automatically know that none of the checkboxes C were checked... – 7ochem May 11 '15 at 6:57
  • Ofcourse that is when you are trying to reload the page on which checkbox are placed.But I need to check that when I am in another module,then Mage::getStoreConfig() picks from table core_Config_data which will never be switched off,so unable to detect – Rahul Kathuria May 11 '15 at 7:45

For system configuration fields, use dropdowns instead of checkboxes. For example, if you have a field called Enabled, use a dropdown with the values Yes (1) and No (0). Don't use a checkbox because the off value is never sent (in fact, an off value doesn't exist - there is only an on value).

| improve this answer | |
  • then what is the use of checkbox,if not for a binary operation? – Rahul Kathuria May 10 '15 at 8:08
  • Checkboxes aren't really for binary operations. They're for optional choices - the type of stuff that can be added to a request, but doesn't have to be there. For example, "Add cheese to my pizza (+$2.00)" would be a checkbox. Unfortunately, a lot of systems do use checkboxes as on/off switches, but they have to explicitly account for the off state because checkboxes don't have off values, they only have on values (i.e. the value attribute). – Agop May 10 '15 at 14:01
  • So basically you can never unset the checkbox once it is checked? – Rahul Kathuria May 10 '15 at 18:35
  • Yup. Unless I'm mistaken, Magento only saves what it receives. I don't remember Magento having any special logic to check for unchecked checkboxes. – Agop May 11 '15 at 14:25
  • So this is wrong. – Rahul Kathuria May 12 '15 at 15:02

Why has Magento made design like that in its JS?

I think because you did it that way in the past. There was no need to add a hidden field right before to make sure the value is either 0 or 1 (I guess you mean this?).

If you are checking a boolean magento value, you want to use \Mage::getStoreConfigFlag, so you get either true or false. It doesn't matter if the value is set.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.