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?

  • 写回答

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) }}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?