Laravel 的电话号码功能
为 Laravel 添加电话号码功能,基于 PHP 移植版 的 Google libphonenumber。
在演示中查看此包的行为。
运行以下命令以安装该包的最新适用版本:
composer require propaganistas/laravel-phone服务提供者会被 Laravel 自动发现。
在你的语言目录中,在每个 validation.php 语言文件中添加一条额外的翻译:
'phone' => 'The :attribute field must be a valid number.',在你的验证规则数组中使用 phone 关键字,或者使用 Propaganistas\LaravelPhone\Rules\Phone 规则类来以表达性的方式定义规则。
为了对允许的来源国家施加限制,你可以明确指定允许的国家代码。
'my_input' => 'phone:US,BE',
// 'my_input' => (new Phone)->country(['US', 'BE'])或者,为了让事情更具动态性,你也可以匹配另一个包含国家代码的数据字段。例如,要求电话号码与提供的居住国家相匹配。
确保国家字段的名称与电话字段相同,但附加 _country 后缀以便自动发现,
或者将你的自定义国家字段名称作为参数提供给验证器:
'my_input' => 'phone',
// 'my_input' => (new Phone)
'my_input_country' => 'required_with:my_input','my_input' => 'phone:custom_country_field',
// 'my_input' => (new Phone)->countryField('custom_country_field')
'custom_country_field' => 'required_with:my_input',注意:国家代码应
暂无开放 Issues,或尚未同步最近议题。