459

Product support

Get help in the plugin support forum.

Categories

A simple plugin that will generate an HTML select element containing all world countries. Also has a validator that will validate the input.

Installation

Just install the plugin and add the "Country select form field" component to any of your pages where you want the select element to be rendered. You can configure the element's id, name, class, required attributes, as well as the default value (either hard-coded or taken from the user's session).

Important note: To have the component auto-detect the user's languages, you need to install the Rainlab.Translate plugin.

Validation rule

The country can be validated using a validation rule, which validates the country based on the 2-character country code:

$data = Input::all();
$rules = [
    // ... other validation rules
    'country' => 'country',
];
$validation = Validator::make($data, $rules);

Getting localized country name

You can get the full country name from the submitted form in your component like this:

First use the required classes:

use Input;
use Multiwebinc\CountrySelect\Models\CountryList;

Then to get the English country name, you would do:

$country = CountryList::getCountryNameFromCountryCode(Input::get('country'));
// e.g. Dominican Republic

Or if you want the country name in a language other than English, use the two-digit language code (see Internationalization section for a list of language codes):

$country = CountryList::getCountryNameFromCountryCode(Input::get('country'), 'es');
// e.g. RepĂșblica Dominicana

Internationalization

Countries are currently translated into the following languages:

ar - Arabic
cn - Chinese
cs - Czech
da - Danish
de - German
el - Greek
en - English (default)
es - Spanish
et - Estonian
fr - French
hu - Hungarian
it - Italian
ja - Japanese
lt - Lithuanian
nl - Dutch
no - Norwegian
pl - Polish
pt - Portuguese
ro - Romanian
ru - Russian
sk - Slovak
th - Thai
uk - Ukranian
  • Found the plugin useful on 21 May, 2020

    Simple but useful functionality that saves time! Great that there is country validation rule also.

    Would be happy to have a formwidget for backend form to store alpha2 code in the field and to choose from the list of countries full name.

    Did it myself now via form extending in my Plugin's boot(). Example for Lovata.Buddies plugin, User form:

    Users::extendFormFields(function($form, $model, $context) {
    
        if (!$model instanceof User)
            return;
    
        $countries = CountryList::getCountries();
        $options = [];
    
        foreach ($countries as $country) {
            $options[$country['alpha2']] = $country['name'];
        }
    
        $form->addTabfields([
            'property[country]' => [
                'label'     => 'my.pluginname::lang.fields.country',
                'tab'       => 'lovata.toolbox::lang.tab.properties',
                'type'      => 'dropdown',
                'span'      => 'auto',
                'options'   => $options,
            ],
        ]);
    });

1.0.1

First version of countryselect

Mar 18, 2020