Php 在Zend Framework 3中填充表单元素的标签或属性

Php 在Zend Framework 3中填充表单元素的标签或属性,php,zend-framework,zend-form,zend-framework3,Php,Zend Framework,Zend Form,Zend Framework3,我对ZF3还是相当陌生,所以如果我在这里问了一个明显的问题,请原谅,但是经过多次搜索、查看源代码和从头开始,我似乎找不到一种明显的方法来用实体中的数据填充标签文本 基本上,我有一个包含表单元素的表单集合,每个表单元素都存储有一个类型(ID),例如“Business phone”、“Mobile phone”等 除了表单元素中的值之外,还有其他填充方法吗 编辑(更多信息) 因此,有一个PersonForm,其中有一个Person字段集,其中包含一个Phone字段集集合: $phoneFieldse

我对ZF3还是相当陌生,所以如果我在这里问了一个明显的问题,请原谅,但是经过多次搜索、查看源代码和从头开始,我似乎找不到一种明显的方法来用实体中的数据填充标签文本

基本上,我有一个包含表单元素的表单集合,每个表单元素都存储有一个类型(ID),例如“Business phone”、“Mobile phone”等

除了表单元素中的值之外,还有其他填充方法吗

编辑(更多信息)

因此,有一个
PersonForm
,其中有一个
Person
字段集,其中包含一个
Phone
字段集集合:

$phoneFieldset = new PhoneFieldset($objectManager);

$this->add([
    "type" => Element\Collection::class,
    "name" => "Phones",
    "options" => [
        "count" => 0,
        "should_create_template" => true,
        "allow_add" => true,
        "allow_remove" => true,
        "target_element" => $phoneFieldset
    ],
]);
电话
字段集包含以下元素:

$this->add([
    "name" => "Type_ID",
    "type" => "hidden",
]);

$this->add([
    "name" => "Number",
    "type" => "text",
    "attributes" => [
        "placeholder" => "Enter phone number",
    ],
    "options" => [
        "label" => "Email" // Ideally this would be $entity->getTypeName() for example, which would return the name based on the Type_ID mapped against a constant
    ]
]);

当然,为
表单集合
字段集
集合
元素)添加标签信息与为输入元素(
元素
)添加标签信息几乎相同()

一些例子:

字段集添加到
表单中(
$this->formCollection($Form->get('address'))
): 文件:

呈现为:

<fieldset>
    <legend>Address</legend>
    <!-- the inputs / labels of `Element` objects -->
</fieldset>
<fieldset class="fieldset-collection" data-fieldset-name="City">
    <legend>Cities</legend>
    <fieldset><!-- add a <legend> using JS here, with the data-fieldset-name of the parent -->
        <!-- the inputs / labels of `Element` objects -->
    </fieldset>
    <span data-template="<!-- contains entire template so you can use JS to create 'add' and 'remove' buttons for additional CityFieldset <fieldset> elements -->"></span>
</fieldset>
呈现为:

<fieldset>
    <legend>Address</legend>
    <!-- the inputs / labels of `Element` objects -->
</fieldset>
<fieldset class="fieldset-collection" data-fieldset-name="City">
    <legend>Cities</legend>
    <fieldset><!-- add a <legend> using JS here, with the data-fieldset-name of the parent -->
        <!-- the inputs / labels of `Element` objects -->
    </fieldset>
    <span data-template="<!-- contains entire template so you can use JS to create 'add' and 'remove' buttons for additional CityFieldset <fieldset> elements -->"></span>
</fieldset>

在我的repo中,在这个文件中添加了
集合的示例:这里是工厂:好的,终于明白你特别想要什么了;-)我以为您想要集合中字段集的标签。但是,实际上您所追求的是:基于实体类型的标签。你遇到的情况是,你需要区分不同类型的电话(例如,移动电话、陆地电话、卫星电话、voip电话-对吗?)。因此,您需要一个
抽象电话
实体,它需要基本信息(国家代码、网络号码、完整号码),然后是一些您需要的鉴别器实体:PhoneMobile、PhoneSatellite。你必须把它们作为自己的实体包括进来,-续。续。-并将其自己的字段集(可能是集合?)放入基本表单中。可以在字段集上添加“必需”标志(参见前面链接的my repo),并允许填写(或不填写)和验证(填写时)这些类型。最棘手的一点是:您必须根据需要的类型创建一个实体。解决方法是创建一个电话实体(单数),该实体包含:国家/地区网络号、电话号码、分机号码等等,但也包含:type。“Type”将由一个常量填写(您可以选择是
int
键还是
enum
),并在(必需的)下拉列表中提供它。顺便说一下:这里有一些让用户输入电话号码的创新方法:->享受
$this->add(
    [
        'name'       => 'roles',
        'required'   => true,
        'type'       => ObjectMultiCheckbox::class,
        'options'    => [
            'object_manager'     => $this->getObjectManager(),
            'target_class'       => Role::class,
            'property'           => 'id',
            'label'              => _('Roles'),
            'label_generator'    => function ($targetEntity) {
                /** @var Role $targetEntity */
                return $targetEntity->getName();
            },
            'label_options'      => [
                'label_position' => FormRow::LABEL_APPEND,
            ],
            'use_hidden_element' => true,
            'checked_value'      => 1,
            'unchecked_value'    => 0,
        ],
        'attributes' => [
            'class'                => 'form-check-input',
        ],
    ]
);