/
ImproveRangeValidator.php
61 lines (55 loc) · 1.25 KB
/
ImproveRangeValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* Created by PhpStorm.
* User: soooldier
* Date: 1/19/15
* Time: 21:52
*/
namespace improve\rangeValidator;
use Yii;
use yii\validators\RangeValidator;
/**
* Class Region
* @package \improve\rangeValidator
*/
class ImproveRangeValidator extends RangeValidator
{
/**
* @var string value默认分隔符
*/
public $sep = ",";
/**
* @var callable
* 参数为range和model,在这里可以定制range数组
*/
public $advanceRange;
/**
* @var bool 返回数组
*/
public $outputArray = false;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->allowArray = true;
}
/**
* @inheritdoc
*/
public function validateAttribute($model, $attribute)
{
if ($this->advanceRange instanceof \Closure) {
$this->range = call_user_func($this->advanceRange, $this->range, $model);
}
$tmp_value = explode($this->sep, $model->$attribute);
$result = $this->validateValue($tmp_value);
if (!empty($result)) {
$this->addError($model, $attribute, $result[0], $result[1]);
}
if ($this->outputArray) {
$model->$attribute = $tmp_value;
}
}
}