簡體   English   中英

使用注釋時,如何更改Zend表單中的Collection Fieldset的計數?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM