Skip to main content
1 of 2
Jafar Pinjar
  • 2k
  • 7
  • 70
  • 144

Add select options dynamically to admin form in magento2

I have created a model.

$fieldset->addField(
        'test1',
        'text',
        array(
            'name' => 'test1',
            'label' => __('Test 1'),
            'title' => __('Test 1'),
            /*'required' => true,*/
        )
    );

  $fieldset->addField(
        "test2",
        "select",
        [
            "label"     =>      __("Test 2"),
            "name"      =>      "test2",
            "values"    =>      [
                ["value" => 1,"label" => __("Option 1")],
                ["value" => 2,"label" => __("Option 2")],
                ["value" => 3,"label" => __("Option 3")],
                ["value" => 4,"label" => __("Option 4")],
                ["value" => 5,"label" => __("Option 5")],
            ]
        ]
    );

This is working well, but i need to add the option values dynamically, that is i need to get the result from one of the custom table and add them as values.

my custom table have the record like below.

  table custom_table;

  id   options

  1   Option 1

  2   Option 2

  3   Option 3

  4   Option 4

  5   Option 5

How to read this table and add the result as value for my admin form field there.

Can anyone look into this and update me please.

Thanks

Jafar Pinjar
  • 2k
  • 7
  • 70
  • 144