在树枝模板中Symfony2嵌入式表单呈现 [英] Symfony2 embedded forms rendering in a twig template

查看:49
本文介绍了在树枝模板中Symfony2嵌入式表单呈现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的表格:

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';
    }
}

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?

推荐答案

基本上,Symfony和Form Framework在Type配置类的基础上创建Form对象.表单对象具有createView()方法,该方法传递给视图 http://api .symfony.com/2.4/Symfony/Component/Form/FormView.html

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

在树枝中,您可以像这样访问嵌入的FormView对象:

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) }}

这篇关于在树枝模板中Symfony2嵌入式表单呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆