Score:0

Can I get form values in a controller after form has been submitted?

cw flag

I'm working in D10 and what I have is a simple search form. It's two fields 'searchKey' (textfield) and 'showHidden' (checkbox). When the form is submitted a list of records is return and displayed in a table but I want the search form to always be available. What I have done is used a controller to build the record tables and the form api to build the search form itself.

The controller loads the form via:

$form = $this->formBuilder->getForm('\Drupal\myModule\Form\SimpleSearchForm');

Currently in the form submit I'm doing this:

$searchKey = $form_state->getValue('searchKey');
$form_state->setRedirectUrl(
    Url::FromRoute('myModule.calendars.search', ['searchKey' => Html::escape($searchKey)])
);

This works fine in that $searchKey is pulled off the route in the controller passed to the method that does the search and then builds out the table of result. Since I've added the showHidden option it seems ugly to pass the form field values as route parameters. If I have more then two that really get ugly.

So is there a better way to pass the form values back to a controller or should I just use a form to do it all?

Using only a form seems counter-intuitive to me because the list of results is formatted and has links added for each row. This all seems like it's better served in a controller and the search form is just that, the form with the search parameters. If my thinking is correct (which it may not be) I need a clean way of getting the search forms fields back to the controller and I can't seem to figure that out. I've tried looking at Request $request but they are not part of that object.

Any thoughts? Am I chasing my tail?

Thanks

Jaypan avatar
de flag
Does this maybe help? https://www.jaypan.com/tutorial/drupal-form-api-ajax-form-and-results-same-page
4uk4 avatar
cn flag
For search forms, in general, the simplest way is to use the GET method instead of POST. The search key is then a query parameter accessible to form and controller.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.