0

I want to change City input to Select and populate its value or Alternative hide city input box and add Custom select and fill that option in City field help me if anyone know.

0

Inject dependency for country and region from your class construct

public function __construct(
        .........
        \Magento\Directory\Model\Config\Source\Country $_country,
        \Magento\Directory\Model\RegionFactory $_regionFactory,
        ..........
    ) {
        ...........
        $this->_country = $_country;
        $this->_regionFactory = $_regionFactory;
        ............
    }

Use the below code to get region collection in your form

$countries = $this->_country->toOptionArray();

$regionCollection = $this->_regionFactory->create()->getCollection()->addCountryFilter($model->getCountryId());
        $regions = $regionCollection->toOptionArray();

Create two field for region ( select & input ) using below code and add it to your form

$fieldset->addField(
            'country_id',
            'select',
            ['name' => 'country_id', 'label' => __('Country'), 'required' => true, 'values' => $countries]
        );


$fieldset->addField(
            'region_id',
            'select',
            ['name' => 'region_id', 'label' => __('Region'), 'values' => $regions]
        );

        $fieldset->addField(
            'region',
            'text',
            ['name' => 'region', 'label' => __('Region')]
        );

And within your form add a js file to perform show/hide functionality

$this->setChild(
            'form_after',
            $this->getLayout()->createBlock('Magento\Framework\View\Element\Template')->setTemplate('Namespace_Modulename::js.phtml')
        );

Create js.phtml file in your templates directory in view folder like ( Module_root/view/frontend/templates/js.phtml )

Add below js code to js.phtml and save

<script>
    require([
        "jquery",
        "mage/adminhtml/form"
    ], function ($) {

        if(!$('[name=region_id]')[0].value)
        {
            $('[name=region_id]').parent().parent().hide();
            $('[name=region]').parent().parent().show();
        }
        else
        {
            $('[name=region]').parent().parent().hide();
            $('[name=region_id]').parent().parent().show();
        }
        var updater = new RegionUpdater(
            $('[name=country_id]')[0], 
            $('[name=region]')[0], 
            $('[name=region_id]')[0],
            <?php echo $this->helper('Magento\Directory\Helper\Data')->getRegionJson() ?>,
            'hide'
        );
        updater.disableRegionValidation();
        window.updater = updater;

        $(document).on('change','[name=country_id]',function(){
            if($('[name=region_id]').is(':disabled')){
                $('[name=region_id]').parent().parent().hide();
                $('[name=region]').parent().parent().show();
            }
            else
            {
                $('[name=region]').parent().parent().hide();
                $('[name=region_id]').parent().parent().show();
            }
        });
    });
</script>

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.