Symfony3为什么form.vars中的变量“标签”为空

时间:2019-02-24 16:46:03

标签: forms symfony variables twig

我想访问其中的变量“标签”

form.postalCode.vars.label

我有一个表单对象:“ form”,然后在以下项的树枝中创建了转储:

{{ dump(form.postalCode.vars) }}

这是结果:

array:24 [▼
  "value" => ""
  "attr" => array:1 [▶]
  "form" => FormView {#771 ▶}
  "id" => "appbundle_test_name"
  "name" => "name"
  "full_name" => "appbundle_test[name]"
  "disabled" => false
  "label" => null
  "label_format" => null
  "multipart" => false
  "block_prefixes" => array:3 [▶]
  "unique_block_prefix" => "_appbundle_test_name"
  "translation_domain" => null
  "cache_key" => "_appbundle_test_name_text"
  "errors" => FormErrorIterator {#765 ▶}
  "valid" => true
  "data" => null
  "required" => true
  "size" => null
  "label_attr" => []
  "compound" => false
  "method" => "POST"
  "action" => ""
  "submitted" => false
]

基于此文档:https://symfony.com/doc/3.4/reference/forms/twig_reference.html#twig-reference-form-variables 使用“标签”变量,我应该可以访问标签的标题。我的问题是为什么这个变量为空。我从表单中检查了每个字段,并且该字段的每个标签都是空的... 但是当我使用form_label生成标签时->生成的标签没有任何问题。

1 个答案:

答案 0 :(得分:2)

我认为您首先需要在表单构建器的postalCode字段中添加标签,就像这样


    $builder = $this->add('postalCode', TextType::class, ['label' => 'Post code']);