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');
    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?


  • Does this maybe help? jaypan.com/tutorial/…
    – Jaypan
    Commented Apr 13, 2023 at 22:22
  • 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.
    – 4uk4
    Commented Apr 14, 2023 at 12:37


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.