【问题标题】:Missing required options in Symfony2Symfony2 中缺少必需的选项
【发布时间】:2015-02-25 08:38:21
【问题描述】:

我正在尝试在 Symfony2 中创建一个嵌入式表单,但我收到了这个错误:

The required options 'em', 'request' are missing. 

我该如何解决这个问题?

编辑: 好的,我有关于表单的新信息。问题似乎是当我尝试在我的初始表单中包含一个子表单时。似乎需要这两个参数,但我不知道如何将它们传递给它。

我的两个实体表单类:

<?php
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
   //.......
}

<?php
     
     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'
                    ) 
                )
            )
        
        ;
    }
  }

还有我在控制器上的功能

<?php

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

还有twig中的表格:

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

【问题讨论】:

  • 请修复您的标记(缩进,删除 sn-p,因为 StackOverflow 无法呈现 Twig 和 PHP)并尝试仅包含相关代码。

标签: forms symfony


【解决方案1】:

首先:包含表单的相关代码 sn-p 以及如何实例化它

第二:很清楚——没有看到代码,只有错误——你需要将两个参数传递给表单实例化过程

你的表单应该是这样的

class YourFormType extends AbstractType
{
  //Some code here, not important for answer
  public function setDefaultOptions(OptionsResolverInterface $resolver)
  {
    //Probably some code here
    $resolver->setRequired(array(
        'em',
        'request',
    ));
  }
}

你的控制器应该是这样的

class YourController extends Controller
{
    //Probably some actions here
    public function fooAction()
    {
        //some actions here
        $your_form = $this->createForm(new YourFormType(), $your_object)); //here you're missing options parameters
    }
}

你的控制器应该是什么样子才能工作

class YourController extends Controller
{
    //Probably some actions here
    public function fooAction()
    {
        //some actions here
        $your_form = $this->createForm(new YourFormType(), $your_object, array(
            'em' => //object, I suppose entity_manager,
            'request' => //I suppose the request object
    }
}

如果您还需要检索实体管理器并请求(但这只是我从您的错误中读取变量名称的猜测)

class YourController extends Controller
{
    //Probably some actions here
    public function fooAction()
    {
        //some actions here
        $em = $this->getDoctrine()->getManager();
        $request = $this->get('request');
        $your_form = $this->createForm(new YourFormType(), $your_object, array(
            'em' => $em,
            'request' => $request
    }
}

【讨论】:

  • 我添加了您建议的这段代码,但它返回一个新错误:选项“em”、“request”不存在。已知选项有:“action”、“allow_extra_fields”、“attr”、“.....
  • @JohnKrit: 很清楚:这是因为您将其传递给“主”形式,但您需要将它们放入转换器中。我建议你看一下第一个sn-p的setDefaultOptions
  • 我尝试在我的 ContactPhonenumbers 表单的 setDefaultOptions 中添加“em”和“request”,但它仍然返回相同的错误。 public function setDefaultOptions(OptionsResolverInterface $resolver) { $em = $this-&gt;getDoctrine()-&gt;getManager(); $request = $this-&gt;get('request'); $resolver-&gt;setDefaults(array( 'data_class' =&gt; 'CnAdmin\AddressBundle\Entity\ContactPhonenumber', 'em' =&gt; $em, 'request' =&gt; $request )); }
猜你喜欢
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 2014-05-31
  • 2013-07-25
  • 2017-03-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多