0

How can I retrieve the currently submitted and validated form state?

Please assume the following scenario.

A page has a form on it created using the form API. The data in the submitted form is transient. In other words it is not saved or stored in any way.

The form is created by a module I do not control and therefore I should not alter the codebase.

On the same page as the form is a block (mine) which displays data based on the form submission.

I want to get access to the validated data from the anonymous user submitted form from within the block plugin code.

The form does all the necessary validation of the user submitted data. I do not want to replicate this validation so I need to get access to the validated form_state so I can extracted the submitted values.

I cannot see a way to retrieve the form_state object from the current request.

How can this be done?

  • By default a Form API form redirects after form submit, so the next page after submitting is built in a new request. In case the existing form really does nothing other than the default behavior the data is lost and you have to add a submit handler yourself in a form alter hook, either to change the default behavior or to store the submitted data. – 4k4 Jan 8 at 23:56
  • In this case the form is not being redirected but it is a good point. If I was to add an onsubmit handler how do I reference my block class? – DeveloperChris Jan 9 at 0:01
  • What is the form doing instead of redirecting? – 4k4 Jan 9 at 0:09
  • In this case nothing. the scenario is actually a little bit contrived. I am trying to learn how to get validated data from a submitted form in my block. We could assume say that the form retrieved data from the db and displayed it based on the submitted request and I want to display ancillary data in my block. In this situation a redirect is not the correct thing to do. In fact I was surprised its the default behaviour. – DeveloperChris Jan 9 at 0:23
1

As with most things you are learning. The answer is often simpler than first thought.

To resolve my issue I was able to use hook_form_alter and implement my own submission handler.

From here I was able to disable the redirect to prevent the page reloading.

Once redirect was disabled then my block plugin was able to get access to the form and use the validated results within it.

My block build for demo purposes contained the following

public function build() {

  // Get the current form.
  $form = \Drupal::formBuilder()->getForm('Drupal\modulename\Form\moduleForm');

  return [
    '#markup' => "<pre>" . print_r($form, TRUE) . "</pre>",
  ];
}

The important point is \Drupal::formBuilder()->getForm('name') returns the cached form results and therefore does not require any further validation.

This can be verified by checking $form['#validated'] == 1.

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.