forked from yii2tech/embedded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathValidator.php
113 lines (105 loc) · 3.12 KB
/
Validator.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
* @link https://github.com/yii2tech
* @copyright Copyright (c) 2015 Yii2tech
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php)
*/
namespace yii2tech\embedded;
use Yii;
use yii\base\InvalidConfigException;
use yii\base\Model;
/**
* Validator validates embedded entities as nested models.
* This validator may be applied only for the model, which implements [[ContainerInterface]] interface.
*
* ```php
* class User extends Model implements ContainerInterface
* {
* use ContainerTrait;
*
* public $contactData;
*
* public function embedContact()
* {
* return $this->mapEmbedded('contactData', Contact::className());
* }
*
* public function rules()
* {
* return [
* ['contact', 'yii2tech\embedded\Validator'],
* ]
* }
* }
*
* class Contact extends Model
* {
* public $email;
*
* public function rules()
* {
* return [
* ['email', 'required'],
* ['email', 'email'],
* ]
* }
* }
* ```
*
* @see ContainerInterface
*
* @author Paul Klimov <[email protected]>
* @since 1.0
*/
class Validator extends \yii\validators\Validator
{
/**
* @var boolean whether to add an error message to embedded source attribute instead of embedded name itself.
*/
public $addErrorToSource = true;
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} is invalid.');
}
}
/**
* @inheritdoc
*/
public function validateAttribute($model, $attribute)
{
if (!($model instanceof ContainerInterface)) {
throw new InvalidConfigException('Owner model must implement "yii2tech\embedded\ContainerInterface" interface.');
}
$mapping = $model->getEmbeddedMapping($attribute);
$embedded = $model->getEmbedded($attribute);
if ($mapping->multiple) {
if (!is_array($embedded) && !($embedded instanceof \IteratorAggregate)) {
$error = $this->message;
} else {
foreach ($embedded as $embeddedModel) {
if (!($embeddedModel instanceof Model)) {
throw new InvalidConfigException('Embedded object "' . get_class($embeddedModel) . '" must be an instance or descendant of "' . Model::className() . '".');
}
if (!$embeddedModel->validate()) {
$error = $this->message;
}
}
}
} else {
if (!($embedded instanceof Model)) {
throw new InvalidConfigException('Embedded object "' . get_class($embedded) . '" must be an instance or descendant of "' . Model::className() . '".');
}
if (!$embedded->validate()) {
$error = $this->message;
}
}
if (!empty($error)) {
$this->addError($model, $this->addErrorToSource ? $mapping->source : $attribute, $error);
}
}
}