Question

I created a custom error template

app/Resources/TwigBundle/views/Exception/error404.html.twig

and

app/Resources/TwigBundle/views/Exception/exception_full.html.twig

for development to change the layout to what ever I want to. This works fine. But, as soon as I add a form (e.g. my search or a contact form) using

{{ include ('MyCoreBundle:Default/forms:myCustomForm.html.twig' ) }}

or enter the code directly it shows a ResourceNotFoundException and a NotFoundHttpException instead of the 404 page.

Is there any way to display a form on the error templates?

Was it helpful?

Solution

What about using an embedded controller?

Here's the short example taken from Symfony's documentation page.

You have a controller that returns and renders some output:

// src/Acme/ArticleBundle/Controller/ArticleController.php
class ArticleController extends Controller
{
    public function recentArticlesAction($max = 3)
    {
        // make a database call or other logic
        // to get the "$max" most recent articles
        $articles = ...;

        return $this->render(
            'AcmeArticleBundle:Article:recentList.html.twig',
            array('articles' => $articles)
        );
    }
}

In this example, it renders a view:

{# src/Acme/ArticleBundle/Resources/views/Article/recentList.html.twig #}
{% for article in articles %}
    <a href="/article/{{ article.slug }}">
        {{ article.title }}
    </a>
{% endfor %}

and in the view you'd like to embed controller's action:

{# app/Resources/views/base.html.twig #}

{# ... #}
<div id="sidebar">
    {{ render(controller('AcmeArticleBundle:Article:recentArticles', {
        'max': 3
    })) }}
</div>

The docs page also says that you should use and embedded controllers when you need data that you don't have access to in a template. From my point of view, it's perfect for creating widgets, some statistics and so on.

Hope this helps.

Edit

To make this answer more accurate to your issue, here's the solution I'd use:

  1. You'll need a controller, let's call it SearchController
  2. In the controller's renderForm() method, you need to create a form and pass it to the view
  3. Form's action attribute should point to resultAction() method where you can fetch the results from your data storage and display result to the end user.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top