简体   繁体   中英

Populate label or attribute of form element in Zend Framework 3

I'm still fairly new to ZF3 so forgive me if I'm asking an obvious question here, but after many searches, looks through the source and head scratching, I can't seem to find an obvious approach to populating label text with data from the entity.

Basically, I have a form collection containing form elements which are each stored with a type (ID), eg "Business phone", "Mobile phone", etc.

Is there a way to populate anything other than a value in a form element?

Edit (more info)

So, there is a PersonForm , with a Person Fieldset which contains a Phone Fieldset collection:

$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 Phone fieldset contains the following elements:

$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
    ]
]);

Sure, adding label information for a formCollection ( Fieldset or Collection elements) is pretty much the same as for input elements ( Element ) ( Element docs ).

Some examples:

Add a Fieldset into a Form ( $this->formCollection($form->get('address')) ): Docs: https://docs.zendframework.com/zend-form/element/collection/

$this->add(
    [
        'type'     => AddressFieldset::class, // Note: FQCN -> provided by FormElementManager
        'name'     => 'address',
        'required' => true,
        'options'  => [
            'use_as_base_fieldset' => false,
            'label'                => _('Address'),
            'label_options'        => [
                // .. add options for the label, see LabelInterface
            ],
            'label_attributes'        => [
                // .. add attributes for the label, see LabelInterface
            ],
        ],
    ]
);

Renders as:

<fieldset>
    <legend>Address</legend>
    <!-- the inputs / labels of `Element` objects -->
</fieldset>

Add a Collection into a Form ( $this->formCollection($form->get('cities')) ): Docs: https://docs.zendframework.com/zend-form/element/collection/

$this->add(
    [
        'name'       => 'cities',
        'type'       => Collection::class,
        'options'    => [
            'label'                  => _('Cities'),
            'should_create_template' => true,
            'allow_add'              => true,
            'allow_remove'           => true,
            'count'                  => 1,
            'target_element'         => $this->getCityFieldset(), // Must be an instantiated Fieldset (so provide via Factory)
            'label_options'        => [
                // .. add options for the label, see LabelInterface
            ],
            'label_attributes'        => [
                // .. add attributes for the label, see LabelInterface
            ],
        ],
        'attributes' => [
            'class'              => 'fieldset-collection',
            'data-fieldset-name' => _('City'),
        ],
    ]
);

Renders as:

<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>

I added in the <!-- comments --> in the HTML output, you can figure those out ;-)


Additionally, if you're using an ORM, Doctrine in this example, then you could do it like this:

$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',
        ],
    ]
);

Doctrine Form Elements docs

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM