Skip to content

Form rendering throws notice if the form is submitted with array instead of string #23995

Closed
@mhujer

Description

@mhujer
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 3.3.6

When the user uses Chrome Inspect feature and changes the <input type="text" name to include an extra array (e.g. from article_form[title] to article_form[title][foobar], the form fails to render. The reason is that the template is trying to render the "invalid" array as an <input> value.

Twig_Error_Runtime:
An exception has been thrown during the rendering of a template ("Notice: Array to string conversion").

  at vendor\symfony\symfony\src\Symfony\Bridge\Twig\Resources\views\Form\form_div_layout.html.twig:13
  at Twig_Template->displayBlock('form_widget_simple', array('value' => array('foo' => 'aa'), 'attr' => array(), 'form' => object(FormView), 'id' => 'article_form_title', 'name' => 'title', 'full_name' => 'article_form[title]', 'disabled' => false, 'label' => 'Article title', 'label_format' => null, 'multipart' => false, 'block_prefixes' => array('form', 'text', '_article_form_title'), 'unique_block_prefix' => '_article_form_title', 'translation_domain' => null, 'cache_key' => '_article_form_title_text', 'errors' => object(FormErrorIterator), 'valid' => false, 'data' => array('foo' => 'aa'), 'required' => true, 'size' => null, 'label_attr' => array(), 'compound' => false, 'method' => 'POST', 'action' => '', 'submitted' => true, 'app' => object(AppVariable)), array('form_widget' => array(object(__TwigTemplate_74fc98e978cb4218b308d1fa4d08196af014f2b1069a5f65da3ed6ecc38bdf83), 'block_form_widget'),
...

The problematic line in the template is this:

<input type="{{ type }}" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>

The simplest repro-case I managed to create is this one:

    public function createAction()
    {

        $form = $this->createFormBuilder(
            [
                'title' => [
                    'foo' => 'bar',
                ],
            ])
            ->add('title', TextType::class)
            ->getForm();

        return $this->render('article/add-article.html.twig', [
            'form' => $form->createView(),
        ]);
    }
# article/add-article.html.twig
{% block body %}
	{{ form(form) }}
{% endblock %}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions