在symfony2中以嵌入形式显示输入

时间:2015-02-25 10:08:50

标签: php forms symfony

我试图在symfony2中创建一个嵌入式表单,该表单将包含一个Contact表单,该表单将嵌入一个ContactPhonenumber表单以添加电话号码。

我已经按照Symfony2文档中有关嵌入表单的说明进行操作,但我找不到至少显示phonenumbers输入字段的方法。如果我没有错误地做,我只返回嵌入式语音形式的标签。 我怎样才能在那里显示输入? 我的代码是:

我的两个实体表单类:

class ContactType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder                        
            ->add('contactType')

            ->add('title')

            ->add('name')

            ->add('givenName')

            ->add('middleName')

            /*->add(
                'contactPhonenumbers', 
                'collection', 
                array( 'type' => new ContactPhonenumberType($options) ) 
                )
          */        

            ->add('contactPhonenumbers', new ContactPhonenumberType($options) )
        ;
    }

//.......
//More functions
//.......
}

class ContactPhonenumberType extends AbstractType
{

    public function __construct(array $options=null) {
        $this->options = $options;
    }

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {    
        $builder                                    
            ->add('position')

            ->add('number')

            ->add( 
                $builder->create(
                    'contact', 
                    'searchable',
                    array( 
                        'data_class'    => '....Bundle\Entity\Contact',
                        'class'         => '....Bundle:Contact',
                        'dataroute'     => '...._contact_searchable',
                        'classlistview' => new ....Bundle\Component\Listview\ContactList
                    )
                )->addViewTransformer( 
                    new NameToIdTransformer( 
                        $options[ 'em' ], 
                        $options[ 'request' ],
                        $this, 
                        array(),
                        '....Bundle:Contact'
                    ) 
                )
            )

            ->add( 
                $builder->create(
                    'phonenumberType', 
                    'searchable',
                    array( 
                        'data_class'    => '....Bundle\Entity\PhonenumberType',
                        'class'         => '.....Bundle:PhonenumberType',
                        'dataroute'     => '...._phonenumbertype_searchable',
                        'classlistview' => new ....\Component\Listview\PhonenumberTypeList
                    )
                )->addViewTransformer( 
                    new NameToIdTransformer( 
                        $options[ 'em' ], 
                        $options[ 'request' ],
                        $this, 
                        array(),
                        '...Bundle:PhonenumberType'
                    ) 
                )
            )
        ;
    }
  }

我在控制器上的功能

    public function newAction( Request $request )
    {
        $em = $this->getDoctrine()->getManager();
        $entity = new Contact();


//        $number = new ArrayCollection();
//        $number[] = new ContactPhonenumber();
//        $entity->setContactPhonenumbers( $number );

        $form   = $this->createCreateForm( $entity );

        return $this->render(
            '...Bundle:Contact:new.html.twig',
            array(
                'entity' => $entity,
                'form'   => $form->createView()
            )  
        );
    }

树枝上的形式:

<div class="widget-body">                               
{{ form_start( form, { 'attr': { 'class': 'form-horizontal', 'role': 'form', 'novalidate': 'novalidate' } } ) }}
{{ form_errors( form ) }}                                                                                                                        
{{ form_row( form.contactType ) }}

{{ form_row( form.title ) }}

{{ form_row( form.name ) }}

{{ form_row( form.givenName ) }}

{{ form_row( form.middleName ) }}

------>
{{ form_row(form.contactPhonenumbers) }}
<--------
{{ form_row( form.salutation ) }}

{{ form_row( form.address ) }}

{{ form_row( form.superiorContact ) }}
<div class="form-group">
    <hr>
</div>
<div class="form-group">
    <div class="col-sm-2"></div>
    <div class="col-sm-10">
        {{ form_widget( form.submit ) }}
        {{ form_widget( form.cancel ) }}
    </div>
</div>
{{ form_row( form._token ) }}
{{ form_end( form, { 'render_rest': false } ) }}

0 个答案:

没有答案