3

Im struggling with my form since yesterday ... I can't find a way to retrieve values from form_state in my form function.

heres my different pieces of code :

    function mymodule_menu() {
  $items['mypath'] = array(
      'title' => 'Bienvenue',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('mymodule_form'),
      'access callback' => 'mymodule_checkID',
      'type' => MENU_CALLBACK,
  );
  return $items;
}


function mymodule_form($form, &$form_state) {

        if(!empty($form_state['values']['rows'])){
            $cmdeData=$form_state['values']['rows'];
            watchdog("TEST","TEST-OUI",array(),WATCHDOG_ALERT);
        }else{
        watchdog("TEST","TEST-NON",array(),WATCHDOG_ALERT);         $cmdeExiste=checkCmde($cmdeID);
            if($cmdeExiste){
                //On récupère les données de la commande via SQL et on les stocke dans un tableau
                $cmdeData=recupCmde($cmdeID);   
            }else{
                if(isset($_GET['D1'])){
                    $cmdeData=recupIntentionCmde(explode('|', $_GET['D1']));
                }
            }
        }


      if (empty($form_state['num_lignes'])){
        if(!$cmdeExiste && (!isset($cmdeData) || empty($cmdeData[0]))) {
            $form_state['num_lignes'] = 1;
        }else{
            $form_state['num_lignes'] = count($cmdeData);
        }
      } 

      $lignes=array();
      $lignes['#tree']=TRUE;
      for ($i = 0; $i <= $form_state['num_lignes']; $i++) {

        $lignes['r'.($i+1)]=array();

        $lignes['r'.($i+1)]['ref'] = array(
          '#type' => 'textfield',
          '#size' => 7,
          '#maxlength' => 7,
          '#required' => TRUE,
          '#default_value' => ( $i<count($cmdeData) ? $cmdeData[$i]['ref'] : '' ),
          '#disabled' => ( $i<count($cmdeData) ? TRUE : FALSE ),
        );
        $lignes['r'.($i+1)]['qte'] = array(
          '#type' => 'textfield',
          '#size' => 3,
          '#maxlength' => 3,
          '#required' => TRUE,
          '#default_value' => ( $i<count($cmdeData) ? $cmdeData[$i]['ref'] : '' ),
          '#disabled' => ( $i<count($cmdeData) ? TRUE : FALSE ),
        );
        $lignes['r'.($i+1)]['des'] = array(
          '#type' => 'item',
          '#size' => 60,
          '#default_value' => ( $i<count($cmdeData) ? $cmdeData[$i]['ref'] : '' ),
        );
        $lignes['r'.($i+1)]['pge'] = array(
          '#type' => 'item',
          '#size' => 3,
          '#default_value' => ( $i<count($cmdeData) ? $cmdeData[$i]['ref'] : '' ),
        );
        $lignes['r'.($i+1)]['tot'] = array(
          '#type' => 'item',
          '#size' => 7,
          '#input' => FALSE,
          '#default_value' => ( $i<count($cmdeData) ? $cmdeData[$i]['ref'] : '' ),
        );

        if($i < count($cmdeData)){
            $lignes['r'.($i+1)]['btn_supp'] = array(
            '#type' => 'button',
            '#value' => t('Supprimer'),
            '#name' => t('Supprimer_'.($i+1)),
            '#ajax' => array(
                'callback' => 'cmde_tlv_deleteRow_ajax_callback',
                'wrapper' => 'lignes_articles',
            ),
            );
        }else{
            $lignes['r'.($i+1)]['btn_supp'] = array(
                '#type' => 'item',
            );
        }
      }

    $form['table'] = array(
    '#theme' => 'cmde_tlv_form_table',
    '#header' => array(t('Référence article'), t('Quantité'), t('Désignation'), t('Page'), t('Total')),
    'rows' => $lignes,
    '#prefix' => "<div id='lignes_articles'>",
    '#suffix' => "</div>",
  );


  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Valider la saisie',
    // '#submit' => array('cmde_tlv_submit2'),

  );


 return $form;
}


function cmde_tlv_deleteRow_ajax_callback($form, &$form_state) {

unset($form['table']['rows'][$form_state['triggering_element']['#parents'][1]]);

$form_state['num_lignes']--;
//$form_state['rebuild'] = TRUE;

return $form['table'];
}

From the drupal API I've read that the 'drupal_get_form' function was reseting the form_state array.

Ive tried to add a HOOK_form_alter function but ive got the same problem. heres the code :

function mymodule_form_alter(&$form, &$form_state, $form_id){

    if($form_id=="mymodule_form"){


        watchdog("TEST",print_r($form_state, TRUE),array(),WATCHDOG_ALERT);
    if(!empty($form_state['values'])) watchdog("TEST","TEST2",array(),WATCHDOG_ALERT);
    }


}

Here what' inside the form_state array :

Array ( [build_info] => Array ( [args] => Array ( ) [form_id] => cmde_tlv_form ) [rebuild] => [rebuild_info] => Array ( ) [redirect] => [temporary] => Array ( ) [submitted] => [executed] => [programmed] => [programmed_bypass_access_check] => 1 [cache] => [method] => post [groups] => Array ( ) [buttons] => Array ( ) [input] => Array ( ) [num_lignes] => 2 )

Note 1 : I can't post the content of 'form_state' from the callback function, but it has 'values' and 'triggering_element' and all normal stuff

Note 2 : I did try to uncomment $form_state['rebuild'] = TRUE; in the callback function and did a lot of test to see if form_state was still empty (default) ... and it still was ...

3
  • Anybody maybe ? I did search through drupal answers or other place but didnt find anything that really helped me ...
    – Math666
    Commented Dec 31, 2014 at 8:38
  • It's not clear what problem are you experiencing exactly. BTW I see several things that seems wrong. 1/ $cmdeID doesn't seem to be declared. 2/ you can't modify a $form_state (neither $form) passed by reference in the ajax callback. The callback is intended to just return a form element, or an array of ajax commands.
    – jonhattan
    Commented Dec 31, 2014 at 8:50
  • Thanks for you reply... $cmdeID is declared but not display here. I could alter the form and delete my row when rebuilding the form but I can't have access to form_state ... What I can't figure out is that in the example module fro dripal developers, I can see a lot of examples of form build like mine (using 'drupal_get_form' and testing for presence of someting specific in $form_state to rebuild differently when after ajax callback) ... Its really bothering ...
    – Math666
    Commented Dec 31, 2014 at 9:11

1 Answer 1

1

I did find why my $form_state array was reset: The required attribute was the problem. Adding '#limit_validation_errors' => array() to my delete button did the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.