我试图在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
//.......
}
&#13;
<?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'
)
)
)
;
}
}
&#13;
我在控制器上的功能
<?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()
)
);
}
&#13;
树枝上的形式:
<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>
&#13;
答案 0 :(得分:0)
首先:包含表单代码的相关代码段以及如何实例化
第二:非常清楚 - 没有看到代码而只看到错误 - 你需要将两个参数传递给表单实例化过程
您的表单应该是
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
}
}