douxie4583
2014-09-13 08:13
浏览 259
已采纳

Symfony2嵌入表单在树枝模板中呈现

I'm new to Symfony and twig templates. The problem I have is that I can't figure it out how to render the embedded form's fields separately in a twig template. I tried to search for it but probably others used a bit different forms and those examples didn't work for me.

My forms:

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('user', new UserType());
        $builder->add(
            'terms',
            'checkbox',
            array('property_path' => 'termsAccepted')
        );
        $builder->add('Register', 'submit');
    }

    public function getName()
    {
        return 'registration';
    }
}

and

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('username', 'text');
        $builder->add('name', 'text');
        $builder->add('email', 'email');
        $builder->add('password', 'repeated', array(
           'first_name'  => 'password',
           'second_name' => 'confirm',
           'type'        => 'password',
           'invalid_message' => 'Neteisingai pakartotas slaptažodis.',
        ));
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Keliones\MainBundle\Entity\User'
        ));
    }

    public function getName()
    {
        return 'user';
    }
}

How the rendering field by field should look for that?

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

我是Symfony和twig模板的新手。 我遇到的问题是我无法弄清楚如何在树枝模板中单独渲染嵌入表单的字段。 我试图搜索它,但可能其他人使用了一些不同的形式,这些例子对我不起作用。

我的表格:

 <  code> class RegistrationType扩展AbstractType 
 {
公共函数buildForm(FormBuilderInterface $ builder,array $ options)
 {
 $ builder-&gt; add('user',new UserType()); 
 $ builder-  &gt;添加(
'terms',
'复选框',
数组('property_path'=&gt;'termsAccepted')
); 
 $ builder-&gt; add('Register','submit'  ); 
} 
 
公共函数getName()
 {
返回'registration'; 
} 
} 
   
 
 

 类UserType扩展AbstractType 
 {
公共函数buildForm(FormBuilderInterface $ builder,array $ options)
 {
 $ builder-&gt; add('username','  text'); 
 $ builder-&gt; add('name','text'); 
 $ builder-&gt; add('email','email'); 
 $ builder-&gt; add(' 密码','重复',数组(
'first_name'=  &GT;  'password',
'second_name'=&gt;  '确认',
'类型'=&gt;  'password',
'invalid_message'=&gt;  'Neteisingaipakartotasslaptažodis。',
)); 
} 
 
公共函数setDefaultOptions(OptionsResolverInterface $ resolver)
 {
 $ resolver-&gt; setDefaults(array(
'data_class'=&gt;')  Keliones \ MainBundle \ Entity \ User'
)); 
} 
 
公共函数getName()
 {
返回'user'; 
} 
} 
   
 
 

字段的渲染字段应该如何查找?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douba9654 2014-09-13 08:35
    已采纳

    Basically Symfony and Form Framework creates Form object base on Type configuration classes. Form object has createView() method which is passed to view http://api.symfony.com/2.4/Symfony/Component/Form/FormView.html

    In twig you can access embeded FormView object like that:

    {# access embeded form #}
    {{ form_row(form.user.username) }}
    {{ form_row(form.user.email) }}
    {{ form_row(form.user.password) }}
    {# access main form field #}
    {{ form_row(form.terms) }}
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题