Symfony6 input submission and routing for GET from twig template

61 Views Asked by At

I am working in Symfony6 getting info from a readonly endpoint. I am rewriting an existing but severely deprecated app, so I know exactly how the stuff needs to go together to update what is retrieved. Also I am not using Symfony forms as this is massively simple and nothing is getting stored or anything like that. My homepage is doing just fine hitting '/', and if I copy and paste all of the '?foo=bar&date=2023-11-16' to the end it pulls it up with the correct stuff in the correct places as expected. However I can't seem to figure out how to get the info from the template into the thing and updating the view.

<div>
    <form class="class" role="form" action="this-is-the-question-i-think" method="get">
        <label for="foo" class="form-label">Foo</label>
        <input class="form-control" type="text" value="{{ bar }}">
        <label for="date" class="form-label">Date:</label>
<input type="date" class="form-control" id="theDate" value="{{ dateVariable }}">
        <button type="submit">Update View</button>
    </form>
</div>

I have tried action = "{{ path(path) }}" which fills in the route name 'app_default_homepage' which loads the homepage without the default values and not the input. I have tried action = "{{ relative_path('/?foo=bar&date=2023-11-16') }}" which loads the '/?' but default values (hardwiring what's added thinking if I can even get it to load the page with the values added to the url, I can then maybe tackle a twig extension to create the string for the url from the input values. I don't know if I need to change to adding an onsubmit to the button instead of using an action to update the variable values in a controller and then redirecting to whatever string I create, but some of my fumblings have thrown the error that "Unable to get URL for named route '/?foo=bar&date=2023-11-16'" or similar, and I'm running out of spaghetti to throw at the wall or new wordings to search.

Editing to add controller code as requested:

#[Route('/', name: 'app_default_homepage')]
public function homepage(Request $request)
{
    $stats = null;
// this baseInit sets the date formatting, region is null on the homepage at first load, the after the ? has region=stuff that sets to entire repo
    $this->baseInit($request);

    if ($this->region == null) {
        $entireRepository = new Region("1", "Repository", "Entire Repository");
        $this->region = $entireRepository->getDisplayId();
// retrieveStatsForEntireRepository sets the $this->region and then passes to same retrieveStats as in the else below
        $stats = $this->statsService->retrieveStatsForEntireRepository(
            $this->fromDate,
            $this->toDate
        );
    } else {
        $stats = $this->statsService->retrieveStats(
            $this->region,
            $this->fromDate,
            $this->toDate
        );
    }
    return $this->render('homepage.html.twig', [
        'stats' => $stats,
    ]);
}

}

1

There are 1 best solutions below

1
Maggie may On

After figuring out this was not actually submitting the form, I set the action back to {{ path(path) }} in the twig template, took it all the way back to w3schools php form handling page, and changed my <button> to an <input type="submit"...

I'm assuming the change to input instead of button is the part that fixed this specific issue. On to the next one.