Skip to content

Commit 664ae61

Browse files
Added bootstrap
1 parent 48350d2 commit 664ae61

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed

src/Bootstrap.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/yii-validation
4+
*
5+
* @copyright (c) PHP Viet
6+
* @license [MIT](https://opensource.org/licenses/MIT)
7+
*/
8+
9+
namespace phpviet\yii\validation;
10+
11+
use yii\base\BootstrapInterface;
12+
use yii\i18n\I18N;
13+
use yii\validators\Validator;
14+
15+
use phpviet\yii\validation\validators\LandLineVNValidator;
16+
use phpviet\yii\validation\validators\MobileVNValidator;
17+
use phpviet\yii\validation\validators\IdVNValidator;
18+
use phpviet\yii\validation\validators\IpVNValidator;
19+
20+
/**
21+
* @author Vuong Minh <vuongxuongminh@gmail.com>
22+
* @since 1.0.0
23+
*/
24+
class Bootstrap implements BootstrapInterface
25+
{
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
public function bootstrap($app): void
31+
{
32+
$this->loadTrans($app->i18n);
33+
$this->loadShortValidators();
34+
}
35+
36+
/**
37+
* Nạp translation hổ trợ đa ngôn ngữ cho câu báo lỗi.
38+
*
39+
* @param I18N $i18n
40+
*/
41+
protected function loadTrans(I18N $i18n): void
42+
{
43+
$i18n->translations['phpviet/validation'] = array_merge([
44+
'class' => 'yii\i18n\PhpMessageSource',
45+
'basePath' => '@phpviet/yii/validation/messages',
46+
'fileMap' => [
47+
'phpviet/validation' => 'validation'
48+
]
49+
], $i18n->translations['phpviet/validation'] ?? []);
50+
51+
}
52+
53+
/**
54+
* Nạp short validators vào lớp `yii\validators\Validator` hổ trợ friendly syntax.
55+
*/
56+
protected function loadShortValidators(): void
57+
{
58+
Validator::$builtInValidators = array_merge(Validator::$builtInValidators, [
59+
'land_line_vn' => LandLineVNValidator::class,
60+
'mobile_vn' => MobileVNValidator::class,
61+
'id_vn' => IdVNValidator::class,
62+
'ip_vn' => IpVNValidator::class,
63+
'ipv4_vn' => [
64+
'class' => IpVNValidator::class,
65+
'version' => IpVNValidator::IPV4
66+
],
67+
'ipv6_vn' => [
68+
'class' => IpVNValidator::class,
69+
'version' => IpVNValidator::IPV6
70+
]
71+
]);
72+
}
73+
}

0 commit comments

Comments
 (0)