0

I have written a single form builder and a form submit handler corresponding to that form.

What I have done is that I have rendered the form multiple times using for loop and then concatenated them into a single variable and then displayed it on a single page.

Each form has separate submit buttons.

But the problem is that whenever I press the submit button of any of the forms I am only getting the form that was built first in the form_state['values'] in the submit handler.

For now, I have made it work by taking the form_state['input'] which seem to have the correct set of values based on the form submit clicked.

I need the corresponding form values in the submit handler. How can I achieve this?

Is this some kind of bug or am I doing it wrong?

$form_markup = '';
  foreach ($ids as $key => $value) {
    $req_form = drupal_get_form('_request_form', $value, $key, $node);
    $form_markup .= render($req_form);
  }

My form and form_submit

function _request_form($form, &$form_state, $value, $key, $node) {
    $form['name'] = array(
      '#type' => 'item',
      '
    );
    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => 'Ask',
    );
    return $form;
  }
}

    function _request_form_submit($form, &$form_state) {
      dpm(form_state['values']);
      dpm(form_state['input']);
    }

Any help is much appreciated. thanks.

2

You'll need to implement hook_forms(), so that you can create 'aliases' of your forms.

For example, let's imagine a form named 'message_form', that you want to include multiple times on the page. To do this, you append a unique number to the form name each time when calling the form:

$page = [
  'form_1' => drupal_get_form('message_form-1'),
  'form_2' => drupal_get_form('message_form-2'),
];

return $page;

Next, you implement hook_forms() to watch for form names in the format above:

function hook_forms($form_id, $args) {
  // Act on form IDs that match the pattern message_form-[NUMBER].
  if (preg_match('/^message_form-\d+$/', $form_id)) {
    // The key is the form ID that has been passed.
    $forms[$form_id] = [
      // The callback is the actual form definition to be called.
      'callback' => 'message_form',
    ];
  }
}

Now each form being put on the page will have a custom form ID and therefore the system will know which form the submitted data refers to.

  • I tried something similar to this. Whenever the form hook was called I had assigned a unique value to the form_id but still it didnt work. – Bej Apr 24 '18 at 11:02
  • This is how you do it. If it didn't work, you did something wrong. Did you implement hook_forms()? Update the original post with your code that is not working. – Jaypan Apr 24 '18 at 11:33
  • I will update it when i try the method that you suggested. thanks – Bej Apr 25 '18 at 4:20

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.