/** * @param ActiveRecord $owner */ public function attach($owner) { parent::attach($owner); // Применяем конфигурационные опции foreach ($this->imageConfig as $key => $value) { $var = '_' . $key; $this->{$var} = $value; } // Вычисляем корень сайта if (empty($this->_rootPathAlias)) { $savePathParts = explode('/', $this->_savePathAlias); // Удаляем последнюю часть unset($savePathParts[count($savePathParts - 1)]); // Объединяем все части обратно $this->_rootPathAlias = implode('/', $savePathParts); } // Добавляем валидатор require if ($this->_imageRequire) { $owner->validators->append(Validator::createValidator(RequiredValidator::className(), $owner, $this->_imageAttribute)); } // Подключаем валидатор изображения $validatorParams = array_merge(['extensions' => $this->_fileTypes, 'maxSize' => $this->_maxFileSize, 'skipOnEmpty' => true, 'tooBig' => 'Изображение слишком велико, максимальный размер: ' . floor($this->_maxFileSize / 1024 / 1024) . ' Мб'], $this->_imageValidatorParams); $validator = Validator::createValidator(ImageValidator::className(), $owner, $this->_imageAttribute, $validatorParams); $owner->validators->append($validator); }
/** * 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(); } }
/** * @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()]]; }
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()]]; }