duankui6150
2013-12-02 12:16
浏览 44
已采纳

访问twig中未映射的表单字段(Symfony 2.3)

I have form with hidden unmapped field:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        ...
        $builder
            ...
            ...
            ->add(
                'geolocationBreadcrumbs',
                'hidden',
                array(
                    'required' => false,
                    'mapped' => false
                )
            );
    }

I want to get access to value of that field in TWIG via {{ form.vars.value.field_name }}

<div>{{ form.vars.value.geolocationBreadcrumbs }}</div>

But I have an error: Method "geolocationBreadcrumbs" for object "Site\UserBundle\Entity\User" does not exist in "%path_to_twig_template%"

Why does Symfony 2.3 try to find method for unmapped field in Entity Class and how I can to get access to unmapped filed value direclty from twig?

Thanks.

图片转代码服务由CSDN问答提供 功能建议

我的表单中包含隐藏的未映射字段:

  public function  buildForm(FormBuilderInterface $ builder,array $ options)
 {
 ... 
 $ builder 
 ... 
 ... 
  - &gt; add(
'geolocationBreadcrumbs',
'hidden'  ,
数组(
'必需'=&gt; false,
'映射'=&gt; false 
)
); 
} 
   
 
 

我想通过{{form.vars.value.field_name}}

 &lt; div&gt; {{form.vars.value'访问TWIG中该字段的值 .geolocationBreadcrumbs}}&lt; / div&gt; 
   
 
 

但是我有一个错误:对象“Site \ UserBundle \ Entity \ User”的方法“geolocationBreadcrumbs”不存在 in“%path_to_twig_template%”

为什么Symfony 2.3尝试在实体类中查找未映射字段的方法以及如何访问未映射的字段值direclty 来自树枝?

谢谢。

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • duanliaoyin3171 2013-12-02 12:51
    已采纳

    You can access value of this field calling:

    {{ form.geolocationBreadcrumbs.vars.value }}
    

    Calling form.vars.value you get access to User entity and that's why Symfony2 returns exception.

    打赏 评论

相关推荐 更多相似问题