1

I'm using an AJAX form to match multiple values of a field to an other entity, to do so i've implemented the example we have in the ajax_example ( https://api.drupal.org/api/examples/ajax_example!ajax_example_graceful_degradation.inc/group/ajax_degradation_example/7 ), see ajax_example_add_more*

It is working great (except i would prefer being able to remove the field i want and not always the last one but we can't have everything).

When i submit the form, my chosen values are saved in database and the page is refreshed. The problem is that the form became empty, i might have multiple names for one fieldset and now i only have one empty textfield.

If we continue with the ajax_example, what i can do is this :

$myValues = variable_get('my_var', '');

And on this part add a default_value (let's assume that in my $myValues['name'] there is the list of the names)

$form['names_fieldset']['name'][$i] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
      '#default_value' => $myValues['name'],
);

In the case where i add only one name, it will work. But if i have multiple name, it will only display the last one.

Do you know how can i do to recreate all the form as it is store in the db ?

Thanks by advance

[EDIT]

Thanks to sareed, i found the way to do so. I'll show the modification to ajax_example_add_more. In the example there is only one fieldset, in my work there is x fieldset so i will add $fieldsetName to know which one :

Change :

 if (empty($form_state['num_names'])) {
    $form_state['num_names'] = 1;
  }

By

     $myValues = variable_get('my_var', '');
     if (empty($form_state[$fieldsetName]['num_names'])) {

          if(isset($myValues[$fieldsetName])) {
            $form_state[$fieldsetName]['num_names'] = 
              count($myValues[$fieldsetName]) > 0 ? count($myValues[$fieldsetName]) : 1;
          }
          else {
            $form_state[$fieldsetName]['num_names'] = 1;
          }
      }

AND

  for ($i = 0; $i < $form_state['num_names']; $i++) {
    $form['names_fieldset']['name'][$i] = array(
      '#type' => 'textfield',
      '#title' => t('Name'),
    );
  }

TO

  $form['names_fieldset']['name'][$i] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => isset($myValues[$fieldsetName][$i]) ? $myValues[$fieldsetName][$i] : '',
  );
0

Loop through the myValues (only use the foreach if you are not already in a for loop) and use the appropriate array element:

foreach($myValues as $index => $name) {
  $form['names_fieldset']['name'][$index] = array( // Already using $i here. Are you in a for loop?
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#default_value' => $name, // Or $myValues[$i]['name'] if you are already in a for loop (it looks like you might be).
  );
}

Depending on the structure of the array you may have to change this a little. If these don't work and you can't figure it out, add a print out of one of your smaller arrays using print_r to your question and we can help.

  • Thanks for your answer. However, if myValues is empty i won't even have a textfield shown. One answer would be to do as you say if myValues isn't empty elsedo the 'classic' way. but it would mean create twice the same form with just a little difference – azertyuiop Dec 9 '15 at 14:26
  • Actually you can still do a loop. A do-while loop would work well. Or you could still use a for just set your stop var to something like this $count = $myValues ? count($myValues) : 1; then your for loop for ($i = 0; $i < $count; $i++). – sareed Dec 9 '15 at 14:35

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.