1
<form>
    ...
    <fieldset>
        ...
        <field name="select_example" formElement="select">
            <settings>
                <dataType>text</dataType>
                <label translate="true">Select Example</label>
                <dataScope>select_example</dataScope>
            </settings>
            <formElements>
                <select>
                    <settings>
                        <options>
                            <option name="1" xsi:type="array">
                                <item name="value" xsi:type="string">1</item>
                                <item name="label" xsi:type="string">Option #1</item>
                            </option>
                            <option name="2" xsi:type="array">
                                <item name="value" xsi:type="string">2</item>
                                <item name="label" xsi:type="string">Option #2</item>
                            </option>
                            <option name="3" xsi:type="array">
                                <item name="value" xsi:type="string">3</item>
                                <item name="label" xsi:type="string">Option #3</item>
                            </option>
                        </options>
                        <caption translate="true">-- Please Select --</caption>
                    </settings>
                </select>
            </formElements>
        </field>
        ...
    </fieldset>
    ...
</form>

i used this for to make dropdown in my form but how can i make this as required entry in my form thanks

1
  • Where you created this script? Apr 12 at 6:09

2 Answers 2

1
  • Use this elements in your form

    Vendor_name/Module_name/view/adminhtml/ui_component

      <field name="select_example" sortOrder="50" formElement="select">
          <settings>
              <validation>
                  <rule name="required-entry" xsi:type="boolean">true</rule>
              </validation>
              <dataType>text</dataType>
              <label translate="true">Select Example</label>
          </settings>
          <formElements>
              <select>
                  <settings>
                      <options class="Vendor_name\Module_name\Model\Source\options"/>
                  </settings>
              </select>
          </formElements>
      </field>
    
  • Than create source file for options

namespace Vendor_name\Module_name\Model\Source;

use Magento\Framework\Data\OptionSourceInterface;

class options implements OptionSourceInterface {

public function toOptionArray(){

     return [
        ['label' => __('-- Please Select --'), 'value' => ''],
        ['label' => __('Option #1'), 'value' => '1'],
        ['label' => __('Option #2'), 'value' => '2'],
        ['label' => __('Option #3'), 'value' => '3'],
    ];
}

}

I hope this will help you

2
  • thanks @Abdullah it's working fine
    – Mani
    Apr 12 at 7:49
  • Welcome @Mani please vote up Apr 12 at 8:02
0

Use this in your form elements:

<settings>
     <validation>
          <rule name="required-entry" xsi:type="boolean">true</rule>
     </validation>
</settings>

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.