5

I am trying to add radio option but showing single radio without any label

$fieldset->addField(
            'test',
            'radio',
            [
                'label' => __('test'),
                'title' => __('test'),
                'name' => 'test',
                'required' => true,
                'values' => array(
                            array('value'=>'1','label'=>'Radio1'),
                            array('value'=>'2','label'=>'Radio2'),
                            array('value'=>'3','label'=>'Radio3'),
                       ),
                'disabled' => $isDisabled
            ]
        );

3 Answers 3

6

change type from radio to radios

$fieldset->addField(
            'test',
            'radios',
            [
                'label' => __('test'),
                'title' => __('test'),
                'name' => 'test',
                'required' => true,
                'values' => array(
                            array('value'=>'1','label'=>'Radio1'),
                            array('value'=>'2','label'=>'Radio2'),
                            array('value'=>'3','label'=>'Radio3'),
                       ),
                'disabled' => $isDisabled
            ]
        );

for more types you can look in to namespace Magento\Framework\Data\Form\Element

1
  • How to make first option as selected? Commented May 27, 2019 at 10:54
2

Use below code:

$fieldset->addField(
            'test',
            'radios',
            [
                'name' => 'test',
                'label' => __('Radio'),
                'title' => __('Radio'),
                'required' => true,
                'values' => [
                                ['value' =>1, 'label' => __('Yes')],
                                ['value' => 0, 'label' => __('No')]
                            ],
                'disabled' => $isElementDisabled
            ]
        );

enter image description here

10
  • could you guide me to get all radios in single row as all my radios are taking 1 row each Commented May 4, 2016 at 11:09
  • Use above code which i have added in answer Commented May 4, 2016 at 11:10
  • It will display only two option in one row if options are more than 2 it will not display in single row Commented May 4, 2016 at 11:15
  • @PrashantValanda you code is not matching with your output image Commented May 4, 2016 at 11:16
  • It is same as image only label is changed because while i capturing snap at that time label is other. :) Commented May 4, 2016 at 11:16
2

It seems like the way Magento 2 handles radio require multiple radio fields with the same name.

There's a good example in app/code/Magento/ConfigurableProduct/Block/Adminhtml/Product/Edit/AttributeSet/Form.php

In your case the right code would be:

    $fieldset->addField(
        'Radio1',
        'radio',
        [
            'name' => 'test',
            'value' => '1'
        ]
    );
    $fieldset->addField(
        'Radio2',
        'radio',
        [
            'name' => 'test',
            'value' => '2'
        ]
    );
    $fieldset->addField(
        'Radio3',
        'radio',
        [
            'name' => 'test',
            'value' => '3'
        ]
    );

On top of that it seems like app/code/Magento/Catalog/Block/Adminhtml/Product/Helper/Form/Weight.php uses a slightly different method in the below code but as it is a custom element I'm not sure if that will fit your needs:

    $this->weightSwitcher = $factoryElement->create('radios');
    $this->weightSwitcher->setValue(
        WeightResolver::HAS_WEIGHT
    )->setValues(
        [
            ['value' => WeightResolver::HAS_WEIGHT, 'label' => __('Yes')],
            ['value' => WeightResolver::HAS_NO_WEIGHT, 'label' => __('No')]
        ]
    )->setId(
        'weight-switcher'
    )->setName(
        'product_has_weight'
    )->setLabel(
        __('Does this have a weight?')
    );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.