-2

I write a form like ajax_example way to bulit a dependent dropdown select.

when select one and called the ajax_call_back function ,the values I get in $form_state['values'] (use dpm($form_state))has no element ,but has $form_state['input'] contains what I want... and even I commit the form,the $form_state['values'] still wrong outputs. here is picture. enter image description here how make the $form_state['values'] works???

when I add another form blow the form,the ajax didn't work at all,even if add the same as itself... and the error message is :

The form has become outdated. Copy any unsaved work in the form below and then reload this page.

the call back of form like this

>  $build['og_user_filter_form'] =drupal_get_form('og_user_filter_form'); 
>  
> $build['user_admin_account']= drupal_get_form('og_user_admin_account');
>         
>return $build['og_user_filter_form'];

if I return $build['og_user_filter_form'],it works,but even though i use blew code,it doesn't work ,either.

>  $build['og_user_filter_form'] = drupal_get_form('og_user_filter_form'); 
>  
> //$build['user_admin_account']= drupal_get_form('og_user_admin_account');
>         
>return $build;

here is my code:

/**
 * Form builder; Return form for user administration filters.
 *
 * @ingroup forms
 * @see user_filter_form_submit()
 */
function og_user_filter_form($form, &$form_state) {
  $form['group_type'] = array(
    '#type' => 'radios',
    '#options' => array(
      'school_group'   => t('School group management'),
      'public_group' => t('Public group management'),
    ),
  );
  $form['school_group'] = array(
    '#type' => 'fieldset',
    '#title' => t('School group'),
    // This #states rule says that the "high school" fieldset should only
    // be shown if the "group_type" form element is set to "High School".
    '#states' => array(
      'visible' => array(
        ':input[name="group_type"]' => array('value' => 'school_group'),
      ),
    ),
  );

  //XXX choose grade.
  //get all grade nodes(content type == 'grade').
  $group_grade_nodes = druedu_og_get_nodes_by_group('grade');

  if(empty($group_grade_nodes)){
    $grade_groups[] = '-No Result-';
  }else{
    $grade_groups[] = '-Select-'; //default value
    foreach ($group_grade_nodes as $grade_nodes) {
       $grade_groups[$grade_nodes->nid] = $grade_nodes->title;
    }
  }
  // School group information.
  $form['school_group']['grade_group'] = array(
    '#type' => 'select',
    '#options' => $grade_groups,
    '#title' => t('Which grade?'),
    '#default_value' => '-Select-',
    '#states' => array(
      'visible' => array(   // action to take.
        ':input[name="group_type"]' => array('value' => 'school_group'),
      ),
    ),
    '#ajax' => array(
      'callback' => '_ajax_grade_group_callback',
      'wrapper' => 'subject-container',
      'effect' => 'slide',
      //'progress' => array('type' => 'none'),
    ),
  );
  //XXX subject container init.
   $form['school_group']['container']['subject_group']= array(
    '#type' => 'fieldset',
     '#title' => t('Subjects'),
    '#prefix' => '<div id="subject-container">',
    '#suffix' => '</div>',
    '#type' => 'fieldset',
    '#attributes' => array('class' => array('hidden')),
    '#states' => array(
      'visible' => array(   // action to take.
        ':input[name="grade_group"]' => array('!value' => '0'),
      ),
    ),
    //TODO in css add .hidden {display:none;}
  );



  dpm($form_state);
  if (!empty($form_state['input']['grade_group']) && ($grade = $form_state['input']['grade_group'])) {
    $subject_groups_nodes = druedu_og_get_nodes_by_group($grade);
    if(empty($subject_groups_nodes)){
      $subject_groups[] = '-No Result-';
    }else{
      $subject_groups[] = '-Select-'; //default value
      foreach ($subject_groups_nodes as $subject_nodes) {
        $subject_groups[$subject_nodes->nid] = $subject_nodes->title;
      }
    }
    $form['school_group']['container']['subject_group']['subject_group'] = array(
      '#type' => 'select',
      '#options' =>  $subject_groups,
      '#title' => t('which subject?'),
      '#default_value' => '-Select-',
      '#prefix' => '<div id="subject-container">',
      '#suffix' => '</div>',
      '#ajax' => array(
        'callback' => '_ajax_subject_group_callback',
        'wrapper' => 'class-container',
        'effect' => 'slide',
        //'progress' => array('type' => 'none'),
      ),
      '#states' => array(
        'visible' => array(
          ':input[name="grade_group"]' => array('!value' => '0'),
        ),
      ),
    );


    // class container init.
    $form['school_group']['container']['class_group'] = array(
      '#type' => 'fieldset',
      '#title' => t('CLass'),
      '#prefix' => '<div id="class-container">',
      '#suffix' => '</div>',
      '#attributes' => array('class' => array('hidden')),
      '#states' => array(
        'visible' => array(
          ':input[name="subject_group"]' => array('!value' => '0'),
        ),
      ),
    );

  }

  //XXX choose class

  if (!empty($form_state['input']['subject_group']) && ($subject = $form_state['input']['subject_group'])) {

    $group_class_nodes = druedu_og_get_nodes_by_group($grade,$subject);
    if(empty($group_class_nodes)){
      $class_groups[] = '-No Result-';
    }else{
      $class_groups[] = '-Select-';
      foreach ($group_class_nodes as $class_node) {
        $class_groups[$class_node->nid] = $class_node->title;
      }
    }
    $form['school_group']['container']['class_group']['class_group']  = array(
      '#type' => 'select',
      '#options' => $class_groups,
      '#title' => t('which class?'),
      '#default_value' => '-Select-',
      '#prefix' => '<div id="class-container">',
      '#suffix' => '</div>',
      '#states' => array(
        'visible' => array(
          ':input[name="subject_group"]' => array('!value' => '0'),
        ),
      ),
    );
  }
  return $form;
}

/**
 * Callback element needs only select the portion of the form to be updated.
 * Since #ajax['callback'] return can be HTML or a renderable array (or an
 * array of commands), we can just return a piece of the form.
 * See @link ajax_example_advanced.inc AJAX Advanced Commands for more details
 * on AJAX framework commands.
 *
 * @return renderable array (the checkboxes fieldset)
 */
function _ajax_grade_group_callback(&$form, &$form_state) {
  //$form_state['rebuild'] = TRUE;
  //$form_state['input']['subject_group']=0;
  //$form_state['input']['class_group']= '0';
  return $form['school_group']['container'];
}
function _ajax_subject_group_callback($form, &$form_state) {

  return $form['school_group']['container']['class_group'];
}
0

Resloved, can't use this hook_menu()

$items['admin/og'] = array(
    'title' => 'Group adminstrator',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('og_admin'),

and

 og_admin(){
  $build['og_user_filter_form'] =drupal_get_form('og_user_filter_form'); 

  $build['user_admin_account']= drupal_get_form('og_user_admin_account');

  return $build['og_user_filter_form'];
}

should user directly user hook_menu()

 'page callback' => 'drupal_get_form',
 'page arguments' => array('og_user_filter_form'),

and the og_user_filter_form is the dependent dropdown form.

  • The form has become outdated. Copy any unsaved work in the form below and then reload this page. also this error when use form ajax and not called directly form hook_menu. – bluesky_still Nov 8 '12 at 10:21

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.