0

I have create custom admin grid . In this i have create form.php file.In file it is satic value but i want the value should be comes from Database table (tbl_billing_cycle) and column name(billing_cycle) form.php

`$fieldset->addField(
        'billingcycle',
        'multiselect',
        [
            'label' => __('Select Billing Cycle'),
            'title' => __('Select Billing Cycle'),
            'name' => 'billingcycle[]',
            'required' => true,
            'values' => [
                ["value" => "Once a day","label" => __("Once a day")],
                ["value" => "Once a week","label" => __("Once a week")],
                ["value" => "Once a month","label" => __("Once a month")],
                ["value" => "Once a year","label" => __("Once a year")],
            ],
        ]

    );`

3 Answers 3

0
$fieldset->addField(
        'billingcycle',
        'multiselect',
        [
            'label' => __('Select Billing Cycle'),
            'title' => __('Select Billing Cycle'),
            'name' => 'billingcycle[]',
            'required' => true,
            'values' => $this->YOURFUNCTIONNAME(),
        ]

    );`

Now on the same file, create function like this :

public function YOURFUNCTIONNAME()
{
    // WRITE LOGIC TO GET OPTIONS FROM DATABASE AND RETURN AS ARRAY
}
0

check below code.

add model dependency in your fieldset file to get billing cycle all options array.

Form.php

public function __construct(
        \Custom\Module\Model\Source\BillingCycle $billingCycle,
        array $data = []
    )
    {
        $this->billingCycle = $billingCycle;
        parent::__construct($context, $registry, $formFactory, $data);
    }

    protected function _prepareForm()
    {
        $allBillingCycle = $this->billingCycle->getAllOptions();
        
        $fieldset->addField(
            'type',
            'select',
            [
             'name' => 'type', 
             'label' => __('Warehouse Type'), 
             'values' => $allBillingCycle
            ]
        );
    }

in the model, file add your table model factory method to get the collection like

Model\BillingCycle.php

<?php
namespace Custom\Module\Model\Source;

class BillingCycle extends \Magento\Eav\Model\Entity\Attribute\Source\Table implements \Magento\Framework\Data\OptionSourceInterface
{

    protected $bankCodesFactory;

    public function __construct(
        \Custom\Module\Model\[ModelName]Factory $billingCycleFactory
    ) {
        $this->billingCycleFactory = $billingCycleFactory;
    }

    /**
     * @inheritdoc
     */
    public function getAllOptions($withEmpty = true, $defaultValues = false) {

        $collection = $this->billingCycleFactory->create()->getCollection();

        $options = [];
        if ($withEmpty) {
            $options[] = ['label' => __('Please Select'), 'value' => ''];
        }
        foreach ($collection as $billingData) {
            $options[] = ['label' => $billingData->getBillingCycle(), 'value' => $billingData->getId()];
        }
        return $options;
    }
}
0

But the value is to be store as a entity_id in a Database. But i want BillingCycle value would be stored. then the code look like : 'value' => $billingData->getBillingCycle(); Is it Correct?

5
  • yes it's correct
    – Devidas
    Commented Aug 19, 2021 at 6:40
  • If i want 'please select' option doesn't show in my admin grid form then what should i do?
    – Sudipa
    Commented Aug 19, 2021 at 9:37
  • if ($withEmpty) { $options[] = ['label' => __('Please Select'), 'value' => '']; }
    – Devidas
    Commented Aug 19, 2021 at 10:45
  • remove above code
    – Devidas
    Commented Aug 19, 2021 at 10:45
  • Thank you. can you solve this question ? click on the link magento.stackexchange.com/questions/344854/…
    – Sudipa
    Commented Aug 19, 2021 at 10:53

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.