![](/img/trans.png)
[英]How can I implement a Zend Form entity with an array of elements, using Annotations?
[英]How can I change count of a Collection Fieldset in Zend Form when using Annotations?
来自https://docs.zendframework.com/zend-form/quick-start/#using-annotations :
通过使用Zend\\Form\\Annotation
,我可以发出这个指令:
/**
* @ComposedObject({
* "target_object":"Namespace\Model\ComposedCollection",
* "is_collection":"true",
* "options":{"count":2}
* });
*/
private $property;
这样做是为了创建一个ComposedCollection
元素的集合,在上面的例子中,大小为2
。
当我需要一个带有2个元素的静态表单时,这很棒,但是当我想要动态改变它时,如何更改它? 即在内部我希望能够使用4或5组数据填充表单,其中数字提前未知。
使用Annotations是静态的,而不是动态的。 有没有办法做到这一点?
我试过用
$form->get("element_name")->setOption("count", $numericValue);
但它不起作用,因为我在使用它时猜测,表单已经由Annotation引擎构建( $annotationBuilder->createForm(MyEntity::class);
有没有办法重建表格?
我试过$form->prepare()
但它实际上只删除了我的Collection元素。
我也试过$form->init()
但它似乎什么都不做。
我正在重写不使用Annotations的表单,但这不是我想要完成的,因为我通过以编程方式重写它实际上失去了我的实体。
使用Collection::setCount()
方法。
$form->get("element_name")->setCount($numericValue);
此外,作为替代方案,您可以以编程方式定义fieldset。 将@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
]
]);
ComposedCollectionFieldset
包含您需要的Elements
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.