简体   繁体   English

使用注释时,如何更改Zend表单中的Collection Fieldset的计数?

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

相关问题 如何使用Annotations实现带有元素数组的Zend Form实体? - How can I implement a Zend Form entity with an array of elements, using Annotations? 如何将参数传递给Zend Form Collection实例以及如何在ZF2中设置自定义Fieldset标签? - How to pass an argument to a Zend Form Collection instance & How to set custom Fieldset labels in ZF2? 如何在zf2中使用表单工厂创建字段集集合 - How to create fieldset collection using form factory in zf2 删除Zend \\ Form \\ Collection的所有元素(带有DoctrineModule ObjectSelect的字段集) - Deleting all elements of a Zend\Form\Collection (Fieldset with a DoctrineModule ObjectSelect) 我如何在zend Studio中使用zend表单隐藏文本 - How can I hide the text Using zend form in zend studio Zend Framework 2-如何使用注释为实体设置表单验证规则? - Zend Framework 2 - How to set form validation rules for an entity using annotations? Zend 2 Form Fieldset可选验证 - Zend 2 Form Fieldset optional validation 我如何使用Zend Form处理无限数量的元素 - How can I process an unlimited amount of elements using Zend Form 如何使用注释首先在Doctrine 2集合上订购NULL值? - How can I order NULL values first on a Doctrine 2 collection using annotations? Zend表单和注释验证 - Zend form and annotations validation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM