public function testValidateAttribute() { $val = new RangeValidator(['range' => range(1, 10, 1)]); $m = FakedValidationModel::createWithAttributes(['attr_r1' => 5, 'attr_r2' => 999]); $val->validateAttribute($m, 'attr_r1'); $this->assertFalse($m->hasErrors()); $val->validateAttribute($m, 'attr_r2'); $this->assertTrue($m->hasErrors('attr_r2')); $err = $m->getErrors('attr_r2'); $this->assertTrue(stripos($err[0], 'attr_r2') !== false); }
public function init() { if (!is_string($this->enumClass)) { throw new InvalidConfigException('The "enumClass" property must be set.'); } $consts = (new ReflectionClass($this->enumClass))->getConstants(); $this->range = array_values($consts); parent::init(); }
public function init() { if (!is_string($this->enum)) { throw new InvalidConfigException('The "enum" property must be set.'); } /* @var $class BaseEnum */ $class = $this->enum; $this->range = $class::items(); parent::init(); }
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()]]; }
/** * @inheritdoc */ public function init() { parent::init(); $this->allowArray = true; }
/** * 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(); } }