atk14 / country-field
为每种语言本地化的国家选择字段。它旨在用于 ATK14 表单。
v1.0.8
2022-05-20 13:47 UTC
Requires
- php: >=5.4.0
- umpirsky/country-list: ^2.0
Requires (Dev)
- atk14/core: dev-master
- atk14/forms: dev-master
- atk14/tester: *
README
为每种语言本地化的国家选择字段。它旨在用于 ATK14 表单。
它使用https://github.com/umpirsky/country-list作为本地化国家列表。
它还包含Smarty修饰符,可以根据ISO代码显示国家名称。
基本用法
在表单中
<?php
// file: app/forms/users/create_new_form.php
class CreateNewForm extends ApplicationForm {
function set_up(){
$this->add_field("name", new CharField([
"label" => "Your name",
"hint" => "John Doe"
]));
// other fields...
$this->add_field("country_code", new CountryField([
"label" => "Choose your country",
"initial" => "CZ"
]));
}
}
在模板中
{*
* file: app/views/users/detail.tpl
*}
<h1>{t}Your personal data{/t}</h1>
<h3>{t}Address{/t}</h3>
{t}Country:{/t} {$user->getCountryCode()|to_country_name}
特殊情况
可以定义一组有限的国家。
$this->add_field("country_code", new CountryField([
"label" => "Choose your favourite V4 country",
"allowed_countries" => ["CZ","SK","PL","HU"]
]));
也可以定义不允许的国家列表。
$this->add_field("country_code", new CountryField([
"label" => "Choose country",
"disallowed_countries" => ["BZ","IO","CF"]
]));
可以使用选项empty_choice_text设置空选项的标题
$this->add_field("country_code", new CountryField([
"empty_choice_text" => "-- select country --"
]));
可以使用选项include_empty_choice省略空选项
$this->add_field("country_code", new CountryField([
"include_empty_choice" => false
]));
安装
只需使用Composer
cd path/to/your/atk14/project/
composer require atk14/country-field
ln -s ../../vendor/atk14/country-field/src/app/fields/country_field.php app/fields/country_field.php
ln -s ../../vendor/atk14/country-field/src/app/helpers/modifier.to_country_name.php app/helpers/modifier.to_country_name.php
许可证
CountryField是免费软件,根据MIT许可证条款分发https://open-source.org.cn/licenses/mit-license