forked from sdlins/Yii-Conditional-Validator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYiiConditionalValidator.php
More file actions
227 lines (197 loc) · 7.49 KB
/
Copy pathYiiConditionalValidator.php
File metadata and controls
227 lines (197 loc) · 7.49 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
/**
* Validates multiple attributes using any Yii Core Validator
* depending on some another attribute's condition (validation) is true;
*
* @author Sidney Lins <solucoes@wmaior.com>
* @copyright 2011 Sidney Lins
* @license New BSD Licence
* @version Release: 0.2.0
*/
class YiiConditionalValidator extends CValidator
{
public $validation = array('required');
public $dependentValidations = array();
public $message = "{dependentAttribute} is invalid.";
/**
* Whether to skip validation if the dependent validators failed. Useful for
* running validation based on a user-entered value.
* @var bool
*/
public $skipOnDependencyError = false;
/**
* Validate the attribute conditionally
*
* @param CActiveRecord $object the model object
* @param string $attribute the attribute name
* Can specify related model via "relatedModel.attribute"
*
* @return void
*/
protected function validateAttribute($object, $attribute)
{
// Get any errors on dependent attributes
$errors = $this->validateDependentAttributes($object);
if ($errors) {
if ($this->skipOnDependencyError) {
return;
}
// Apply dependecy errors
foreach ($errors as $error) {
$this->applyDependentValidationError($error, $object, $attribute);
}
}
// No dependency errors. Run validator
$this->runValidator($object, $attribute, $this->validation);
}
/**
* Validate dependent attributes
*
* @param CActiveRecord $object the model object
*
* @return array of errors (object, attribute, message)
*/
protected function validateDependentAttributes($object)
{
$errors = array();
foreach ($this->dependentValidations as $attribute => $validationData) {
if (!is_array($validationData) || !count($validationData)) {
throw new CException(
'YiiConditionalValidator: dependentAttributesAndValidators '
. 'must be an array and must have at least one value.'
);
}
$attributes = array_map('trim', explode(',', $attribute));
foreach ($attributes as $attribute) {
foreach ($validationData as $validation) {
$validateTarget = $this
->getValidationTarget($object, $attribute);
if (is_null($validateTarget)) {
// If it's possible that the relation doesn't exist, this
// shoudl be a separate validation rule that occurs before
// this one.
continue;
}
// relObject
$errorMessage = $this->runValidatorKeepExisting(
$validateTarget['object'],
$validateTarget['attribute'],
$validation
);
if (!empty($errorMessage)) {
$errorMessage = isset($validationData['message'])
? $validationData['message'] : $errorMessage;
$errors[] = array(
'object' => $validateTarget['object'],
'attribute' => $validateTarget['attribute'],
'message' => $errorMessage,
);
}
}
}
}
return $errors;
}
/**
* Get the validation target. May be a related object attribute.
*
* @param CActiveRecord $object the model object
* @param string $attribute the attribute
*
* @return array(object => $object, attribute => $attribute) or null
*/
protected function getValidationTarget($object, $attribute)
{
$validateAttribute = $attribute;
$validateObject = $object;
// If this is a related object, parse and retrieve
if (strpos($validateAttribute, '.') !== false) {
$parts = explode('.', $attribute);
$validateAttribute = $parts[1];
$validateObject = $object->getRelated($parts[0]);
}
if (is_null($validateObject)) {
return null;
}
return array(
'object' => $validateObject,
'attribute' => $validateAttribute,
);
}
/**
* Apply errors for dependent attribute validation. Errors are applied to the
* original attribute on the original object
*
* @param array $error (object, attribute, message)
* @param CActiveRecord $originalObject the original model object
* @param string $originalAttribute the original attribute
*
* @return void
*/
protected function applyDependentValidationError(
$error, $originalObject, $originalAttribute
) {
$dependentObject = $error['object'];
$dependentAttribute = $error['attribute'];
$message = $error['message'];
$originalObject->addError(
$originalAttribute,
Yii::t(
'yii',
$message,
array(
'{attribute}'
=> $originalObject->getAttributeLabel($originalAttribute),
'{value}'
=> $originalObject->{$originalAttribute},
'{dependentAttribute}'
=> $dependentObject->getAttributeLabel($dependentAttribute),
'{dependentValue}'
=> $dependentObject->{$dependentAttribute},
)
)
);
}
/**
* Run a validator but keep existing errors without modification
*
* @param CActiveRecord $object the model object
* @param string $attribute the attribute
* @param array $validation the validator definition
*
* @return string|null the error message if validation failed, or null
*/
protected function runValidatorKeepExisting(
$object, $attribute, $validation
) {
// Back up and clear errors
$errorsBackup = $object->getErrors();
$object->clearErrors();
$this->runValidator($object, $attribute, $validation);
// Record error
$errorMessage = $object->getError($attribute);
// Restore existing errors
$object->clearErrors();
$object->addErrors($errorsBackup);
return $errorMessage;
}
/**
* Run a validator
*
* @param CActiveRecord $object the model object
* @param string $attribute the attribute
* @param array $validation the validator definition
*
* @return void
*/
protected function runValidator(
$object, $attribute, $validation
) {
$validatorName = $validation[0];
$validatorParams = array_slice($validation, 1, count($validation) - 1);
$validator = CValidator::createValidator(
$validatorName, $object, $attribute, $validatorParams
);
$validator->validate($object, $attribute);
}
}