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) }}
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.
Comments