/** * * @param CModel $object the object to be validated * @param mixed $rule the rules to validate the object against * @return boolean false if it has errors, true otherwise */ protected function validateConditional(&$object, $rule) { if (isset($rule['group'])) { if (is_array($rule['group'])) { foreach ($rule['group'] as $r) { if (is_array($r)) { $val = $this->validateConditional($object, $r); if (!$val) { return false; } } else { continue; } } } else { throw new \CException(\Yii::t('EConditionalValidator', 'Group must be an array of rules')); } } else { list($attributes, $conditionalValidator) = $rule; $parameters = array_splice($rule, 2); $validator = \CValidator::createValidator($conditionalValidator, $object, $attributes, $parameters); $validator->validate($object); if ($object->hasErrors()) { $object->clearErrors(); return false; } } return true; }
public function beforeValidate() { if (parent::beforeValidate()) { $validator = CValidator::createValidator('unique', $this, 'district_name', array( 'criteria' => array( 'condition'=>'city_id=:city_id', 'params'=>array( ':city_id'=>$this->city_id ) ) )); $validator2 = CValidator::createValidator('unique', $this, 'url', array( 'criteria' => array( 'condition'=>'city_id=:city_id', 'params'=>array( ':city_id'=>$this->city_id ) ) )); $this->getValidatorList()->insertAt(0, $validator); $this->getValidatorList()->insertAt(0, $validator2); return true; } return false; }
public function localConfigValueValidator($attribute, $params) { if ($this->type == self::TYPE_INT) { $numericValidator = CValidator::createValidator('CNumberValidator', $this, $attribute, array('allowEmpty' => false, 'integerOnly' => true)); $numericValidator->validate($this); } }
public function actionAdmin($id) { $model = new ContestEntry(); $model->contest_id = $id; if (!($contest = Contest::model()->findByPk($id))) { throw new CHttpException(404); } $fieldsValidators = ''; $i = 0; foreach (ContestField::model()->with(array('multi' => array('index' => 'id')))->findAllByAttributes(array('contest_id' => $id)) as $field) { if ($field->result && $field->type != 'title') { $this->fields[] = array('title' => $field->title, 'type' => $field->type, 'multi' => $field->multi, 'id' => $field->id); $fieldsValidators .= 'field_' . $i . ', '; if (isset($_GET['ContestEntry'])) { $model->{'field_' . $i} = $_GET['ContestEntry']['field_' . $i]; } $i++; } } $model->fields = $this->fields; $validators = $model->getValidatorList(); $validators->add(CValidator::createValidator('safe', $this, substr($fieldsValidators, 0, -2))); if (isset($_GET['ContestEntry'])) { $model->id = $_GET['ContestEntry']['id']; $model->created_at = $_GET['ContestEntry']['created_at']; } $this->render('admin', array('model' => $model, 'contest' => $contest)); }
/** * {@inheritDoc} * @see CValidator::validateAttribute() */ public function validateAttribute($object, $attribute) { $array_to_validate = $object->{$attribute}; if (!is_array($array_to_validate)) { $this->addError($object, $attribute, Yii::t('validator.array', 'The attribute "{attr}" is not an array.', array('{attr}' => $attribute))); return; } if ($this->minElements !== null && count($array_to_validate) < $this->minElements) { $this->addError($object, $attribute, Yii::t('validator.array', '{attribute} should contains at least {n} elements.', array('{n}' => $this->minElements))); return; } if ($this->maxElements !== null && count($array_to_validate) > $this->maxElements) { $this->addError($object, $attribute, Yii::t('validator.array', '{attribute} should contains at max {n} elements.', array('{n}' => $this->maxElements))); return; } foreach ($this->rules as $rule) { $name = $rule[0]; unset($rule[0]); $validated_values = array(); // extract the values to validate from object foreach ($array_to_validate as $key => $value) { $object->{$attribute} = $value; // for each value, set the attribute at this specific value $validator = CValidator::createValidator($name, $object, array($attribute), $rule); $validator->validate($object, array($attribute)); // get the validated values into another array $validated_values[$key] = $object->{$attribute}; } // set back the attribute array with the validated attributes $object->{$attribute} = $validated_values; } }
public function actionCreate() { $model = new AttendanceAbsences(); $modelPhoto = new ImportForm(); // Uncomment the following line if AJAX validation is needed // $this->performAjaxValidation($model); if (isset($_POST['AttendanceAbsences'])) { $model->attributes = $_POST['AttendanceAbsences']; $modelPhoto->attributes = $_POST['ImportForm']; $imgFile = CUploadedFile::getInstance($modelPhoto, 'file'); if ($_POST['command'] == 'save') { $model->status = 'approved'; } if ($model->doctor_note == 1) { if (count($imgFile) == 0) { $model->validatorList->add(CValidator::createValidator('letter_required', $model, 'doctor_letter_proof')); } } if ($model->save()) { if (count($imgFile) != 0) { $imgFile->saveAs(letterPaths() . $model->id . '.jpg'); } if ($_POST['command'] == 'save' && $model->type != 'Late') { $model->savePresence(); } $this->redirect(array('index')); } } $this->render('create', array('model' => $model, 'modelPhoto' => $modelPhoto)); }
public function attach($owner) { parent::attach($owner); // Set up translatedAttributes. foreach ($this->attributes as $attribute) { foreach (Yii::app()->languageManager->suffixes as $suffix) { $this->_translatedAttributes[] = $attribute . $suffix; } } foreach ($this->_translatedAttributes as $varName) { $this->{$varName} = null; } // Copying validation rules. $rules = $owner->rules(); $validators = $owner->getValidatorList(); foreach (array_keys(Yii::app()->languageManager->languages) as $l) { if ($l != Yii::app()->sourceLanguage) { foreach ($this->attributes as $attr) { foreach ($rules as $rule) { $ruleAttributes = array_map('trim', explode(',', $rule[0])); if (in_array($attr, $ruleAttributes)) { $validators->add(CValidator::createValidator($rule[1], $this, $attr . '_' . $l, array_slice($rule, 2))); } } } } } }
public function myCaptcha($attr, $params) { if (Yii::app()->request->isAjaxRequest) { return; } CValidator::createValidator('captcha', $this, $attr, $params)->validate($this); }
/** * Проверка ссылки на уникальность * * @param string $attribute * @param array $params */ public function linkUnique($attribute, $params = array()) { if (!$this->hasErrors()) { $params['criteria'] = array('condition' => 'link = :link AND gs_id = :gs_id', 'params' => array('link' => $this->link, 'gs_id' => $this->gs_id)); $validator = CValidator::createValidator('unique', $this, $attribute, $params); $validator->validate($this, array($attribute)); } }
public function files($attribute, $params) { $validator = CValidator::createValidator('file', $this, $attribute, $params); foreach (CUploadedFile::getInstances($this, $attribute) as $file) { $this->{$attribute} = $file; $validator->validate($this, $attribute); } }
public function attach($owner) { $validators = $owner->getValidatorList(); foreach ($this->rules() as $rule) { $validators->add(CValidator::createValidator($rule[1], $owner, $rule[0], array_slice($rule, 2))); } parent::attach($owner); }
public function uniqueLogin($attribute, $params = array()) { if (!$this->hasErrors()) { $params['criteria'] = array('condition' => 'login=:login', 'params' => array(':login' => $this->login)); $validator = CValidator::createValidator('unique', $this, $attribute, $params); $validator->validate($this, array($attribute)); } }
public function attach($owner) { parent::attach($owner); if (in_array($owner->scenario, $this->scenarios)) { // добавляем валидатор файла $fileValidator = CValidator::createValidator('file', $owner, $this->attributeName, array('types' => $this->fileTypes, 'allowEmpty' => true)); $owner->validatorList->add($fileValidator); } }
/** * Создание валидатора для загружаемого изображения */ public function attach($owner) { parent::attach($owner); $this->simpleImage = Yii::app()->simpleImage; if (in_array($owner->scenario, $this->scenarios)) { $fileValidator = CValidator::createValidator('FileValidator', $owner, $this->attributeName, array('safe' => false, 'enableClientValidation' => true, 'allowEmpty' => true, 'maxSize' => $this->maxSize, 'types' => $this->fileTypes, 'mimeTypes' => $this->mimeTypes, 'tooLarge' => Yii::t('yii', 'Размер файла "{file}" слишком велик, он не должен превышать {limit}.'))); $owner->validatorList->add($fileValidator); } }
public function init() { parent::init(); if ($this->isInt()) { $this->model->addValidator(CValidator::createValidator('numerical', $this->model, $this->attributeName, array('on' => 'backendInsert, backendUpdate'))); } else { $this->model->addValidator(CValidator::createValidator('length', $this->model, $this->attributeName, array('on' => 'backendInsert, backendUpdate', 'max' => 255))); } }
/** * Checks the transport type. * This is the 'checkType' validator as declared in rules(). */ public function checkType($attribute, $params) { if ($this->type == 'smtp') { $requireValidator = CValidator::createValidator('required', $this, 'host, username, password'); $requireValidator->validate($this); $emailValidator = CValidator::createValidator('email', $this, 'username'); $emailValidator->validate($this); } }
public function init() { parent::init(); $cs = Yii::app()->clientScript; $cs->registerScriptFile(CHtml::asset(dirname(__FILE__) . '/../assets/BackendUploadedFiles.js')); if ($this->getObjectParameter()->isRequired()) { $this->model->getValidatorList()->add(CValidator::createValidator('required', $this->model, array($this->getObjectParameter()->getFieldName()), array('on' => 'backendInsert, backendUpdate'))); } }
public function validateValue() { if (parent::validateValue()) { $validator = CValidator::createValidator('type', $this, 'value', array('type' => 'integer')); $validator->allowEmpty = false; $validator->validate($this); return !$this->hasErrors(); } return false; }
/** * Anchor or title requires title field. */ public function beforeValidate($event) { $owner = $this->getOwner(); $this->_validatorIndexes = array(); if ($this->title_anchor || $this->title_page) { foreach (Yii::app()->languageManager->suffixes as $suffix) { $this->_validatorIndexes[] = $this->_validators->add(CValidator::createValidator('required', $owner, 'title' . $suffix)); } } $this->_validatorIndexes = array_reverse($this->_validatorIndexes); }
public function __construct(array $options = array()) { parent::__construct($options); $valueTypesAll = array(ITEM_VALUE_TYPE_FLOAT => true, ITEM_VALUE_TYPE_UINT64 => true, ITEM_VALUE_TYPE_STR => true, ITEM_VALUE_TYPE_TEXT => true, ITEM_VALUE_TYPE_LOG => true); $valueTypesNum = array(ITEM_VALUE_TYPE_FLOAT => true, ITEM_VALUE_TYPE_UINT64 => true); $valueTypesChar = array(ITEM_VALUE_TYPE_STR => true, ITEM_VALUE_TYPE_TEXT => true, ITEM_VALUE_TYPE_LOG => true); $valueTypesLog = array(ITEM_VALUE_TYPE_LOG => true); $valueTypesInt = array(ITEM_VALUE_TYPE_UINT64 => true); $argsIgnored = array(array('type' => 'str')); $this->allowed = array('abschange' => array('args' => $argsIgnored, 'value_types' => $valueTypesAll), 'avg' => array('args' => array(array('type' => 'sec_num', 'mandat' => true), array('type' => 'sec_zero', 'can_be_empty' => true)), 'value_types' => $valueTypesNum), 'band' => array('args' => array(array('type' => 'sec_num_zero', 'mandat' => true, 'can_be_empty' => true), array('type' => 'num', 'mandat' => true), array('type' => 'sec_zero', 'can_be_empty' => true)), 'value_types' => $valueTypesInt), 'change' => array('args' => $argsIgnored, 'value_types' => $valueTypesAll), 'count' => array('args' => array(array('type' => 'sec_num', 'mandat' => true), array('type' => 'str'), array('type' => 'operation'), array('type' => 'sec_zero', 'can_be_empty' => true)), 'value_types' => $valueTypesAll), 'date' => array('args' => $argsIgnored, 'value_types' => $valueTypesAll), 'dayofmonth' => array('args' => $argsIgnored, 'value_types' => $valueTypesAll), 'dayofweek' => array('args' => $argsIgnored, 'value_types' => $valueTypesAll), 'delta' => array('args' => array(array('type' => 'sec_num', 'mandat' => true), array('type' => 'sec_zero', 'can_be_empty' => true)), 'value_types' => $valueTypesNum), 'diff' => array('args' => $argsIgnored, 'value_types' => $valueTypesAll), 'fuzzytime' => array('args' => array(array('type' => 'sec_zero', 'mandat' => true)), 'value_types' => $valueTypesNum), 'iregexp' => array('args' => array(array('type' => 'str', 'mandat' => true), array('type' => 'sec_num', 'can_be_empty' => true)), 'value_types' => $valueTypesChar), 'last' => array('args' => array(array('type' => 'sec_num_zero', 'mandat' => true, 'can_be_empty' => true), array('type' => 'sec_zero', 'can_be_empty' => true)), 'value_types' => $valueTypesAll), 'logeventid' => array('args' => array(array('type' => 'str', 'mandat' => true)), 'value_types' => $valueTypesLog), 'logseverity' => array('args' => $argsIgnored, 'value_types' => $valueTypesLog), 'logsource' => array('args' => array(array('type' => 'str', 'mandat' => true)), 'value_types' => $valueTypesLog), 'max' => array('args' => array(array('type' => 'sec_num', 'mandat' => true), array('type' => 'sec_zero', 'can_be_empty' => true)), 'value_types' => $valueTypesNum), 'min' => array('args' => array(array('type' => 'sec_num', 'mandat' => true), array('type' => 'sec_zero', 'can_be_empty' => true)), 'value_types' => $valueTypesNum), 'nodata' => array('args' => array(array('type' => 'sec_zero', 'mandat' => true)), 'value_types' => $valueTypesAll), 'now' => array('args' => $argsIgnored, 'value_types' => $valueTypesAll), 'prev' => array('args' => $argsIgnored, 'value_types' => $valueTypesAll), 'regexp' => array('args' => array(array('type' => 'str', 'mandat' => true), array('type' => 'sec_num', 'can_be_empty' => true)), 'value_types' => $valueTypesChar), 'str' => array('args' => array(array('type' => 'str', 'mandat' => true), array('type' => 'sec_num', 'can_be_empty' => true)), 'value_types' => $valueTypesChar), 'strlen' => array('args' => array(array('type' => 'sec_num_zero', 'mandat' => true, 'can_be_empty' => true), array('type' => 'sec_zero', 'can_be_empty' => true)), 'value_types' => $valueTypesChar), 'sum' => array('args' => array(array('type' => 'sec_num', 'mandat' => true), array('type' => 'sec_zero', 'can_be_empty' => true)), 'value_types' => $valueTypesNum), 'time' => array('args' => $argsIgnored, 'value_types' => $valueTypesAll)); }
public function beforeValidate() { if (!$this->isNewRecord) { $this->_storedPassword = self::model()->findByPk($this->id)->password; } if ($this->isNewRecord && $this->password == '') { $validators = $this->getValidatorList(); $validators->add(CValidator::createValidator('required', $this, 'password')); } return parent::beforeValidate(); }
/** * Validate Rules * * @param BaseElementModel $element Element * * @return void */ private function validateRules(BaseElementModel $element) { foreach ($this->rules as $rule) { $name = $rule[1]; $attributes = $rule[0]; $params = array_slice($rule, 2); $validator = \CValidator::createValidator($name, $element, $attributes, $params); $validator->validate($element); } $this->addErrors($element->getErrors()); }
public function __construct(array $options = []) { parent::__construct($options); $valueTypesAll = [ITEM_VALUE_TYPE_FLOAT => true, ITEM_VALUE_TYPE_UINT64 => true, ITEM_VALUE_TYPE_STR => true, ITEM_VALUE_TYPE_TEXT => true, ITEM_VALUE_TYPE_LOG => true]; $valueTypesNum = [ITEM_VALUE_TYPE_FLOAT => true, ITEM_VALUE_TYPE_UINT64 => true]; $valueTypesChar = [ITEM_VALUE_TYPE_STR => true, ITEM_VALUE_TYPE_TEXT => true, ITEM_VALUE_TYPE_LOG => true]; $valueTypesLog = [ITEM_VALUE_TYPE_LOG => true]; $valueTypesInt = [ITEM_VALUE_TYPE_UINT64 => true]; $argsIgnored = [['type' => 'str']]; $this->allowed = ['abschange' => ['args' => $argsIgnored, 'value_types' => $valueTypesAll], 'avg' => ['args' => [['type' => 'sec_num', 'mandat' => true], ['type' => 'sec_zero', 'can_be_empty' => true]], 'value_types' => $valueTypesNum], 'band' => ['args' => [['type' => 'sec_num_zero', 'mandat' => true, 'can_be_empty' => true], ['type' => 'num', 'mandat' => true], ['type' => 'sec_zero', 'can_be_empty' => true]], 'value_types' => $valueTypesInt], 'change' => ['args' => $argsIgnored, 'value_types' => $valueTypesAll], 'count' => ['args' => [['type' => 'sec_num', 'mandat' => true], ['type' => 'str'], ['type' => 'operation'], ['type' => 'sec_zero', 'can_be_empty' => true]], 'value_types' => $valueTypesAll], 'date' => ['args' => $argsIgnored, 'value_types' => $valueTypesAll], 'dayofmonth' => ['args' => $argsIgnored, 'value_types' => $valueTypesAll], 'dayofweek' => ['args' => $argsIgnored, 'value_types' => $valueTypesAll], 'delta' => ['args' => [['type' => 'sec_num', 'mandat' => true], ['type' => 'sec_zero', 'can_be_empty' => true]], 'value_types' => $valueTypesNum], 'diff' => ['args' => $argsIgnored, 'value_types' => $valueTypesAll], 'forecast' => ['args' => [['type' => 'sec_num', 'mandat' => true], ['type' => 'sec_zero', 'can_be_empty' => true], ['type' => 'sec_zero', 'mandat' => true], ['type' => 'str', 'can_be_empty' => true], ['type' => 'str', 'can_be_empty' => true]], 'value_types' => $valueTypesNum], 'fuzzytime' => ['args' => [['type' => 'sec_zero', 'mandat' => true]], 'value_types' => $valueTypesNum], 'iregexp' => ['args' => [['type' => 'str', 'mandat' => true], ['type' => 'sec_num', 'can_be_empty' => true]], 'value_types' => $valueTypesChar], 'last' => ['args' => [['type' => 'sec_num_zero', 'mandat' => true, 'can_be_empty' => true], ['type' => 'sec_zero', 'can_be_empty' => true]], 'value_types' => $valueTypesAll], 'logeventid' => ['args' => [['type' => 'str', 'mandat' => true]], 'value_types' => $valueTypesLog], 'logseverity' => ['args' => $argsIgnored, 'value_types' => $valueTypesLog], 'logsource' => ['args' => [['type' => 'str', 'mandat' => true]], 'value_types' => $valueTypesLog], 'max' => ['args' => [['type' => 'sec_num', 'mandat' => true], ['type' => 'sec_zero', 'can_be_empty' => true]], 'value_types' => $valueTypesNum], 'min' => ['args' => [['type' => 'sec_num', 'mandat' => true], ['type' => 'sec_zero', 'can_be_empty' => true]], 'value_types' => $valueTypesNum], 'nodata' => ['args' => [['type' => 'sec_zero', 'mandat' => true]], 'value_types' => $valueTypesAll], 'now' => ['args' => $argsIgnored, 'value_types' => $valueTypesAll], 'percentile' => ['args' => [['type' => 'sec_num', 'mandat' => true], ['type' => 'sec_zero', 'can_be_empty' => true], ['type' => 'percent', 'mandat' => true]], 'value_types' => $valueTypesNum], 'prev' => ['args' => $argsIgnored, 'value_types' => $valueTypesAll], 'regexp' => ['args' => [['type' => 'str', 'mandat' => true], ['type' => 'sec_num', 'can_be_empty' => true]], 'value_types' => $valueTypesChar], 'str' => ['args' => [['type' => 'str', 'mandat' => true], ['type' => 'sec_num', 'can_be_empty' => true]], 'value_types' => $valueTypesChar], 'strlen' => ['args' => [['type' => 'sec_num_zero', 'mandat' => true, 'can_be_empty' => true], ['type' => 'sec_zero', 'can_be_empty' => true]], 'value_types' => $valueTypesChar], 'sum' => ['args' => [['type' => 'sec_num', 'mandat' => true], ['type' => 'sec_zero', 'can_be_empty' => true]], 'value_types' => $valueTypesNum], 'time' => ['args' => $argsIgnored, 'value_types' => $valueTypesAll], 'timeleft' => ['args' => [['type' => 'sec_num', 'mandat' => true], ['type' => 'sec_zero', 'can_be_empty' => true], ['type' => 'num', 'mandat' => true], ['type' => 'str', 'can_be_empty' => true]], 'value_types' => $valueTypesNum]]; }
protected function addValidators() { $validators = new CList(); foreach ($this->rules() as $rule) { if (isset($rule[0], $rule[1])) { $validator = CValidator::createValidator($rule[1], $this->_model, $rule[0], array_slice($rule, 2)); $validators->add($validator); } else { throw new CException(Yii::t('yii', '{class} has an invalid validation rule. The rule must specify attributes to be validated and the validator name.', array('{class}' => get_class($this)))); } } return $validators; }
public function init() { parent::init(); if (empty($this->secret)) { if (!empty(Yii::$app->reCaptcha->secret)) { $this->secret = Yii::$app->reCaptcha->secret; } else { throw new Exception('Required `secret` param isn\'t set.'); } } if ($this->message === null) { $this->message = Yii::t('yii', 'The verification code is incorrect.'); } }
/** * {@inheritDoc} * @see CValidator::validateAttribute() */ public function validateAttribute($object, $attribute) { $validate = false; foreach ($this->rules as $validatorConfig) { $class = $validatorConfig[0]; unset($validatorConfig[0]); $vobject = CValidator::createValidator($class, $object, $this->attributes, $validatorConfig); if ($vobject->validate($object, $attribute)) { $validate = true; } } if ($validate) { $object->clearErrors($attribute); } }
/** * Value can either be dateTime or if dynamic, then it is an integer * @return bool */ public function validateValue() { if (parent::validateValue()) { if ($this->type == self::TYPE_STATIC) { $validator = CValidator::createValidator('TypeValidator', $this, 'value', array('type' => 'datetime')); $validator->validate($this); return !$this->hasErrors(); } else { $validator = CValidator::createValidator('CRequiredValidator', $this, 'durationInterval'); $validator->validate($this); return !$this->hasErrors(); } } return false; }
function testErrors() { $model = new NewModel(); $model->attr1 = 3; $model->validatorList->add(CValidator::createValidator('required', $model, 'attr2', array())); $model->validatorList->add(CValidator::createValidator('required', $model, 'attr4', array())); $model->validate(); $this->assertTrue($model->hasErrors()); $this->assertTrue($model->hasErrors('attr2')); $this->assertFalse($model->hasErrors('attr1')); $model->clearErrors('attr2'); $this->assertFalse($model->hasErrors('attr2')); $model->clearErrors(); $this->assertFalse($model->hasErrors()); }
/** * Value can either be a string or if dynamic step, then it is an integer * @return bool */ public function validateValue() { if (parent::validateValue()) { if ($this->type == self::TYPE_STATIC) { $validator = CValidator::createValidator('type', $this, 'value', array('type' => 'string')); $validator->validate($this); return !$this->hasErrors(); } else { $validator = CValidator::createValidator('type', $this, 'alternateValue', array('type' => 'integer')); $validator->allowEmpty = false; $validator->validate($this); return !$this->hasErrors(); } } return false; }
public function createValidators() { $validators = new CList(); $rules = $this->rules(); if ($this->addDefaultRules) { $rules = array_merge($rules, $this->defaultRules()); } foreach ($rules as $rule) { if (isset($rule[0], $rule[1])) { // attributes, validator name $validators->add(CValidator::createValidator($rule[1], $this, $rule[0], array_slice($rule, 2))); } else { throw new CException(Yii::t('yii', '{class} has an invalid validation rule. The rule must specify attributes to be validated and the validator name.', array('{class}' => get_class($this)))); } } return $validators; }