mirror of
https://github.com/yiisoft/yii2.git
synced 2026-03-23 15:37:24 +01:00
20160611
This commit is contained in:
@@ -72,4 +72,91 @@ class EachValidatorTest extends TestCase
|
||||
$validator->validateAttribute($model, 'attr_one');
|
||||
$this->assertNotContains('integer', $model->getFirstError('attr_one'));
|
||||
}
|
||||
}
|
||||
<<<<<<< HEAD
|
||||
}
|
||||
=======
|
||||
|
||||
/**
|
||||
* @depends testValidate
|
||||
*/
|
||||
public function testCustomMessageValue()
|
||||
{
|
||||
$model = FakedValidationModel::createWithAttributes([
|
||||
'attr_one' => [
|
||||
'TEXT',
|
||||
],
|
||||
]);
|
||||
$validator = new EachValidator(['rule' => ['integer', 'message' => '{value} is not an integer']]);
|
||||
|
||||
$validator->validateAttribute($model, 'attr_one');
|
||||
$this->assertSame('TEXT is not an integer', $model->getFirstError('attr_one'));
|
||||
|
||||
$model->clearErrors();
|
||||
$validator->allowMessageFromRule = false;
|
||||
$validator->message = '{value} is invalid';
|
||||
$validator->validateAttribute($model, 'attr_one');
|
||||
$this->assertEquals('TEXT is invalid', $model->getFirstError('attr_one'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/yiisoft/yii2/issues/10825
|
||||
*
|
||||
* @depends testValidate
|
||||
*/
|
||||
public function testSkipOnEmpty()
|
||||
{
|
||||
$validator = new EachValidator(['rule' => ['integer', 'skipOnEmpty' => true]]);
|
||||
$this->assertTrue($validator->validate(['']));
|
||||
|
||||
$validator = new EachValidator(['rule' => ['integer', 'skipOnEmpty' => false]]);
|
||||
$this->assertFalse($validator->validate(['']));
|
||||
|
||||
$model = FakedValidationModel::createWithAttributes([
|
||||
'attr_one' => [
|
||||
''
|
||||
],
|
||||
]);
|
||||
$validator = new EachValidator(['rule' => ['integer', 'skipOnEmpty' => true]]);
|
||||
$validator->validateAttribute($model, 'attr_one');
|
||||
$this->assertFalse($model->hasErrors('attr_one'));
|
||||
|
||||
$model->clearErrors();
|
||||
$validator = new EachValidator(['rule' => ['integer', 'skipOnEmpty' => false]]);
|
||||
$validator->validateAttribute($model, 'attr_one');
|
||||
$this->assertTrue($model->hasErrors('attr_one'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://github.com/yiisoft/yii2/issues/9935
|
||||
*
|
||||
* @depends testValidate
|
||||
*/
|
||||
public function testCompare()
|
||||
{
|
||||
$model = FakedValidationModel::createWithAttributes([
|
||||
'attr_one' => [
|
||||
'value1',
|
||||
'value2',
|
||||
'value3',
|
||||
],
|
||||
'attr_two' => 'value2',
|
||||
]);
|
||||
$validator = new EachValidator(['rule' => ['compare', 'compareAttribute' => 'attr_two']]);
|
||||
$validator->validateAttribute($model, 'attr_one');
|
||||
$this->assertNotEmpty($model->getErrors('attr_one'));
|
||||
$this->assertEquals(3, count($model->attr_one));
|
||||
|
||||
$model = FakedValidationModel::createWithAttributes([
|
||||
'attr_one' => [
|
||||
'value1',
|
||||
'value2',
|
||||
'value3',
|
||||
],
|
||||
'attr_two' => 'value4',
|
||||
]);
|
||||
$validator = new EachValidator(['rule' => ['compare', 'compareAttribute' => 'attr_two', 'operator' => '!=']]);
|
||||
$validator->validateAttribute($model, 'attr_one');
|
||||
$this->assertEmpty($model->getErrors('attr_one'));
|
||||
}
|
||||
}
|
||||
>>>>>>> master
|
||||
|
||||
Reference in New Issue
Block a user