[英]How can I change count of a Collection Fieldset in Zend Form when using Annotations?
From https://docs.zendframework.com/zend-form/quick-start/#using-annotations : 来自https://docs.zendframework.com/zend-form/quick-start/#using-annotations :
Via using Zend\\Form\\Annotation
, I can issue this directive: 通过使用
Zend\\Form\\Annotation
,我可以发出这个指令:
/**
* @ComposedObject({
* "target_object":"Namespace\Model\ComposedCollection",
* "is_collection":"true",
* "options":{"count":2}
* });
*/
private $property;
What this does is create a collection of ComposedCollection
elements, in the above case, of size 2
. 这样做是为了创建一个
ComposedCollection
元素的集合,在上面的例子中,大小为2
。
It's great when I need a static form with 2 elements, but how do I change it when I want to alter it dynamically? 当我需要一个带有2个元素的静态表单时,这很棒,但是当我想要动态改变它时,如何更改它? ie internally I want to be able to populate the form with 4 or 5 sets of data, with the number being unknown ahead of time.
即在内部我希望能够使用4或5组数据填充表单,其中数字提前未知。
Using Annotations is static, and not dynamic. 使用Annotations是静态的,而不是动态的。 Is there any way to do this?
有没有办法做到这一点?
I have tried using 我试过用
$form->get("element_name")->setOption("count", $numericValue);
but it does not work, because I am guessing by the time I am using it, the form has already been built by the Annotation engine ( $annotationBuilder->createForm(MyEntity::class);
但它不起作用,因为我在使用它时猜测,表单已经由Annotation引擎构建(
$annotationBuilder->createForm(MyEntity::class);
Is there a way to rebuild the form? 有没有办法重建表格?
I have tried $form->prepare()
我试过
$form->prepare()
but it only actually removes my Collection elements instead. 但它实际上只删除了我的Collection元素。
I have also tried $form->init()
but it appears to do nothing. 我也试过
$form->init()
但它似乎什么都不做。
I am in the process of rewriting the form NOT using Annotations, but that's not what I am trying to accomplish, since I am effectively losing my Entity by rewriting it in a programmatic way. 我正在重写不使用Annotations的表单,但这不是我想要完成的,因为我通过以编程方式重写它实际上失去了我的实体。
Use Collection::setCount()
method. 使用
Collection::setCount()
方法。
$form->get("element_name")->setCount($numericValue);
Also, as an alternative you can define fieldset programmatically. 此外,作为替代方案,您可以以编程方式定义fieldset。 Place
@Annotation\\Exclude()
on the $property
and create a Fieldset like so: 将
@Annotation\\Exclude()
放在$property
并创建一个Fieldset,如下所示:
$c = new Element\Collection('collection');
$c->setOptions([
'label' => 'Collection',
'count' => 1 ,
'should_create_template' => true,
'allow_add' => true,
'target_element' => [
'type' => ComposedCollectionFieldset::class
]
]);
Where ComposedCollectionFieldset
contains the Elements
that you need. ComposedCollectionFieldset
包含您需要的Elements
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.