1

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 your ideas please.

Thanks

2 Answers 2

1

This is not really a recommended way but I think you can write a custom Mysql Query (Without Using Model).

You can try like this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance(); 
$resource = $objectManager->get('Magento\Framework\App\ResourceConnection');
$connection = $resource->getConnection();
$tableName = $resource->getTableName('custom_table'); 

//Select Data from custom_table
$sql = "Select * FROM " . $tableName;
$result = $connection->fetchAll($sql);  

And you can loop the result in your select field options like this:

foreach($result as $key => $option){
      $options[] = ["value" => $key,"label" => __($option)],
}

$fieldset->addField(
     "test2",
     "select",
     [
         "label"     =>      __("Test 2"),
         "name"      =>      "test2",
         "values"    =>    [$options]
     ]
 );

Adding here: Fetch database custom table using custom model and Resourcemodel

9
  • facing syntax error,Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ']' Commented May 27, 2019 at 11:45
  • this is not working, i hope there is no any syntax errors Commented May 27, 2019 at 11:54
  • @jafarpinjar try updated answer
    – fmsthird
    Commented May 27, 2019 at 12:01
  • ,Undefined index: value in vendor/magento/framework/Data/Form/Element/Select.php getting below error Commented May 27, 2019 at 12:19
  • oh I cant really check now as Im using mobile only. tomorrow I can check when in office
    – fmsthird
    Commented May 27, 2019 at 13:02
0

This is the correct answer

foreach($result as $key => $option){
      $options[] = ["value" => $key,"label" => __($option)],
}

    $fieldset->addField(
         "test2",
         "select",
         [
             "label"     =>      __("Test 2"),
             "name"      =>      "test2",
             "values"    =>    $options
         ]
     );

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.