[英]Symfony Form with Form Collection, cannot pass Options array into sub forms
In the Entity controller, the create and edit use the same formType, this then has a definition for a field which is a relation to a collection of Entities. 在实体控制器中,创建和编辑使用相同的formType,然后为字段定义一个与实体集合有关的字段。 The issue I am having is that I cannot find a way to pass in the $options
array into the form builder which would then be available to the sub entity formType. 我遇到的问题是我找不到将$options
数组传递到表单生成器的方法,该方法之后可用于子实体formType。 I could pass all the values through the constructors of the formTypes but this feels to be a workaround not a solution. 我可以通过formTypes的构造函数传递所有值,但这感觉是一种解决方法,而不是解决方案。
My controller example (state is the additional option i wish to pass through); 我的控制器示例(状态是我希望通过的附加选项);
private function createEditForm(Delivery $entity)
{
$form = $this->createForm(new DeliveryType(), $entity, array(
'state'=>'update', // This is the extra value I wish to pass through.
'action' => $this->generateUrl('delivery_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
and in the form builder class I've included it into the setDefaultOptions()
like so 在表单构建器类中,我将其包含在setDefaultOptions()
如下所示
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\DemoBundle\Entity\Delivery',
'state' => 'create'
));
}
but in this formType class I cannot find a way to pass it into the collection of entities without using the constructor of the collection formType. 但是在这个formType类中,如果不使用集合formType的构造函数,就找不到将其传递到实体集合中的方法。 My main formType class looks like this; 我的主要formType类看起来像这样;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('date', 'date', array(
'widget' => 'single_text',
'datepicker' => true
))
->add('poNumber')
->add('deliveryItems', 'collection', array(
'type' => new DeliveryItemType($id),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
))
;
}
and the sub entity formType looks like this; 子实体formType看起来像这样;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('stock', 'entity', array(
'class' => 'Acme\DemoBundle\Entity\Stock',
'attr' => array(
'class' => 'chosen'
)
))
->add('quantity')
;
}
The reason I am trying to specify the difference between the update and create is so I do not have to duplicate the formType class files with just a single line change to each. 我尝试指定更新和创建之间的区别的原因是,这样我不必重复formType类文件,而只需对每个文件进行一行更改。 Passing the value through the constructors will work but it's not clean or maintainable. 通过构造函数传递值可以使用,但它不是干净的或不可维护的。 Another possible option is doing this through Twig but I feel that manually outputting the form widgets a step backwards. 另一个可能的选择是通过Twig进行此操作,但我认为手动输出表单小部件会向后退一步。
My ideal solution would be to give the sub-entity fields a custom status (disabled) on the edit Controller/page so that the relations cannot be reset once it was created as this would cause problems in my code. 我的理想解决方案是在“控制器/页面”编辑器中为子实体字段提供自定义状态(已禁用),以便一旦创建关系就无法重置该关系,因为这会导致我的代码出现问题。
I've also looked into Form EventListeners but this is post/pre submit and gives access to the data, I could not force the output of a field to be disabled on the edit page only. 我也研究了Form EventListeners,但这是发布/预提交,并提供对数据的访问权限,我无法强制仅在编辑页面上禁用字段的输出。
The issue was that in the DeliveryType class I couldn't work out how to pass options into the sub formType. 问题是在DeliveryType类中,我无法弄清楚如何将选项传递到子formType中。 The information here: http://symfony.com/doc/current/reference/forms/types/collection.html#basic-usage and some help in IRC cleared up what I was missing. 此处的信息: http : //symfony.com/doc/current/reference/forms/types/collection.html#basic-usage和IRC中的一些帮助清除了我所缺少的内容。
My main formType now has an extra line in the entity collection definition; 我的主要formType现在在实体集合定义中增加了一行;
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('date', 'date', array(
'widget' => 'single_text',
'datepicker' => true
))
->add('poNumber')
->add('deliveryItems', 'collection', array(
'type' => new DeliveryItemType($id),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
'options' => array('state' => $options['state']), // THIS LINE!
))
;
}
Which then means I can call the $options
array in the sub formType (ensure both formTypes have the setDefaultOptions
function with the name of the option) 然后,这意味着我可以在子formType中调用$options
数组(确保两个formType都具有带有选项名称的setDefaultOptions
函数)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.