Symfony - Using twig extension with form prevents errors from showing up

Rafael Adel

I'm trying to use twig extensions as widgets to render sections of the page. I have this class EventWidget, which is a twig extension that defines a twig function called event_widget_create that's responsible for rendering the create page of my entity.

The problem is, There's no form errors printed inside the view, even though there're errors displayed when I do var_dump($form->getErrorsAsString()) inside the controller or the widget function.

There's something that I noticed, when clicking submit, the fields data disappears and I get the feeling that the page reloads.

Here's my widget extension code:

class EventWidget extends \Twig_Extension
{
    /**
     * @var Twig_Environment
     */
    protected $env;

    /**
     * @var \Tsk\FEBundle\FormHandler\EventFormHandler
     */
    protected $eventFormHandler;

    function __construct(EventFormHandler $eventFormHandler)
    {
        $this->eventFormHandler = $eventFormHandler;
    }

    /**
     * @param Twig_Environment $environment
     */
    public function initRuntime(Twig_Environment $environment)
    {
        $this->env = $environment;
    }

    public function getFunctions()
    {
        return [
            new \Twig_SimpleFunction("event_widget_create", [$this, "getEventWidgetCreate"]),             
        ];
    }


    public function getEventWidgetCreate(FormInterface $form)
    {
        return $this->env->render("@Default/Partial/events_widget_create.html.twig",[
            "form" => $form->createView()
        ]);
    }

    /**
     * @return string
     */
    public function getName()
    {
        return "event_widget";
    }
}

And here's my controller:

/**
 * @Route("/")
 *
 * @package Tsk\FEBundle\Controller
 */
class EventController extends Controller
{
    /**
     * @Route("/event/create", name="tsk_fe_event_create")
     * @Template("@Default/event_create.html.twig")
     * @Security("has_role('ROLE_ARTIST')")
     */
    public function createEventAction(Request $request)
    {
        $handler = $this->get("tsk_fe.event_form.handler");
        $form = $handler->create();
        if ($request->getMethod() == "POST") {
            if ($handler->process($form)) {
                return new RedirectResponse($this->generateUrl("tsk_fe_default_index"));
            }
        }
        return ["form" => $form];
    }
}

And the form:

class EventType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add("title", "text", [
                "error_bubbling" => true
            ])
            ->add("description", "textarea", [
                "required" => false
            ])
            ->add("start", "thrace_datetimepicker", [
                "label" => "From",
                "error_bubbling" => true
            ])
            ->add("end", "thrace_datetimepicker", [
                "label" => "To",
                "error_bubbling" => true
            ])
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults([
            "data_class" => "Tsk\FEBundle\Entity\Event",
        ]);
    }

    public function getName()
    {
        return "event";
    }
}

And lastly the view:

This's the main view: "@Default/event_create.html.twig"

{% extends "@TskFE/layout.html.twig" %}
{% block content %}
    {{ event_widget_create(form)|raw }}
{% endblock content %}

And this's the partial view that's rendered by the widget: "@Default/Partial/events_widget_create.html.twig":

{% form_theme form with ['ThraceFormBundle:Form:fields.html.twig'] %}
{{ form_start(form, {"action": url("tsk_fe_event_create"), 'attr': {'novalidate': 'novalidate'} }) }}
    <div class="form-group">
        {{ form_label(form.title) }}
        {{ form_widget(form.title, {"attr":{"class": "form-control"} }) }}
        {{ form_errors(form.title) }}
    </div>
    <div class="form-group">
        {{ form_label(form.description) }}
        {{ form_widget(form.description, {"attr":{"class": "form-control"} }) }}
        {{ form_errors(form.description) }}
    </div>
    <div class="form-group">
        {{ form_label(form.start) }}
        {{ form_widget(form.start, {"attr":{"class": "form-control"} }) }}
        {{ form_errors(form.start) }}
    </div>
    <div class="form-group">
        {{ form_label(form.end) }}
        {{ form_widget(form.end, {"attr":{"class": "form-control"} }) }}
        {{ form_errors(form.end) }}
    </div>
    <input type="submit" value="Submit" class="btn btn-info"/>
<a href="{{ url("tsk_fe_default_index") }}">Cancel</a>
{{ form_rest(form) }}
{{ form_end(form) }}
egeloen

It seems you have bubbled your errors by using 'error_bubbling' => true. Not sure if it is what you want because you render error individually by field.

Error bubbling means all errors are attached to the parent form instead of being attach to the related field. So, if it is what you want, you must add {{ form_errors(form) }} on top of your form in your template in order to get your errors displayed otherwise, just remove the error_bubbling option.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Symfony2 Twig extension: Creating form

From Dev

Create a Symfony/Twig Form with data from the database

From Dev

Symfony2 ajax form validation render errors in twig

From Dev

Symfony2 ajax form validation render errors in twig

From Dev

Register Twig Extension in Symfony

From Dev

Any way to handle errors on symfony without using twig?

From Dev

Any way to handle errors on symfony without using twig?

From Dev

Validate the form if it is null/undefined using a twig form in symfony2

From Dev

How to access a global service function within a twig extension using Symfony?

From Dev

Symfony display form errors from validation method

From Dev

Symfony dependency injection in twig extension

From Dev

Activate StringLoader Twig Extension in Symfony

From Dev

Symfony Twig extension render view

From Dev

Access form.vars.value in Symfony subform using Twig

From Dev

Validation errors not showing up

From Dev

How to access from Twig to the underlying object in an embedded form in a Symfony project

From Dev

Extension method not showing up

From Dev

Showing Form Errors with MVC

From Dev

Symfony Form Collection Twig Rendering

From Dev

Twig/Symfony form template syntax

From Dev

Symfony 2 Form Errors

From Dev

Invalid Form in Symfony with no errors

From Dev

Symfony form dummy errors

From Dev

Showing form errors on a dropdown form

From Dev

Bootstrap prevents form from submission

From Dev

Include assets in Twig Extension with Symfony2

From Dev

Multiple Twig_Extension in Symfony2

From Dev

getting image extension in Twig Symfony2

From Dev

Twig Extension doesn't work with Symfony 2