Einius
Einius

Reputation: 1382

Symfony2 embedded forms rendering in a twig template

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?

Upvotes: 0

Views: 3642

Answers (1)

l3l0
l3l0

Reputation: 3393

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

Upvotes: 1

Related Questions