We have a strange error in our Symfony 2 project. We use Symfony forms, and we don't have any problems in our development environment, but do receive messages of an error in production. An error we can't replicate, and never had. Everything works fine when we test (both in prod & dev) but we do receive notifications of this error happening.
We are notified of the error:
"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Type error: Argument 1 passed to Symfony\Component\Form\FormRenderer::renderBlock() must be an instance of Symfony\Component\Form\FormView, null given
Most important code (Stripped for compactness & readability )
Formtype:
class WishlistType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('rank', HiddenType::class)
->add('description', TextType::class)
// Image is deprecated and will be removed.
->add('image');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(
[
'data_class' => Item::class,
]
);
}
}
Controller:
public function showAction(Request $request, $url)
{
/** @var Participant $participant */
$participant = $this->get('participant_repository')->findOneByUrl($url);
if ($participant === null) {
throw new NotFoundHttpException();
}
$wishlistForm = $this->createForm(
WishlistType::class,
$participant,
[
'action' => $this->generateUrl(
'wishlist_update',
['url' => $participant->getUrl()]
),
]
);
if (!$request->isXmlHttpRequest()) {
return [
'entry' => $participant,
'wishlistForm' => $wishlistForm->createView(),
];
}
}
Twig template:
{{ form_start(wishlistForm, {'attr': {'id': 'add_item_to_wishlist_form'}}) }}
{{ form_row(wishlistForm._token) }}
<table>
<!-- ADD SOMETHING TO YOUR WISHLIST -->
<thead>
<tr>
<th>#</th>
<th>{{ 'entry_show_valid.label.description'|trans }}</th>
<th>
<button type="button" class="btn btn-mini btn-success add-new-entry">
{{ 'entry_show_valid.btn.add_wishlist'|trans }}
</button>
</th>
</tr>
</thead>
<!-- OVERVIEW OF ITEMS IN WISHLIST, AND POSSIBILITY TO REMOVE SINGLE ITEMS-->
<tbody>
{% for item in wishlistForm.wishlistItems %}
<tr>
<td>{{ form_widget(item.rank) }}
<span>{{ item.rank.vars.value }}</span>
</td>
<td>
{{ form_widget(item.description, {'attr': {'class': 'wishlistitem-description'} }) }}
</td>
<td>
<button type="submit" >
<span>{{ 'entry_show_valid.btn.update_item'|trans }}</span>
</button>
<button type="button">
<span>{{ 'entry_show_valid.btn.remove_item'|trans }}</span>
</button>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ form_end(wishlistForm, {'render_rest': false}) }}
Form handling is done over Ajax, but request is handled as regular symfony form ($wishlistForm->handleRequest($request);
etc...).