在树枝模板中呈现的Symfony表格,因为控制器未提交

发布于 2025-02-09 00:07:51 字数 1872 浏览 1 评论 0原文

我想在base.html.twig中使用简单的“搜索”输入字段。通常,我需要编写代码以在每条路线中维护形式。为了解决这个问题,我决定使用直接在base.html.twig Template的途径中创建单独的控制器:

<div class="top-middle col-12 col-md-6 d-flex order-2 order-md-1">
{{ render(controller("App\\Controller\\SearchController::searchProduct"))}}
</div>

它的工作发现,除了提交表单时什么都没有发生。我在我的一条路线中以正常的方式尝试了它,而且工作正常。所以不知道问题在哪里。

我的搜索controller带有twig中呈现的路由:

class SearchController extends AbstractController
{
    #[Route('search-product', name: 'search_product')]
    public function searchProduct(Request $request)
    {
        $searchForm = $this->createForm(SearchProductType::class);
        $searchForm->handleRequest($request);
      
        if ($searchForm->isSubmitted() && $searchForm->isValid()) {
            
            dump('Form submitted');
        }
        return $this->render('components/search-input.html.twig', [
            'searchForm' => $searchForm->createView()
            ]);
    }
}

search input.html.twig组件:

<div class="top-search">
            <i class="bi-search top-search__icon"></i>
            {{ form(searchForm) }}
</div>

和主控制器,它呈现index.html.twig thtwig base.html.twig:

 #[Route('/', name: 'home')]
    public function index(FileHandler $fileHandler, SessionInterface $session, Request $request): Response
    {
      
        $products = $this->doctrine->getRepository(Product::class)->getProducts('Dresses', 4);
        $products = $this->addPathToImages($products, 'Dresses');
        
        return $this->render('shop/index.html.twig', [
            'products' => $products
        ]);
    }

该行

dump('Form submitted');

当表单被提交时, 未执行。页面刷新,但什么也不会发生。 我认为整个逻辑应该留在此路线/控制器中,还是我缺少某些内容?

I'd like to have simple "Search" input field in base.html.twig. Normally I would need to write code to maintain form in every route. To solve this problem I decided to create separate controller with route to render it directly in base.html.twig template:

<div class="top-middle col-12 col-md-6 d-flex order-2 order-md-1">
{{ render(controller("App\\Controller\\SearchController::searchProduct"))}}
</div>

It works find except nothing happens when the form is submitted. I tried it in normal way in one of my routes and it was working fine. So don't know where the problem is.

My SearchController with route which is rendered in twig :

class SearchController extends AbstractController
{
    #[Route('search-product', name: 'search_product')]
    public function searchProduct(Request $request)
    {
        $searchForm = $this->createForm(SearchProductType::class);
        $searchForm->handleRequest($request);
      
        if ($searchForm->isSubmitted() && $searchForm->isValid()) {
            
            dump('Form submitted');
        }
        return $this->render('components/search-input.html.twig', [
            'searchForm' => $searchForm->createView()
            ]);
    }
}

Search input.html.twig component:

<div class="top-search">
            <i class="bi-search top-search__icon"></i>
            {{ form(searchForm) }}
</div>

and the main controller which renders index.html.twig with base.html.twig:

 #[Route('/', name: 'home')]
    public function index(FileHandler $fileHandler, SessionInterface $session, Request $request): Response
    {
      
        $products = $this->doctrine->getRepository(Product::class)->getProducts('Dresses', 4);
        $products = $this->addPathToImages($products, 'Dresses');
        
        return $this->render('shop/index.html.twig', [
            'products' => $products
        ]);
    }

The line

dump('Form submitted');

is not executed when the form is submitted. Page refreshes but nothing happens.
I think the whole logic should stay in this route/controller or I am missing something?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆橘 2025-02-16 00:07:51

根据要求,我发布了我的解决方案:

而不是直接将控制器嵌入树枝文件中,而是决定处理我的小表单(只需搜索输入,通过按JS按“ Enter”提交)。原因是不可能从嵌入式控制器重定向。
Twig中的代码:

<form id="top-search-form">
                <div class="top-search">
                        <input id="search-string"
                        class="top-search__input" type="search" 
                        placeholder="Search shop">
                </div>
</form>             

和JavaScript编写的代码(需要FosjSrouting Bundle):

const routes = require('/public/js/fos_js_routes.json');
import Routing from '/vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';

Routing.setRoutingData(routes);

document.addEventListener('DOMContentLoaded', function() {
    const searchForm = document.getElementById('top-search-form');
    
    searchForm.addEventListener('submit', function(e) {
        e.preventDefault();
        const searchString = document.getElementById('search-string').value;

        var url = Routing.generate('items_filter', {
            'searchText': searchString
        });
       
        location.href = url;
    });
})

As requested I publish my solution:

Instead of embedding controller directly in Twig file and decided to handle my little form (just Search input, submitted by pressing "enter") with js. The reason for this is that it's impossible to redirect from embedded controller.
Code in twig:

<form id="top-search-form">
                <div class="top-search">
                        <input id="search-string"
                        class="top-search__input" type="search" 
                        placeholder="Search shop">
                </div>
</form>             

and code written in Javascript (requires FOSJSRouting Bundle):

const routes = require('/public/js/fos_js_routes.json');
import Routing from '/vendor/friendsofsymfony/jsrouting-bundle/Resources/public/js/router.min.js';

Routing.setRoutingData(routes);

document.addEventListener('DOMContentLoaded', function() {
    const searchForm = document.getElementById('top-search-form');
    
    searchForm.addEventListener('submit', function(e) {
        e.preventDefault();
        const searchString = document.getElementById('search-string').value;

        var url = Routing.generate('items_filter', {
            'searchText': searchString
        });
       
        location.href = url;
    });
})
痴骨ら 2025-02-16 00:07:51

您好,我将此代码与Symfony 5.4和6.2一起在控制器中重定向是一个sub请求。

$response = $this->redirectToRoute('my_route');
$response->sendHeaders();
exit;

这很好

Hello i use this code with Symfony 5.4 and 6.2 to redirect in a controller wich is a sub request.

$response = $this->redirectToRoute('my_route');
$response->sendHeaders();
exit;

this works fine

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文