I have a problem where I have a few checkboxes in a form which have textfields associated to them. I want the textfields to be enabled when the individual checkboxes are checked. I have done this with states and it works just fine. However, I wanted to add a checkall checkbox for the user to select if they wanted to select all the checkboxes at once. I have done that with states as well.
The problem is, when the checkall checkbox is there, the textfield's enable and disable functionality gets messed up. It basically becomes inconsistent. For instance, if I select the checkall box and then deselect it, then, when I individually select a checkbox, the textfield doesn't enable, you have to deselect the individual checkbox and then select it again, for it to work. I have tested this in a number of modules and it does the same thing everywhere.
So I have basically concluded that this is a bug in Drupal and was hoping that someone might have a workaround as I would like to keep my jquery simple with the states. My code looks like this:
function checkall_demo_form($form, &$form_state) {
$form['#tree'] = TRUE;
//Add a checkall checkbox to select/deselect the individual checkboxes
$form['checkall_demo']['checkall_checkbox'] = array(
'#type' => 'checkbox',
'#title' => t('Guarantee All Packages'),
);
//Add a fieldset
$form['checkall_demo']['checkall_fieldset'] = array(
'#type' => 'fieldset',
);
//Add the individual checkbox 1, clicking the above checkall box should check this checkbox as well
$form['checkall_demo']['checkall_fieldset']['checkbox_1'] = array(
'#type' => 'checkbox',
'#title' => t('Checkbox 1'),
'#states' => array(
'checked' => array(':input[name="checkall_demo[checkall_checkbox]"]' => array('checked' => TRUE)
),
),
);
//Add a textfield which will be enabled when the individual checkbox 1 is checked
$form['checkall_demo']['checkall_fieldset']['textfield_1'] = array(
'#type' => 'textfield',
'#title' => t('Textfield 1'),
'#default_value' => t('Hi'),
'#states' => array(
'enabled' => array(':input[name="checkall_demo[checkall_fieldset][checkbox_1]"]' => array('checked' => TRUE),
),
),
);
//Add the individual checkbox 2, clicking the above checkall box should check this checkbox as well
$form['checkall_demo']['checkall_fieldset']['checkbox_2'] = array(
'#type' => 'checkbox',
'#title' => t('Checkbox 2'),
'#states' => array(
'checked' => array(':input[name="checkall_demo[checkall_checkbox]"]' => array('checked' => TRUE)
),
),
);
//Add a textfield which will be enabled when the individual checkbox 2 is checked
$form['checkall_demo']['checkall_fieldset']['textfield_2'] = array(
'#type' => 'textfield',
'#title' => t('Textfield 2'),
'#default_value' => t('Hello'),
'#states' => array(
'enabled' => array(':input[name="checkall_demo[checkall_fieldset][checkbox_2]"]' => array('checked' => TRUE),
),
),
);
return $form;
}
Thanks.
Comments
I don't know if I would call
I don't know if I would call this a bug, so much as incompatible functionalities. These weren't built to work together, so if you want them to do so, you'll have to write a patch to core that handles it.
Contact me to contract me for D7 -> D10/11 migrations.