Why do changes don't get saved in $form
, or what is it that's overriding it?
I would like to set custom class to multiple fieldsets in an ajax callback function:
if (something) {
$form['fieldset_01']['#attributes'] = array('class' => array('custom_class'));
$form['fieldset_02']['#attributes'] = array('class' => array('custom_class'));
}
elseif (somethingelse) {
$form['fieldset_11']['#attributes'] = array('class' => array('custom_class'));
$form['fieldset_12']['#attributes'] = array('class' => array('custom_class'));
}
return $form;
When $form
is returned, the element that I have set in this call is rendered fine, with a new CSS class but the elements I have rendered in previous calls have lost this new class, as if they were rendered with default values.
EDIT: my form creation function
function mynode_form($form, &$form_state) {
$nid = $form_state['values']['nid'];
$ajax = array(
'callback' => 'mynode_ajax',
'wrapper' => 'mynode_form',
'event' => 'change',
'method' => 'replace',
);
$node = node_load($nid);
$title = $node->field_title['und'][0]['value'];
$form = array();
$form['#prefix'] = '<div id="mynode_form">';
$form['#suffix'] = '</div>';
$form['nid'] = array(
'#value' => $nid,
'#type' => 'hidden',
);
$form['fieldset_0'] = array(
'#type' => 'fieldset',
'#collapsible' => true,
'#title' => 'Fieldset title 0',
//'#attributes' => array('class' => array('a', 'b')),
'#weight' => 1,
);
$form['fieldset_0']['textfield_0'] = array(
'#type' => 'textfield',
'#title' => t('Title 0?'),
'#ajax' => $ajax,
);
$form['fieldset_1'] = array(
'#type' => 'fieldset',
'#collapsible' => true,
'#title' => 'Fieldset title 1',
//'#attributes' => array('class' => array('a', 'b')),
'#weight' => 1,
);
$form['fieldset_1']['textfield_1'] = array(
'#type' => 'textfield',
'#title' => t('Title 1?'),
'#ajax' => $ajax,
);
if (isset($form_state['triggering_element']['#name'])){
$key = $form_state['triggering_element']['#name'];
switch($key){
case 'textfield_0':
$form['fieldset_0']['#attributes'] = array('class' => array('a', 'b', 'c'));
$form['fieldset_1']['#attributes'] = array('class' => array('a', 'b'));
break;
}
}
else{
//only the first time
$form['fieldset_0']['#attributes'] = array('class' => array('a', 'b'));
$form['fieldset_1']['#attributes'] = array('class' => array('a', 'b'));
}
return $form;
}
$form_state
inside anisset()
check to avoid warnings and notices.