|
10 | 10 | use yii\base\Exception; |
11 | 11 | use yii\validators\ExistValidator; |
12 | 12 | use yiiunit\data\ar\ActiveRecord; |
| 13 | +use yiiunit\data\ar\Customer; |
13 | 14 | use yiiunit\data\ar\Order; |
14 | 15 | use yiiunit\data\ar\OrderItem; |
15 | 16 | use yiiunit\data\validators\models\ValidatorTestMainModel; |
@@ -268,4 +269,31 @@ public function testForceMaster() |
268 | 269 |
|
269 | 270 | ActiveRecord::$db = $this->getConnection(); |
270 | 271 | } |
| 272 | + |
| 273 | + public function testSecondTargetAttributeWithError() |
| 274 | + { |
| 275 | + $validator = new ExistValidator(['targetAttribute' => ['email', 'name']]); |
| 276 | + $customer = new Customer(); |
| 277 | + $customer-> email = '[email protected]'; |
| 278 | + $customer->name = 'user11111'; |
| 279 | + |
| 280 | + $validator->validateAttribute($customer, 'email'); |
| 281 | + $this->assertTrue($customer->hasErrors('email')); |
| 282 | + |
| 283 | + $customer->clearErrors(); |
| 284 | + |
| 285 | + $customer->addError('name', 'error'); |
| 286 | + $validator->validateAttribute($customer, 'email'); |
| 287 | + $this->assertFalse($customer->hasErrors('email')); // validator should be skipped |
| 288 | + |
| 289 | + $validator = new ExistValidator([ |
| 290 | + 'targetAttribute' => ['email', 'name'], |
| 291 | + 'skipOnError' => false, |
| 292 | + ]); |
| 293 | + |
| 294 | + $customer->clearErrors(); |
| 295 | + $customer->addError('name', 'error'); |
| 296 | + $validator->validateAttribute($customer, 'email'); |
| 297 | + $this->assertTrue($customer->hasErrors('email')); // validator should not be skipped |
| 298 | + } |
271 | 299 | } |
0 commit comments