Skip to main content
Code indenting, removed "Thanks" and some "grammar police" stuff
Source Link

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; }

Why does changes not get saved in $form, or what is overriding it?

i would like to set custom class to multiple fieldsets in 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; }

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:

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.

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;
}
added 13 characters in body
Source Link
gregab
  • 507
  • 2
  • 6
  • 20
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; }

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;
}
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; }

added 2068 characters in body
Source Link
gregab
  • 507
  • 2
  • 6
  • 20

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;
}

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;
}
improved grammar and spelling
Source Link
avpaderno
  • 97.9k
  • 15
  • 164
  • 284
Loading
Source Link
gregab
  • 507
  • 2
  • 6
  • 20
Loading