0

customform.phtml

<div class="field country required">
    <label class="label" for="country"><span><?php /* @escapeNotVerified */
            echo __('Country') ?></span></label>
    <div class="control">
        <input name="country" id="country" title="<?php /* @escapeNotVerified */
        echo __('Country') ?>"
               value="<?php echo $block->escapeHtml($this->helper('Magento\Contact\Helper\Data')
                   ->getPostValue('country')) ?>"
               class="input-text" type="text" data-validate="{required:true}"/>
    </div>
</div>

Country dropdown:

<?php $countryBlock = $block->getLayout()->createBlock('Magento\Directory\Block\Data');?>

<?= $countryBlock->getCountryHtmlSelect($defValue = null, $name = 'country_id', $id = 'country', $title = 'Country');?>

as per: https://magento.stackexchange.com/a/262790

How to put the dropdown in the form and get the selected country as $post variable / value? Thanks

1 Answer 1

0

Nevermind, I got it. In case somebody needs it:

In block

public function __construct(
        \Magento\Directory\Model\ResourceModel\Country\CollectionFactory $countryCollectionFactory)
    {
        $this->countryCollectionFactory = $countryCollectionFactory;

    }

    public function getAvailableCountries()
    {
        $collection = $this->countryCollectionFactory->create();
        $collection->addFieldToSelect('*');

        return $collection;
    }

In template

                <?php $countries = $block->getAvailableCountries(); ?>
                <select name="country" id="country">
                    <option value="">-- Please Select --</option>
                    <?php foreach ($countries as $country): ?>
                        <option value="<?php echo $country->getCountryId(); ?>">
                            <?php echo $country->getName(); ?>
                        </option>
                    <?php endforeach; ?>
                </select>

Hope it helps

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.