Flag of Ukraine
SymfonyCasts stands united with the people of Ukraine

Frame Redirecting & Dynamic Frame Targets

Keep on Learning!

If you liked what you've learned so far, dive in!
Subscribe to get access to this tutorial plus
video, code and script downloads.

Start your All-Access Pass
Buy just this tutorial for $12.00

With a Subscription, click any sentence in the script to jump to that part of the video!

Login Subscribe

It was subtle, but we just saw one important property of Turbo frames. When we submitted this form successfully, it submitted to the edit action inside of ProductAdminController. This code handled the form submit and, because it was successful, it redirected to the public product show page.

It turns out, if you submit a form in a frame and that Ajax request redirects to another page, Turbo does not follow the redirect and navigate the entire page. Well, let me be more clear.

Redirects Do Not Move the Entire Page

Check out the network tools. This POST request was for the unsuccessful form submit we did a minute ago: the one that failed validation. This second request was for our successful form submit. And you can see that it returned a 302 redirect. When Turbo sees a redirect, it does follow it in a sense... it makes a second Ajax call to the redirected URL: the product show page. This is also how Turbo Drive works... but with one key difference: after making the second Ajax request, a Turbo frame does not navigate the entire page and update the URL in our browser to match the redirected URL.

Nope, because we submitted to a turbo frame, it reads the HTML of this redirected page, finds the product-info frame and loads just that into the frame.

This is... kind of hard to see in this case, because it's redirecting back to the URL that is already in our address bar. But this is the behavior: if you submit a form inside a frame, even if that request redirects, all navigation will stay inside the frame.

Actually, there is a super obvious place where we can see this. Go to the product admin area and edit a product. Like with the show page, the frame is targeting _top but the form is targeting product-info. If we clear out the title and submit, it submits to the frame and looks fine.

But if we put the title back, change it and submit, watch what happens. Ah! Frankenstein page! Half of the public product page just exploded onto this admin page!

Unfortunately... the turbo frame is doing exactly what we're asking it to do. Look at the network tools... and scroll up a bit. We submitted successfully to the edit page and that redirected to the public show page. Then, because we're submitting in a turbo-frame, the frame found the product-info frame on that page - which is all this product info - grabbed it, and popped it right here.

In the admin area... this is not what we want. And things are getting a bit complicated as a result of us really pushing for the best possible user experience.

So let's stop and think. When we load the form from the product show page and hit edit, we do want this form to submit into the frame. But when we load that same form in the product admin area, we kind of just want this to behave like normal, by submitting to the entire page. Could we do that? Could we make the same form behave differently based on the situation? Totally!

The Turbo-Frame Request Header

Head to ProductAdminController's edit action. Whenever turbo is navigating inside a frame, it sends an extra header called Turbo-Frame with the name of the frame. So when we click the edit link from the product show page, that Ajax request will add a Turbo-Frame header. You can see it all the way down here under request headers... there it is: Turbo-Frame: product-info.

But when navigate directly to the product admin area and look at that Ajax request, down here, there is no Turbo-Frame header. This means we can detect whether a request is being loaded inside a turbo frame from inside of Symfony!

Back in the controller, when we render the template, pass in a new variable called formTarget set to $request->headers->get('Turbo-Frame'). If that header was not sent, add a second argument to default this to _top.

... lines 1 - 61
/**
* @Route("/{id}/edit", name="product_admin_edit", methods={"GET","POST"})
*/
public function edit(Request $request, Product $product): Response
{
... lines 68 - 82
return $this->renderForm('product_admin/edit.html.twig', [
'product' => $product,
'form' => $form,
'formTarget' => $request->headers->get('Turbo-Frame', '_top')
]);
}
... lines 89 - 104

Now in _form.html.twig, instead of setting the target to product-info, use the formTarget variable. And because this template is also included on the new product page... and we're not setting this variable there, code defensively by defaulting it to _top.

{{ form_start(form, {
attr: { 'data-turbo-frame': formTarget|default('_top') }
}) }}
{{ form_widget(form) }}
<button class="btn btn-primary" formnovalidate>{{ button_label|default('Save') }}</button>
{{ form_end(form) }}

I think that's going to do it! Refresh the product admin page and hit save. Beautiful! That submitted to the entire page and redirected the entire page. Now click edit, empty the title and hit enter. Yes: this still navigates inside the frame. If you inspect element on the form, you can see that it does have the extra data-turbo-frame attribute set to product-info.

So, inline product admin form done! I included this example both because it's really cool to load the form inline... but also because it shows a situation where turbo frames can get a bit complex. It's up to you to balance the added complexity with the user experience that you want.

Next: what about using a turbo frame inside of a modal? After all, you often want navigation - like links and form submits inside of a modal - to stay inside of that modal... which is what turbo frames are really good at. So let's transform this modal into a turbo-frame powered modal.

Leave a comment!

5
Login or Register to join the conversation
Joel-L Avatar

Hey,
i'm using Turbo but it seems by default removing anchor (_fragment) when i use redirectToRoute, here is a way to fix it ?

Reply

Hey Joel L.!

Can you tell me about more about what's going on? You're using redirecToRoute() to return a redirect response, but you're adding a #something to the end of it (is that what you mean by "anchor")? Or were you referring to Symfony's fragments system - https://symfonycasts.com/screencast/turbo/frames-find-frames#using-fragment-uri ?

Cheers!

Reply
Joel-L Avatar
Joel-L Avatar Joel-L | weaverryan | posted 1 year ago | edited

yes fragments system, like this :
`
return $this->redirectToRoute('app_sales_edit', [

            '_fragment' => 'logistic',
            'id' => $sale->getId()
        ]);`

The fragment in not present

Reply

Hey Joel L.!

Gotcha :). I have not personally tried this. However, it DOES appear that this may be a legit issue with Turbo - https://github.com/hotwired...

If that's correct, I'm not sure if any reasonable way to workaround it at the moment, other than disabling Turbo for the link or form submit that then redirects using the fragment... which is doable, but not great.

Sorry I can't give a better answer!

Cheers!

Reply
Joel-L Avatar

ok thanks

Reply
Cat in space

"Houston: no signs of life"
Start the conversation!

What PHP libraries does this tutorial use?

// composer.json
{
    "require": {
        "php": ">=8.1",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "composer/package-versions-deprecated": "1.11.99.1", // 1.11.99.1
        "doctrine/annotations": "^1.0", // 1.13.1
        "doctrine/doctrine-bundle": "^2.2", // 2.3.2
        "doctrine/orm": "^2.8", // 2.9.1
        "phpdocumentor/reflection-docblock": "^5.2", // 5.2.2
        "sensio/framework-extra-bundle": "^6.1", // v6.1.4
        "symfony/asset": "5.3.*", // v5.3.0-RC1
        "symfony/console": "5.3.*", // v5.3.0-RC1
        "symfony/dotenv": "5.3.*", // v5.3.0-RC1
        "symfony/flex": "^1.3.1", // v1.18.5
        "symfony/form": "5.3.*", // v5.3.0-RC1
        "symfony/framework-bundle": "5.3.*", // v5.3.0-RC1
        "symfony/property-access": "5.3.*", // v5.3.0-RC1
        "symfony/property-info": "5.3.*", // v5.3.0-RC1
        "symfony/proxy-manager-bridge": "5.3.*", // v5.3.0-RC1
        "symfony/runtime": "5.3.*", // v5.3.0-RC1
        "symfony/security-bundle": "5.3.*", // v5.3.0-RC1
        "symfony/serializer": "5.3.*", // v5.3.0-RC1
        "symfony/twig-bundle": "5.3.*", // v5.3.0-RC1
        "symfony/ux-chartjs": "^1.1", // v1.3.0
        "symfony/ux-turbo": "^1.3", // v1.3.0
        "symfony/ux-turbo-mercure": "^1.3", // v1.3.0
        "symfony/validator": "5.3.*", // v5.3.0-RC1
        "symfony/webpack-encore-bundle": "^1.9", // v1.11.2
        "symfony/yaml": "5.3.*", // v5.3.0-RC1
        "twig/extra-bundle": "^2.12|^3.0", // v3.3.1
        "twig/intl-extra": "^3.2", // v3.3.0
        "twig/string-extra": "^3.3", // v3.3.1
        "twig/twig": "^2.12|^3.0" // v3.3.2
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.4", // 3.4.0
        "symfony/debug-bundle": "^5.2", // v5.3.0-RC1
        "symfony/maker-bundle": "^1.27", // v1.31.1
        "symfony/monolog-bundle": "^3.0", // v3.7.0
        "symfony/stopwatch": "^5.2", // v5.3.0-RC1
        "symfony/var-dumper": "^5.2", // v5.3.0-RC1
        "symfony/web-profiler-bundle": "^5.2", // v5.3.0-RC1
        "zenstruck/foundry": "^1.10" // v1.10.0
    }
}

What JavaScript libraries does this tutorial use?

// package.json
{
    "devDependencies": {
        "@babel/preset-react": "^7.0.0", // 7.13.13
        "@fortawesome/fontawesome-free": "^5.15.3", // 5.15.3
        "@hotwired/turbo": "^7.0.0-beta.5", // 1.2.6
        "@popperjs/core": "^2.9.1", // 2.9.2
        "@symfony/stimulus-bridge": "^2.0.0", // 2.1.0
        "@symfony/ux-chartjs": "file:vendor/symfony/ux-chartjs/Resources/assets", // 1.1.0
        "@symfony/ux-turbo": "file:vendor/symfony/ux-turbo/Resources/assets", // 0.1.0
        "@symfony/ux-turbo-mercure": "file:vendor/symfony/ux-turbo-mercure/Resources/assets", // 0.1.0
        "@symfony/webpack-encore": "^1.0.0", // 1.3.0
        "bootstrap": "^5.0.0-beta2", // 5.0.1
        "chart.js": "^2.9.4",
        "core-js": "^3.0.0", // 3.13.0
        "jquery": "^3.6.0", // 3.6.0
        "react": "^17.0.1", // 17.0.2
        "react-dom": "^17.0.1", // 17.0.2
        "regenerator-runtime": "^0.13.2", // 0.13.7
        "stimulus": "^2.0.0", // 2.0.0
        "stimulus-autocomplete": "https://github.com/weaverryan/stimulus-autocomplete#toggle-event-always-dist", // 2.0.0
        "stimulus-use": "^0.24.0-1", // 0.24.0-2
        "sweetalert2": "^11.0.8", // 11.0.12
        "webpack-bundle-analyzer": "^4.4.0", // 4.4.2
        "webpack-notifier": "^1.6.0" // 1.13.0
    }
}
userVoice