Why doesdo changes notdon't get saved in $form$form
, or what is it that's overriding it?
iI would like to set custom class to multiple fieldsets in an ajax callback function:
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.
Thanks!
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;
}
}
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;
}