DefaultValueValidator is not really a validator. It is provided mainly to allow specifying attribute default values when they are empty.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends Validator
 public function testValidateAttribute()
 {
     $val = new DefaultValueValidator();
     $val->value = 'test_value';
     $obj = new \stdclass();
     $obj->attrA = 'attrA';
     $obj->attrB = null;
     $obj->attrC = '';
     // original values to chek which attritubes where modified
     $objB = clone $obj;
     $val->validateAttribute($obj, 'attrB');
     $this->assertEquals($val->value, $obj->attrB);
     $this->assertEquals($objB->attrA, $obj->attrA);
     $val->value = 'new_test_value';
     $obj = clone $objB;
     // get clean object
     $val->validateAttribute($obj, 'attrC');
     $this->assertEquals('new_test_value', $obj->attrC);
     $this->assertEquals($objB->attrA, $obj->attrA);
     $val->validateAttribute($obj, 'attrA');
     $this->assertEquals($objB->attrA, $obj->attrA);
 }
示例#2
0
 public function rules()
 {
     return [[['createdByAttribute', 'updatedByAttribute'], 'blameable'], [['createdAtAttribute', 'updatedAtAttribute'], 'timestampValidator'], [['slugAttribute'], 'sluggableValidator'], [['langClassSuffix', 'alias'], 'safe'], [['userTable', 'attribute', 'pathAttribute', 'baseUrlAttribute', 'name'], 'match', 'pattern' => '/^([\\w ]+\\.)?([\\w\\* ]+)$/', 'message' => 'Only word characters, and optionally spaces, an asterisk and/or a dot are allowed.'], [['immutable'], BooleanValidator::className()], [['attribute', 'pathAttribute', 'baseUrlAttribute'], 'uploadValidation'], [['createdAtAttribute', 'updatedAtAttribute', 'createdByAttribute', 'updatedByAttribute'], 'string', 'max' => 255], [['phoneAttribute'], 'phoneValidate'], [['langForeignKey'], 'langField'], [['dynamicLangClass'], DefaultValueValidator::className(), 'value' => function ($value, $model, $attribute = null) {
         return $this->class || $this->tableName ? true : null;
     }], [['abridge'], DefaultValueValidator::className(), 'value' => function ($value, $model, $attribute = null) {
         return $this->class || $this->tableName ? false : null;
     }], [['requireTranslations'], DefaultValueValidator::className(), 'value' => function ($value, $model, $attribute = null) {
         return $this->class || $this->tableName ? false : null;
     }], [['tableName', 'class'], 'classValid'], [['languageField'], DefaultValueValidator::className(), 'value' => function ($value, $model, $attribute = null) {
         return $this->class || $this->tableName ? 'language' : $value;
     }], [['attributesLang'], DefaultValueValidator::className(), 'value' => function () {
         $this->class || $this->tableName ? [new Field(['name' => 'title', 'type' => Schema::TYPE_STRING, 'length' => 500, 'comment' => 'Title']), new Field(['name' => 'body', 'type' => Schema::TYPE_TEXT, 'comment' => 'Body']), new Field(['name' => 'slug', 'type' => Schema::TYPE_STRING, 'length' => 2000, 'comment' => 'Slug'])] : null;
     }]];
 }