Possible boolean values can be configured via the [[trueValue]] and [[falseValue]] properties. And the comparison can be either [[strict]] or not.
Since: 2.0
Author: Qiang Xue (qiang.xue@gmail.com)
Inheritance: extends Validator
コード例 #1
0
ファイル: BooleanValidatorTest.php プロジェクト: howq/yii2
 public function testValidateAttributeAndError()
 {
     $obj = new FakedValidationModel();
     $obj->attrA = true;
     $obj->attrB = '1';
     $obj->attrC = '0';
     $obj->attrD = [];
     $val = new BooleanValidator();
     $val->validateAttribute($obj, 'attrA');
     $this->assertFalse($obj->hasErrors('attrA'));
     $val->validateAttribute($obj, 'attrC');
     $this->assertFalse($obj->hasErrors('attrC'));
     $val->strict = true;
     $val->validateAttribute($obj, 'attrB');
     $this->assertFalse($obj->hasErrors('attrB'));
     $val->validateAttribute($obj, 'attrD');
     $this->assertTrue($obj->hasErrors('attrD'));
 }
コード例 #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;
     }]];
 }
コード例 #3
0
ファイル: ValidatorTest.php プロジェクト: aivavic/yii2
 public function testCreateValidator()
 {
     $model = FakedValidationModel::createWithAttributes(['attr_test1' => 'abc', 'attr_test2' => '2013']);
     /* @var $numberVal NumberValidator */
     $numberVal = TestValidator::createValidator('number', $model, ['attr_test1']);
     $this->assertInstanceOf(NumberValidator::className(), $numberVal);
     $numberVal = TestValidator::createValidator('integer', $model, ['attr_test2']);
     $this->assertInstanceOf(NumberValidator::className(), $numberVal);
     $this->assertTrue($numberVal->integerOnly);
     $val = TestValidator::createValidator('boolean', $model, ['attr_test1', 'attr_test2'], ['on' => ['a', 'b']]);
     $this->assertInstanceOf(BooleanValidator::className(), $val);
     $this->assertSame(['a', 'b'], $val->on);
     $this->assertSame(['attr_test1', 'attr_test2'], $val->attributes);
     $val = TestValidator::createValidator('boolean', $model, ['attr_test1', 'attr_test2'], ['on' => ['a', 'b'], 'except' => ['c', 'd', 'e']]);
     $this->assertInstanceOf(BooleanValidator::className(), $val);
     $this->assertSame(['a', 'b'], $val->on);
     $this->assertSame(['c', 'd', 'e'], $val->except);
     $val = TestValidator::createValidator('inlineVal', $model, ['val_attr_a']);
     $this->assertInstanceOf(InlineValidator::className(), $val);
     $this->assertSame('inlineVal', $val->method);
 }
コード例 #4
0
ファイル: MenuItem.php プロジェクト: kuzmiand/easyii
 /**
  * @return array
  */
 public function rules()
 {
     return [['parent_id', 'validateParent'], ['label', StringValidator::className()], ['label', RequiredValidator::className()], ['path', StringValidator::className()], ['status', BooleanValidator::className()], ['order_num', NumberValidator::className()]];
 }
コード例 #5
0
ファイル: Field.php プロジェクト: infinitydevphp/infinity-gii
 public function rules()
 {
     return [[['type', 'name'], RequiredValidator::className()], [['length', 'precision', 'scale'], NumberValidator::className()], [['is_not_null', 'is_unique', 'unsigned', 'isCompositeKey'], BooleanValidator::className()], [['name'], StringValidator::className(), 'max' => 50], [['comment', 'fk_name'], StringValidator::className(), 'max' => 255], [['type'], RangeValidator::className(), 'range' => $this->getTypeList()]];
 }
コード例 #6
0
ファイル: Config.php プロジェクト: skoro/yii2-admin-template
 /**
  * Validate 'value' attribute against of 'value_type'.
  */
 public function validateValue($attribute, $params = [])
 {
     if ($this->required) {
         $required = Yii::createObject(['class' => \yii\validators\RequiredValidator::className(), 'message' => Yii::t('app', '{label} is required.', ['label' => $this->title])]);
         if (!$required->validate($this->{$attribute})) {
             $this->addError($attribute, $required->message);
             return;
         }
     } else {
         $value = $this->{$attribute};
         if ($value === null || $value === '') {
             return;
         }
     }
     switch ($this->value_type) {
         case static::TYPE_INT:
             $args = ['class' => NumberValidator::className(), 'integerOnly' => true, 'message' => 'Not an integer'];
             break;
         case static::TYPE_NUM:
             $args = ['class' => NumberValidator::className(), 'message' => 'Not a number'];
             break;
         case static::TYPE_EMAIL:
             $args = ['class' => EmailValidator::className(), 'message' => 'Not a valid email'];
             break;
         case static::TYPE_URL:
             $args = ['class' => UrlValidator::className(), 'message' => 'Not a valid url'];
             break;
         case static::TYPE_SWITCH:
             $args = ['class' => BooleanValidator::className(), 'message' => 'Must be boolean value'];
             break;
         case static::TYPE_TEXT:
         case static::TYPE_EDITOR:
         case static::TYPE_PASSWORD:
             $args = ['class' => StringValidator::className()];
             break;
         case static::TYPE_SELECT:
             $args = ['class' => RangeValidator::className(), 'range' => array_keys($this->options), 'message' => 'Invalid value'];
             break;
         default:
             throw new InvalidParamException('Unknown config type: ' . $this->value_type);
     }
     $validator = Yii::createObject($args);
     if (!$validator->validate($this->{$attribute})) {
         $this->addError($attribute, $validator->message);
     } else {
         $this->castType();
     }
 }