0

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

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

0

Your Answer

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

Browse other questions tagged or ask your own question.