6

As the title says,

Looking at Magento\Framework\Data\Form\Element\Select looks like it does but i cant figure out the array? that i have to pass to my addField(id, type, [name, label, options]);

1 Answer 1

10

Try this solution:

$fieldset->addField(
    'test',
    'select',
    [
        'label' => __('Test'),
        'name' => 'test',
        'required' => true,
        'values' => [
            [
                'label' => 'optgroup 1',
                'value' => [
                    [
                        'label' => 'in opt group 1-1',
                        'value' => 'value 1-1'
                    ],
                    [
                        'label' => 'in opt group 1-2',
                        'value' => 'value 1-2'
                    ],
                    [
                        'label' => 'in opt group 1-3',
                        'value' => 'value 1-3'
                    ],
                ]
            ],
            [
                'label' => 'optgroup 2',
                'value' => [
                    [
                        'label' => 'in opt group 2-1',
                        'value' => 'value 2-1'
                    ],
                    [
                        'label' => 'in opt group 2-2',
                        'value' => 'value 2-2'
                    ],
                    [
                        'label' => 'in opt group 2-3',
                        'value' => 'value 2-3'
                    ],
                ]
            ],
            [
                'label' => 'optgroup 3',
                'value' => [
                    [
                        'label' => 'in opt group 3-1',
                        'value' => 'value 3-1'
                    ],
                    [
                        'label' => 'in opt group 3-2',
                        'value' => 'value 3-2'
                    ],
                    [
                        'label' => 'in opt group 3-3',
                        'value' => 'value 3-3'
                    ],
                ]
            ]
        ]
    ]
);

The result looks as follows:

result

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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