Skip to main content
Removed Drupal and version tag from title
Link
leymannx
  • 18.8k
  • 6
  • 72
  • 126

How tocan I create a GET form with the Drupal 8 Form API?

Remove (partly incorrect) answer
Source Link
vrijdenker
  • 234
  • 3
  • 10

A fix

I just retried by described attempt to call buildForm instead of getForm. Again I got into the redirect loop. But aperently that happens because I gave my submit button type submit instead of button.

So when I change my submit button to:

$form['actions']['submit'] = [
  '#name' => 'test-submit',
  '#id' => Html::getUniqueId('edit-submit-test-form'),
  '#type' => 'button',
  '#value' => 'Submit it',
];

And I call:

$form = new TestForm();
$form_state = new \Drupal\Core\Form\FormState();
$form_state->setMethod('GET');
$variables['test_form'] = \Drupal::formBuilder()->buildForm($form, $form_state);

It now all works (Drupal picks up the GET request and processes the form). It's still not what I'm expecting of it, but that's a whole other question, so I'll open a new question instead.


A fix

I just retried by described attempt to call buildForm instead of getForm. Again I got into the redirect loop. But aperently that happens because I gave my submit button type submit instead of button.

So when I change my submit button to:

$form['actions']['submit'] = [
  '#name' => 'test-submit',
  '#id' => Html::getUniqueId('edit-submit-test-form'),
  '#type' => 'button',
  '#value' => 'Submit it',
];

And I call:

$form = new TestForm();
$form_state = new \Drupal\Core\Form\FormState();
$form_state->setMethod('GET');
$variables['test_form'] = \Drupal::formBuilder()->buildForm($form, $form_state);

It now all works (Drupal picks up the GET request and processes the form). It's still not what I'm expecting of it, but that's a whole other question, so I'll open a new question instead.

Added a fix to my problem
Source Link
vrijdenker
  • 234
  • 3
  • 10

A fix

I just retried by described attempt to call buildForm instead of getForm. Again I got into the redirect loop. But aperently that happens because I gave my submit button type submit instead of button.

So when I change my submit button to:

$form['actions']['submit'] = [
  '#name' => 'test-submit',
  '#id' => Html::getUniqueId('edit-submit-test-form'),
  '#type' => 'button',
  '#value' => 'Submit it',
];

And I call:

$form = new TestForm();
$form_state = new \Drupal\Core\Form\FormState();
$form_state->setMethod('GET');
$variables['test_form'] = \Drupal::formBuilder()->buildForm($form, $form_state);

It now all works (Drupal picks up the GET request and processes the form). It's still not what I'm expecting of it, but that's a whole other question, so I'll open a new question instead.


A fix

I just retried by described attempt to call buildForm instead of getForm. Again I got into the redirect loop. But aperently that happens because I gave my submit button type submit instead of button.

So when I change my submit button to:

$form['actions']['submit'] = [
  '#name' => 'test-submit',
  '#id' => Html::getUniqueId('edit-submit-test-form'),
  '#type' => 'button',
  '#value' => 'Submit it',
];

And I call:

$form = new TestForm();
$form_state = new \Drupal\Core\Form\FormState();
$form_state->setMethod('GET');
$variables['test_form'] = \Drupal::formBuilder()->buildForm($form, $form_state);

It now all works (Drupal picks up the GET request and processes the form). It's still not what I'm expecting of it, but that's a whole other question, so I'll open a new question instead.

Added a properly setup example
Source Link
vrijdenker
  • 234
  • 3
  • 10
Loading
Source Link
vrijdenker
  • 234
  • 3
  • 10
Loading