2

I'm using magento 2.3.4 and I tried to create a custom form with dropdown options like this:

   <field name="year">
         <argument name="data" xsi:type="array">
                 <item name="options" xsi:type="object">Vendor\Module\Model\Year\Attribute\Source\Years</item>
                 <item name="config" xsi:type="array">
                         <item name="dataType" xsi:type="string">text</item>
                         <item name="label" xsi:type="string" translate="true">Year</item>
                         <item name="formElement" xsi:type="string">select</item>
                         <item name="source" xsi:type="string">Year</item>
                         <item name="dataScope" xsi:type="string">year</item>
                         <item name="default" xsi:type="string">0</item>
                         <item name="component" xsi:type="string">Magento_Ui/js/form/element/select</item>
                         <item name="validation" xsi:type="array">
                                 <item name="required-entry" xsi:type="boolean">true</item>
                         </item>
                 </item>
         </argument>
  </field>

app/code/Vendor/Module/Model/Year/Attribute/Source/Years.php

namespace Vendor\Module\Model\Year\Attribute\Source;

class Years implements \Magento\Framework\Option\ArrayInterface
{

  public function toOptionArray() {
    $earliest_year = 2012;
    $options = array();
    foreach (range(date('Y'), $earliest_year) as $x) {
      $options[] = ['value' => $x, 'label' => $x];
    }
    return $options;
  }
}

but the dropdown options always empty, when i print the $options using print_r($options);exit; it shows:

Array
(
    [0] => Array
        (
            [label] => 2020
            [value] => 2020
        )

    [1] => Array
        (
            [label] => 2019
            [value] => 2019
        )

    [2] => Array
        (
            [label] => 2018
            [value] => 2018
        )

    [3] => Array
        (
            [label] => 2017
            [value] => 2017
        )

    [4] => Array
        (
            [label] => 2016
            [value] => 2016
        )

    [5] => Array
        (
            [label] => 2015
            [value] => 2015
        )

    [6] => Array
        (
            [label] => 2014
            [value] => 2014
        )

    [7] => Array
        (
            [label] => 2013
            [value] => 2013
        )

    [8] => Array
        (
            [label] => 2012
            [value] => 2012
        )

)

1 Answer 1

1

Please replace the option class with the below code. Please implement \Magento\Framework\Data\OptionSourceInterface instead of \Magento\Framework\Option\ArrayInterface

class Years implements \Magento\Framework\Data\OptionSourceInterface
{
    /**
     * @var array
     */
    public $options = null;

    public function toOptionArray() {
        $earliest_year = 2012;
        if ($this->options === null) {
            foreach (range(date('Y'), $earliest_year) as $x) {
                $this->options[] = [
                    "label" => __($x),
                    "value" => $x
                ];
            }
        }
        return $this->options;
    }
}

Thanks

1
  • thanks it works, whats wrong with my code?
    – jojo
    Commented May 8, 2020 at 6:22

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.