Symfony:在呈现模板期间抛出异常 ("Notice: Array to string conversion")。当我尝试将 class 添加到标签时

Symfony: An exception has been thrown during the rendering of a template ("Notice: Array to string conversion"). when I try to add a class to a label

我有以下表格:

namespace AppBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Misd\PhoneNumberBundle\Form\Type\PhoneNumberType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Validator\Constraints\IsTrue as TrueConstraint;

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('name',TextType::class,["label"=>"register.name","required"=>true,'translation_domain' => 'FOSUserBundle'])
                ->add('surname',TextType::class,["label"=>"register.surname","required"=>true,'translation_domain' => 'FOSUserBundle'])
                ->add('phone',PhoneNumberType::class,["label"=>"register.phonenum",'translation_domain' => 'FOSUserBundle'])
                ->add('organization',TextType::class,["label"=>"register.organization","required"=>false,'translation_domain' => 'FOSUserBundle'])
                ->add('occupation',TextType::class,["label"=>"register.position","required"=>false,'translation_domain' => 'FOSUserBundle'])
                ->add('accept_terms',CheckboxType::class,[
                                                          "label"=>"register.acceptTerms",
                                                          "required"=>true,
                                                          'translation_domain' => 'FOSUserBundle',
                                                          'mapped' => false,
                                                          'constraints' => new TrueConstraint(array('message' => 'Your Confirmation Message','groups' => 'Registration')),]);
    }

    public function getParent()
    {
        return 'FOS\UserBundle\Form\Type\RegistrationFormType';
    }

    public function getBlockPrefix()
    {
        return 'app_user_registration';
    }

    // For Symfony 2.x
    public function getName()
    {
        return $this->getBlockPrefix();
    }
}

使用以下模板呈现:

register_content.html.twig

{% trans_default_domain 'FOSUserBundle' %}

{{ form_errors(form) }}
  <div class="form-row">
    <div class="form-group col-xs-12 col-sm-6">
        {{ form_label(form.name) }}
        {{ form_widget(form.name, {'attr': {'class': 'form-control'}}) }}
    </div>
    <div class="form-group col-xs-12 col-sm-6">
        {{ form_label(form.surname) }}
        {{ form_widget(form.surname, {'attr': {'class': 'form-control'}}) }}
    </div>
  </div>
  <div class="form-row">
    <div class="form-group col">
      {{ form_label(form.email) }}
      {{ form_widget(form.email, {'attr': {'class': 'form-control'}}) }}
    </div>
  </div>

  <div class="form-row">
    <div class="form-group col">
      {{ form_label(form.username) }}
      {{ form_widget(form.username, {'attr': {'class': 'form-control'}}) }}
    </div>
  </div>
  <div class="form-row">
    <div class="form-group col-xs-12 col-sm-6">
      {{ form_label(form.plainPassword.first) }}
      {{ form_widget(form.plainPassword.first, {'attr': {'class': 'form-control'}}) }}
    </div>
    <div class="form-group col-xs-12 col-sm-6">
      {{ form_label(form.plainPassword.second) }}
      {{ form_widget(form.plainPassword.second, {'attr': {'class': 'form-control'}}) }}
    </div>
  </div>
  <div class="form-row">
    <div class="form-group col">
      {{ form_label(form.phone) }}
      {{ form_widget(form.phone, {'attr': {'class': 'form-control'}}) }}
    </div>
  </div>
  <div class="form-row">
    <div class="form-group col">
      {{ form_label(form.organization) }}
      {{ form_widget(form.organization, {'attr': {'class': 'form-control'}}) }}
    </div>
  </div>
  <div class="form-row">
    <div class="form-group col">
      {{ form_label(form.occupation) }}
      {{ form_widget(form.occupation, {'attr': {'class': 'form-control'}}) }}
    </div>
  </div>

还有 register_no_login.html.twig

{% extends "@FOSUserBundle/layout.html.twig" %}
{% block title %} Register {% endblock title %}

{% block fos_user_content %}

  {% trans_default_domain 'FOSUserBundle' %}

  <div class="row clearfix"><div class="col"><h1 style="text-align:center">{{ 'register.title'|trans }}</h1></div></div>
  <div class="row clearfix">
    {{ form_start(form, {'method': 'post', 'action': path('fos_user_registration_register'), 'attr': {'class': 'fos_user_registration_register col'}}) }}
      {% include "@FOSUser/Registration/register_content.html.twig" %}
      <div class="form-row">
        <div class="form-group col">
          <div class="form-check">
            {{ form_label(form.accept_terms, {'attr':{'class':'form-check-label'}}) }}
            {{ form_widget(form.accept_terms, {'attr': {'class': 'form-check-input'}}) }}
          </div>
      </div>
      <div class="form-row">
        <input type="submit" class="btn btn-primary" value="{{ 'registration.submit'|trans }}" />
      </div>
    {{ form_end(form) }}
  </row>

{% endblock fos_user_content %}

我的问题是出现以下错误:

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

我发现它被触发了,因为我有这个标签定义:

        {{ form_label(form.accept_terms, {'attr':{'class':'form-check-label'}}) }}

但是当我改成那个定义时:

        {{ form_label(form.accept_terms) }}

一切似乎都正常(除了 html 的错误渲染部分)。所以我想知道为什么会导致该错误以及如何将 class form-check-label 添加到我的标签中?

编辑 2

根据要求,我在表单中放置了以下树枝片段:

        {{ dump(form.accept_terms) }}

和returns以下响应:

FormView {#4272 ▼
  +vars: array:25 [▼
    "value" => "1"
    "attr" => []
    "form" => FormView {#4272}
    "id" => "fos_user_registration_form_accept_terms"
    "name" => "accept_terms"
    "full_name" => "fos_user_registration_form[accept_terms]"
    "disabled" => false
    "label" => "register.acceptTerms"
    "label_format" => null
    "multipart" => false
    "block_prefixes" => array:3 [▶]
    "unique_block_prefix" => "_fos_user_registration_form_accept_terms"
    "translation_domain" => "FOSUserBundle"
    "cache_key" => "_fos_user_registration_form_accept_terms_checkbox"
    "errors" => FormErrorIterator {#4273 ▶}
    "valid" => true
    "data" => false
    "required" => true
    "size" => null
    "label_attr" => []
    "compound" => false
    "method" => "POST"
    "action" => ""
    "submitted" => false
    "checked" => false
  ]
  +parent: FormView {#4228 ▼
    +vars: array:24 [▼
      "value" => User {#1742 ▼
        #id: null
        #name: null
        #surname: null
        #phone: null
        #organization: null
        #occupation: null
        #username: null
        #usernameCanonical: null
        #email: null
        #emailCanonical: null
        #enabled: true
        #salt: null
        #password: null
        #plainPassword: null
        #lastLogin: null
        #confirmationToken: null
        #passwordRequestedAt: null
        #groups: null
        #roles: []
      }
      "attr" => []
      "form" => FormView {#4228}
      "id" => "fos_user_registration_form"
      "name" => "fos_user_registration_form"
      "full_name" => "fos_user_registration_form"
      "disabled" => false
      "label" => null
      "label_format" => null
      "multipart" => false
      "block_prefixes" => array:4 [▶]
      "unique_block_prefix" => "_fos_user_registration_form"
      "translation_domain" => null
      "cache_key" => "_fos_user_registration_form_app_user_registration"
      "errors" => FormErrorIterator {#4229 ▶}
      "valid" => true
      "data" => User {#1742 ▶}
      "required" => true
      "size" => null
      "label_attr" => []
      "compound" => true
      "method" => "POST"
      "action" => ""
      "submitted" => false
    ]
    +parent: null
    +children: array:10 [▼
      "email" => FormView {#4232 ▶}
      "username" => FormView {#4236 ▶}
      "plainPassword" => FormView {#4240 ▶}
      "name" => FormView {#4252 ▶}
      "surname" => FormView {#4256 ▶}
      "phone" => FormView {#4260 ▶}
      "organization" => FormView {#4264 ▶}
      "occupation" => FormView {#4268 ▶}
      "accept_terms" => FormView {#4272}
      "_token" => FormView {#4299 ▶}
    ]
    -rendered: false
    -methodRendered: true
  }
  +children: []
  -rendered: false
  -methodRendered: false
}

您目前正在尝试将属性数组设置为标签的字符串。与

        {{ form_label(form.accept_terms, {'attr':{'class':'form-check-label'}}) }}

像这样更改它会起作用:

        {{ form_label(form.accept_terms, "your label", {'attr':{'class':'form-check-label'}}) }}

其中 "your label" 是您想要作为此字段标签的任何内容。

为了解决您的问题,请更改:

{{ form_label(form.accept_terms, {'attr':{'class':'form-check-label'}}) }}

进入:

{{ form_label(form.accept_terms, null, {'label_attr':{'class':'form-check-label'}}) }}

发生这种情况是因为第二个参数是某种数组,需要传递给第三个参数也看看这个 answer 解释你需要传递 label_attr 而不是普通 attr.