Skip to content

Commit 48350d2

Browse files
Added validators
1 parent be59ae2 commit 48350d2

File tree

7 files changed

+260
-3
lines changed

7 files changed

+260
-3
lines changed

composer.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
],
1717
"require": {
1818
"php": "^7.1",
19-
"yiisoft/yii2": "~2.0.14"
19+
"phpviet/validation": "^1.0",
20+
"yiisoft/yii2": "~2.0.13"
2021
},
2122
"autoload": {
2223
"psr-4": {
23-
"phpViet\\yii\\validation\\": "src"
24+
"phpviet\\yii\\validation\\": "src"
2425
}
2526
},
2627
"config": {
@@ -30,6 +31,6 @@
3031
"branch-alias": {
3132
"dev-master": "1.0-dev"
3233
},
33-
"bootstrap": "phpViet\\yii\\validation\\Bootstrap"
34+
"bootstrap": "phpviet\\yii\\validation\\Bootstrap"
3435
}
3536
}

src/messages/en/validation.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
return [
10+
'id' => '{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' => '{attribute} must be Vietnam ip.',
14+
'ipv4' => '{attribute} must be Vietnam ipv4.',
15+
'ipv6' => '{attribute} must be Vietnam ipv6.',
16+
];

src/messages/vi/validation.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
return [
10+
'id' => '{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' => '{attribute} phải là ip Việt Nam.',
14+
'ipv4' => '{attribute} phải là ipv4 tại Việt Nam.',
15+
'ipv6' => '{attribute} phải là ipv6 tại Việt Nam.',
16+
];

src/validators/IdVNValidator.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\validators;
10+
11+
use Yii;
12+
13+
use yii\validators\RegularExpressionValidator;
14+
15+
use PHPViet\Validation\Rules\IdVN as PatternProvider;
16+
17+
/**
18+
* @author Vuong Minh <vuongxuongminh@gmail.com>
19+
* @since 1.0.0
20+
*/
21+
class IdVNValidator extends RegularExpressionValidator
22+
{
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
public function init()
28+
{
29+
$this->message = $this->message ?? Yii::t('phpviet/validation', 'id');
30+
$this->pattern = PatternProvider::pregFormat();
31+
32+
parent::init();
33+
}
34+
35+
}

src/validators/IpVNValidator.php

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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\validators;
10+
11+
use Yii;
12+
13+
use yii\validators\Validator;
14+
use yii\validators\IpValidator;
15+
16+
use PHPViet\Validation\Validator as ConcreteValidator;
17+
use PHPViet\Validation\Rules\IpVN as ConcreteIpVN;
18+
19+
/**
20+
* @author Vuong Minh <vuongxuongminh@gmail.com>
21+
* @since 1.0.0
22+
*/
23+
class IpVNValidator extends Validator
24+
{
25+
26+
const IPV4 = ConcreteIpVN::IPV4;
27+
28+
const IPV6 = ConcreteIpVN::IPV6;
29+
30+
/**
31+
* @var int|null Version ip cần kiểm tra
32+
*/
33+
public $version;
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function init()
39+
{
40+
$this->message = $this->message ?? $this->getDefaultMessage();
41+
42+
parent::init();
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
public function validateValue($value)
49+
{
50+
if (ConcreteValidator::ipVN($this->version)->validate($value)) {
51+
52+
return null;
53+
}
54+
55+
return [$this->message, []];
56+
}
57+
58+
/**
59+
* @inheritDoc
60+
* @throws \yii\base\InvalidConfigException
61+
*/
62+
public function clientValidateAttribute($model, $attribute, $view)
63+
{
64+
return $this->getClientIpValidator()->clientValidateAttribute($model, $attribute, $view);
65+
}
66+
67+
/**
68+
* @inheritDoc
69+
* @throws \yii\base\InvalidConfigException
70+
*/
71+
public function getClientOptions($model, $attribute)
72+
{
73+
return $this->getClientIpValidator()->getClientOptions($model, $attribute);
74+
}
75+
76+
/**
77+
* @inheritDoc
78+
*/
79+
protected function getDefaultMessage(): string
80+
{
81+
switch ($this->version) {
82+
case self::IPV4:
83+
return Yii::t('phpviet/validation', 'ipv4');
84+
case self::IPV6:
85+
return Yii::t('phpviet/validation', 'ipv6');
86+
default:
87+
return Yii::t('phpviet/validation', 'ip');
88+
}
89+
}
90+
91+
/**
92+
* @var IpValidator
93+
* @see getClientIpValidator()
94+
*/
95+
private $_clientIpValidator;
96+
97+
/**
98+
* Trả về [[IpValidator]] hổ trợ cho việc tạo js validator tại client.
99+
*
100+
* @return IpValidator
101+
* @throws \yii\base\InvalidConfigException
102+
*/
103+
protected function getClientIpValidator(): IpValidator
104+
{
105+
if (null === $this->_clientIpValidator) {
106+
return $this->_clientIpValidator = Yii::createObject([
107+
'class' => IpValidator::class,
108+
'message' => $this->message,
109+
'ipv4NotAllowed' => $this->message,
110+
'ipv6NotAllowed' => $this->message,
111+
'ipv4' => null === $this->version || self::IPV4 === $this->version,
112+
'ipv6' => null === $this->version || self::IPV6 === $this->version
113+
]);
114+
}
115+
116+
return $this->_clientIpValidator;
117+
}
118+
119+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\validators;
10+
11+
use Yii;
12+
13+
use yii\validators\RegularExpressionValidator;
14+
15+
use PHPViet\Validation\Rules\LandLineVN as PatternProvider;
16+
17+
/**
18+
* @author Vuong Minh <vuongxuongminh@gmail.com>
19+
* @since 1.0.0
20+
*/
21+
class LandLineVNValidator extends RegularExpressionValidator
22+
{
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
public function init()
28+
{
29+
$this->message = $this->message ?? Yii::t('phpviet/validation', 'land_line');
30+
$this->pattern = PatternProvider::pregFormat();
31+
32+
parent::init();
33+
}
34+
35+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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\validators;
10+
11+
use Yii;
12+
13+
use yii\validators\RegularExpressionValidator;
14+
15+
use PHPViet\Validation\Rules\MobileVN as PatternProvider;
16+
17+
/**
18+
* @author Vuong Minh <vuongxuongminh@gmail.com>
19+
* @since 1.0.0
20+
*/
21+
class MobileVNValidator extends RegularExpressionValidator
22+
{
23+
24+
/**
25+
* @inheritDoc
26+
*/
27+
public function init()
28+
{
29+
$this->message = $this->message ?? Yii::t('phpviet/validation', 'mobile');
30+
$this->pattern = PatternProvider::pregFormat();
31+
32+
parent::init();
33+
}
34+
35+
}

0 commit comments

Comments
 (0)