Fix #19407: Fix yii\validators\UniqueValidator and yii\validators\ExistValidator to respect skipOnError option for target attributes

This commit is contained in:
Bizley
2022-07-29 08:47:33 +02:00
committed by GitHub
parent bba3806961
commit 009961963c
5 changed files with 74 additions and 2 deletions

View File

@ -505,6 +505,33 @@ abstract class UniqueValidatorTest extends DatabaseTestCase
ActiveRecord::$db = $this->getConnection();
}
public function testSecondTargetAttributeWithError()
{
$validator = new UniqueValidator(['targetAttribute' => ['email', 'name']]);
$customer = new Customer();
$customer->email = 'user1@example.com';
$customer->name = 'user1';
$validator->validateAttribute($customer, 'email');
$this->assertTrue($customer->hasErrors('email'));
$customer->clearErrors();
$customer->addError('name', 'error');
$validator->validateAttribute($customer, 'email');
$this->assertFalse($customer->hasErrors('email')); // validator should be skipped
$validator = new UniqueValidator([
'targetAttribute' => ['email', 'name'],
'skipOnError' => false,
]);
$customer->clearErrors();
$customer->addError('name', 'error');
$validator->validateAttribute($customer, 'email');
$this->assertTrue($customer->hasErrors('email')); // validator should not be skipped
}
}
class WithCustomer extends Customer {