├── CHANGELOG.md ├── resources └── lang │ ├── en │ └── validation.php │ └── vi │ └── validation.php ├── src ├── Rules │ ├── IdVN.php │ ├── MobileVN.php │ ├── LandLineVN.php │ ├── CallableRule.php │ └── IpVN.php └── ServiceProvider.php ├── LICENSE.md ├── composer.json └── README.md /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | Tất cả lịch sử tiến trình phát triển thư viện 4 | 5 | # 1.0.5 6 | 7 | - Hổ trợ Laravel 6 & 7. 8 | 9 | # 1.0.4 10 | 11 | - Sửa lỗi DI khi khởi tạo ip rules. 12 | 13 | # 1.0.3 14 | 15 | - Sửa lỗi lazy load service provider đè lên thư viện. 16 | 17 | # 1.0.2 18 | 19 | - Cải thiện performance thông qua lazy load service provider. 20 | 21 | # 1.0.1 22 | 23 | - Hổ trợ đa ngôn ngữ mặc định. 24 | -------------------------------------------------------------------------------- /resources/lang/en/validation.php: -------------------------------------------------------------------------------- 1 | ':attribute must be an id number of Vietnam.', 11 | 'mobile' => ':attribute must be a mobile phone number of Vietnam.', 12 | 'land_line' => ':attribute must be a land line phone number of Vietnam.', 13 | 'ip' => [ 14 | 'v4' => ':attribute must be Vietnam ipv4.', 15 | 'v6' => ':attribute must be Vietnam ipv6.', 16 | 'default' => ':attribute must be Vietnam ip.', 17 | ], 18 | ]; 19 | -------------------------------------------------------------------------------- /resources/lang/vi/validation.php: -------------------------------------------------------------------------------- 1 | ':attribute phải là số chứng minh thư hoặc thẻ căn cước tại Việt Nam.', 11 | 'mobile' => ':attribute phải là số di động tại Việt Nam.', 12 | 'land_line' => ':attribute phải là số điện thoại bàn tại Việt Nam.', 13 | 'ip' => [ 14 | 'v4' => ':attribute phải là ipv4 tại Việt Nam.', 15 | 'v6' => ':attribute phải là ipv6 tại Việt Nam.', 16 | 'default' => ':attribute phải là ip Việt Nam.', 17 | ], 18 | ]; 19 | -------------------------------------------------------------------------------- /src/Rules/IdVN.php: -------------------------------------------------------------------------------- 1 | 15 | * @since 1.0.0 16 | */ 17 | class IdVN extends CallableRule 18 | { 19 | /** 20 | * {@inheritdoc} 21 | */ 22 | public function passes($attribute, $value): bool 23 | { 24 | return Validator::idVN()->validate($value); 25 | } 26 | 27 | /** 28 | * {@inheritdoc} 29 | */ 30 | public function message(): string 31 | { 32 | return __('phpVietValidation::validation.id'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Rules/MobileVN.php: -------------------------------------------------------------------------------- 1 | 15 | * @since 1.0.0 16 | */ 17 | class MobileVN extends CallableRule 18 | { 19 | /** 20 | * {@inheritdoc} 21 | */ 22 | public function passes($attribute, $value): bool 23 | { 24 | return Validator::mobileVN()->validate($value); 25 | } 26 | 27 | /** 28 | * {@inheritdoc} 29 | */ 30 | public function message(): string 31 | { 32 | return __('phpVietValidation::validation.mobile'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Rules/LandLineVN.php: -------------------------------------------------------------------------------- 1 | 15 | * @since 1.0.0 16 | */ 17 | class LandLineVN extends CallableRule 18 | { 19 | /** 20 | * {@inheritdoc} 21 | */ 22 | public function passes($attribute, $value): bool 23 | { 24 | return Validator::landLineVN()->validate($value); 25 | } 26 | 27 | /** 28 | * {@inheritdoc} 29 | */ 30 | public function message(): string 31 | { 32 | return __('phpVietValidation::validation.land_line'); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/Rules/CallableRule.php: -------------------------------------------------------------------------------- 1 | 15 | * @since 1.0.0 16 | */ 17 | abstract class CallableRule implements Rule 18 | { 19 | /** 20 | * Hổ trợ sử dụng rule dưới dạng extension. 21 | * 22 | * @param $attribute 23 | * @param $value 24 | * @param $parameters 25 | * @param $validator 26 | * @return bool 27 | */ 28 | public function __invoke($attribute, $value, $parameters, $validator) 29 | { 30 | return $this->passes($attribute, $value); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) PHP Viet 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "phpviet/laravel-validation", 3 | "description": "Laravel validation hổ trợ kiểm tra dữ liệu đặc thù trong nước", 4 | "keywords": [ 5 | "phpviet", 6 | "laravel-validation" 7 | ], 8 | "homepage": "https://github.com/phpviet/laravel-validation", 9 | "license": "MIT", 10 | "authors": [ 11 | { 12 | "name": "Vuong Xuong Minh", 13 | "email": "vuongxuongminh@gmail.com" 14 | } 15 | ], 16 | "require": { 17 | "php": "^7.1", 18 | "phpviet/validation": "^1.0", 19 | "illuminate/support": "^5.7 || ^6.0 || ^7.0" 20 | }, 21 | "require-dev": { 22 | "orchestra/testbench": "^3.7 || ^4.0 || ^5.0" 23 | }, 24 | "autoload": { 25 | "psr-4": { 26 | "PHPViet\\Laravel\\Validation\\": "src" 27 | } 28 | }, 29 | "autoload-dev": { 30 | "psr-4": { 31 | "PHPViet\\Laravel\\Validation\\Tests\\": "tests" 32 | } 33 | }, 34 | "config": { 35 | "sort-packages": true 36 | }, 37 | "extra": { 38 | "branch-alias": { 39 | "dev-master": "1.0-dev" 40 | }, 41 | "laravel": { 42 | "providers": [ 43 | "PHPViet\\Laravel\\Validation\\ServiceProvider" 44 | ] 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/Rules/IpVN.php: -------------------------------------------------------------------------------- 1 | 16 | * @since 1.0.0 17 | */ 18 | class IpVN extends CallableRule 19 | { 20 | const IPV4 = BaseIpVN::IPV4; 21 | 22 | const IPV6 = BaseIpVN::IPV6; 23 | 24 | /** 25 | * @var int|null 26 | */ 27 | protected $version; 28 | 29 | /** 30 | * IpVN constructor. 31 | * 32 | * @param int|null $version 33 | */ 34 | public function __construct(?int $version = null) 35 | { 36 | $this->version = $version; 37 | } 38 | 39 | /** 40 | * {@inheritdoc} 41 | */ 42 | public function passes($attribute, $value): bool 43 | { 44 | return Validator::ipVN($this->version)->validate($value); 45 | } 46 | 47 | /** 48 | * {@inheritdoc} 49 | */ 50 | public function message(): string 51 | { 52 | switch ($this->version) { 53 | case self::IPV4: 54 | 55 | return __('phpVietValidation::validation.ip.v4'); 56 | case self::IPV6: 57 | 58 | return __('phpVietValidation::validation.ip.v6'); 59 | default: 60 | 61 | return __('phpVietValidation::validation.ip.default'); 62 | 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/ServiceProvider.php: -------------------------------------------------------------------------------- 1 | 19 | * @since 1.0.0 20 | */ 21 | class ServiceProvider extends BaseServiceProvider 22 | { 23 | public function boot(): void 24 | { 25 | $this->loadTrans(); 26 | $this->loadExt(); 27 | } 28 | 29 | protected function loadTrans(): void 30 | { 31 | $this->publishes([ 32 | __DIR__.'/../resources/lang' => resource_path('lang/vendor/phpVietValidation'), 33 | ]); 34 | $this->loadTranslationsFrom(__DIR__.'/../resources/lang/', 'phpVietValidation'); 35 | } 36 | 37 | protected function loadExt(): void 38 | { 39 | if (isset($this->app['validator'])) { 40 | foreach ($this->getCallableRules() as $name => $rule) { 41 | $this->app['validator']->extend($name, $rule, $rule->message()); 42 | } 43 | } 44 | } 45 | 46 | protected function getCallableRules(): array 47 | { 48 | return [ 49 | 'land_line_vn' => $this->app->make(LandLineVN::class), 50 | 'mobile_vn' => $this->app->make(MobileVN::class), 51 | 'id_vn' => $this->app->make(IdVN::class), 52 | 'ip_vn' => $this->app->make(IpVN::class), 53 | 'ipv4_vn' => $this->app->make(IpVN::class, ['version' => IpVN::IPV4]), 54 | 'ipv6_vn' => $this->app->make(IpVN::class, ['version' => IpVN::IPV6]), 55 | ]; 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

2 | 3 | 4 | 5 |

Laravel Validation

6 |
7 |

8 | Latest version 9 | Build status 10 | Quantity score 11 | StyleCI 12 | Total download 13 | License 14 |

15 |

16 | 17 | ## Thông tin 18 | 19 | Laravel validation hổ trợ kiểm tra các kiểu dữ liệu đặc thù trong nước ta. 20 | 21 | ## Cài đặt 22 | 23 | Cài đặt Laravel Validation thông qua [Composer](https://getcomposer.org): 24 | 25 | ```bash 26 | composer require phpviet/laravel-validation 27 | ``` 28 | 29 | ## Cách sử dụng 30 | 31 | ### Các kiểu dữ liệu được hổ trợ kiểm tra hiện tại 32 | 33 | 34 | - [`Số điện thoại di động`](#Số-điện-thoại-di-động) 35 | - [`Số điện thoại bàn`](#Số-điện-thoại-bàn) 36 | - [`Thẻ căn cước / chứng minh thư`](#Thẻ-căn-cước-/-chứng-minh-thư) 37 | - [`Địa chỉ IP`](#Địa-chỉ-IP) 38 | 39 | ### Số điện thoại di động 40 | 41 | + Sử dụng tại `request`: 42 | 43 | ```php 44 | $request->validate([ 45 | 'mobile_number' => 'required|mobile_vn' 46 | ]); 47 | ``` 48 | 49 | + Sử dụng trong `FormRequest`: 50 | 51 | ```php 52 | public function rules() 53 | { 54 | return [ 55 | 'mobile_number' => 'required|mobile_vn', 56 | ]; 57 | } 58 | ``` 59 | 60 | + Sử dụng dưới dạng `Rule`: 61 | 62 | ```php 63 | public function rules() 64 | { 65 | return [ 66 | 'mobile_number' => ['required', new \PHPViet\Laravel\Validation\Rules\MobileVN()] 67 | ]; 68 | } 69 | ``` 70 | 71 | ### Số điện thoại bàn 72 | 73 | + Sử dụng tại `request`: 74 | 75 | ```php 76 | $request->validate([ 77 | 'land_line_number' => 'required|land_line_vn' 78 | ]); 79 | ``` 80 | 81 | + Sử dụng trong `FormRequest`: 82 | 83 | ```php 84 | public function rules() 85 | { 86 | return [ 87 | 'land_line_number' => 'required|land_line_vn', 88 | ]; 89 | } 90 | ``` 91 | 92 | + Sử dụng dưới dạng `Rule`: 93 | 94 | ```php 95 | public function rules() 96 | { 97 | return [ 98 | 'land_line_number' => ['required', new \PHPViet\Laravel\Validation\Rules\LandLineVN()] 99 | ]; 100 | } 101 | ``` 102 | 103 | ### Thẻ căn cước / chứng minh thư 104 | 105 | + Sử dụng tại `request`: 106 | 107 | ```php 108 | $request->validate([ 109 | 'id_number' => 'required|id_vn' 110 | ]); 111 | ``` 112 | 113 | + Sử dụng trong `FormRequest`: 114 | 115 | ```php 116 | public function rules() 117 | { 118 | return [ 119 | 'id_number' => 'required|id_vn', 120 | ]; 121 | } 122 | ``` 123 | 124 | + Sử dụng dưới dạng `Rule`: 125 | 126 | ```php 127 | public function rules() 128 | { 129 | return [ 130 | 'id_number' => ['required', new \PHPViet\Laravel\Validation\Rules\IdVN()] 131 | ]; 132 | } 133 | ``` 134 | 135 | ### Địa chỉ IP 136 | 137 | + Sử dụng tại `request`: 138 | 139 | ```php 140 | $request->validate([ 141 | 'ip_address' => 'required|ip_vn', // Kiểm tra tất cả ipv4 HOẶC v6 chỉ cần ip trong nước là được. 142 | 'ipv4_address' => 'required|ipv4_vn', // Kiểm tra phải là ipv4 trong nước. 143 | 'ipv6_address' => 'required|ipv6_vn', // Kiểm tra phải là ipv6 trong nước. 144 | ]); 145 | ``` 146 | 147 | + Sử dụng trong `FormRequest`: 148 | 149 | ```php 150 | public function rules() 151 | { 152 | return [ 153 | 'ip_address' => 'required|ip_vn', // Kiểm tra tất cả ipv4 HOẶC v6 chỉ cần ip trong nước là được. 154 | 'ipv4_address' => 'required|ipv4_vn', // Kiểm tra phải là ipv4 trong nước. 155 | 'ipv6_address' => 'required|ipv6_vn', // Kiểm tra phải là ipv6 trong nước. 156 | ]; 157 | } 158 | ``` 159 | 160 | + Sử dụng dưới dạng `Rule`: 161 | 162 | ```php 163 | public function rules() 164 | { 165 | return [ 166 | 'ip_address' => ['required', new \PHPViet\Laravel\Validation\Rules\IpVN()], // Kiểm tra tất cả ipv4 HOẶC v6 chỉ cần ip trong nước là được. 167 | 'ipv4_address' => ['required', new \PHPViet\Laravel\Validation\Rules\IpVN(4)], // Kiểm tra phải là ipv4 trong nước. 168 | 'ipv6_address' => ['required', new \PHPViet\Laravel\Validation\Rules\IpVN(6)], // Kiểm tra phải là ipv6 trong nước. 169 | ]; 170 | } 171 | ``` 172 | 173 | ## Ngôn ngữ 174 | 175 | Nếu như bạn muốn thay đổi các error message thì hãy publish resource đính kèm thông qua câu lệnh: 176 | 177 | ```php 178 | php artisan vendor:publish 179 | ``` 180 | 181 | Sau khi publish xong bạn hãy vào `resources/lang/vendor/phpVietValidation` để thao tác chỉnh sửa theo ý bạn. 182 | 183 | ## Dành cho nhà phát triển 184 | 185 | Nếu như bạn cảm thấy các kiểu kiểm tra dữ liệu bên trên vẫn chưa đủ đối với thị trường 186 | trong nước và bạn muốn đóng góp để phát triển chung, chúng tôi rất hoan nghênh! 187 | Hãy tạo các `issue` để đóng góp ý tưởng cho phiên bản kế tiếp hoặc tạo `PR` 188 | để đóng góp thêm các kiểu kiểm tra dữ liệu còn thiếu sót. Cảm ơn! 189 | --------------------------------------------------------------------------------