/**
  * @covers \opus\base\behaviors\TimestampBehavior::inject
  */
 public function testUpdate()
 {
     $now = new Expression('NOW()');
     $this->mock->expects($this->once())->method('getIsNewRecord')->will($this->returnValue(false));
     $this->mock->expects($this->any())->method('hasAttribute')->will($this->returnValue(true));
     $this->mock->expects($this->once())->method('setAttribute')->with('up', $now);
     $this->mock->validate();
 }
Beispiel #2
0
 public function validate()
 {
     if (!$this->_instance->validate()) {
         DI::getImporter()->wrongModel = $this->_instance;
         throw new RowException($this->row, 'Model data is not valid.');
     }
 }
    public function validate($attributeNames = null, $clearErrors = true){

        if(parent::validate($attributeNames, $clearErrors)){
            Auction::infoLog('Model :: ' . self::className() . ' Has Successfully Validated ', $this->getAttributes());
            return true;
        }else{
            Auction::errorLog('Model record :: '. self::className() .' not validated ',$this->getErrors());
            return false;
        }
    }
Beispiel #4
0
 public function validate($attributeNames = null, $clearErrors = true)
 {
     if (Tag::findOne(['name' => $this->name, 'user_id' => $this->user_id])) {
         $this->addError('name', 'Данный тег уже существует');
     }
     if (mb_strpos($this->name, ' ', null, 'utf-8') != false) {
         $this->addError('name', 'Тег не должен содержать пробелы');
     }
     return parent::validate($attributeNames, false);
 }
 /**
  * @param ActiveRecord $model
  * @param array $opts
  * @return ActiveQuery | array
  */
 static function searchQuery($model, $opts = [])
 {
     $opts = ArrayHelper::merge(['data' => null, 'query' => null, 'columns' => [], 'filters' => []], $opts);
     $columns = $opts['columns'];
     $filters = $opts['filters'];
     $data = $opts['data'];
     if (null === $data) {
         $data = \Yii::$app->request->get();
     }
     $query = $opts['query'];
     if (is_string($query)) {
         $query = call_user_func([$model, $opts['query']]);
     } elseif (null === $query) {
         $query = $model->find();
         foreach (array_filter($model->getAttributes()) as $prop => $val) {
             $query->andWhere([$prop => $val]);
         }
     }
     if ($model->load($data) && $model->validate()) {
         foreach ($model->getAttributes($model->safeAttributes()) as $name => $value) {
             if ($model->isAttributeChanged($name)) {
                 $attributeTypes = [];
                 if (method_exists($model, 'attributeTypes')) {
                     $attributeTypes = $model->attributeTypes();
                 }
                 $type = null;
                 if (isset($attributeTypes[$name])) {
                     $type = $attributeTypes[$name];
                 }
                 // Default filter function
                 $filterFunc = isset($filters[$name]) && is_callable($filters[$name]) ? $filters[$name] : function (ActiveQuery $query, $name, $value, $type) {
                     /**
                      * @var string $name
                      * @var string|array $value
                      * @var string $type
                      */
                     $query->andFilterWhere(static::searchAttribute($name, $value, $type));
                 };
                 if (isset($columns[$name])) {
                     $name = $columns[$name];
                 }
                 call_user_func($filterFunc, $query, $name, $value, $type);
             }
         }
     }
     return $query;
 }
Beispiel #6
0
 /**
  * @param ActiveRecord $model
  * @return string|\yii\web\Response
  */
 protected function editModel($model)
 {
     // validate() && save(false) because of CantSave exception
     if ($model->load(Yii::$app->request->post()) && $model->validate() && $model->save(false)) {
         return $this->redirect($this->getReturnUrl());
     } else {
         return $this->render($this->formView, ['model' => $model, 'returnUrl' => $this->getReturnUrl(), 'modelTitleForms' => $this->modelTitleForms, 'formElements' => $this->getFormElements($model)]);
     }
 }
Beispiel #7
0
    public function update($runValidation = true, $attributeNames = null){
        $this->image = UploadedFile::getInstance($this->user0, 'profile_pic');

        if(!parent::validate())
            return false;

        if($this->image instanceof  UploadedFile){

            if(!getimagesize($this->image->tempName)){
                $this->addError('image','Please Upload a valid Image');
                return false;
            }

            Auction::info('Image Upload Event Triggered');
            $this->trigger(Events::UPLOAD_IMAGE);

            parent::update(false);
            $this->user0->profile_pic = $this->image;
        }
        else{
            $this->user0->profile_pic = $this->user0->oldAttributes['profile_pic'];
        }

        if($this->user0->save(false)) {
            Auction::info('Dealer is successsfully Updated');
            return true;
        }else{
            Auction::infoLog('Dealer is not updated Due to following validation Errors',$this->user0->getErrors());
            return false;
        }
    }
Beispiel #8
0
 /**
  * need to validate the relation models
  * @inheritdoc
  */
 public function validate($attributes = null, $clearErrors = true)
 {
     $isValid = parent::validate($attributes, $clearErrors);
     $relatedAttributes = [];
     if ($attributes) {
         foreach ($attributes as $attribute) {
             if (($pos = strpos($attribute, '.')) !== false) {
                 $relatedName = substr($attributes, 0, $pos + 1);
                 $relatedAttribute = substr($attributes, $pos + 2);
                 if (!isset($relatedAttributes[$relatedName])) {
                     $relatedAttributes[$relatedName] = [];
                 }
                 $relatedAttributes[$relatedName][] = $relatedAttribute;
             }
         }
     }
     foreach ($this->_relations as $name => $models) {
         $modelAttributes = isset($relatedAttributes[$name]) ? $relatedAttributes[$name] : null;
         if (is_array($models)) {
             foreach ($models as $model) {
                 /** @var \yii\db\ActiveRecord $model */
                 if (!$model->validate($modelAttributes, $clearErrors)) {
                     $isValid = false;
                     $this->addError($name, $model->getErrors());
                 }
             }
         } else {
             /** @var \yii\db\ActiveRecord $models */
             if (!$models->validate($modelAttributes, $clearErrors)) {
                 $isValid = false;
                 $this->addError($name, $models->getErrors());
             }
         }
     }
     return $isValid;
 }
 /**
  * need to validate the relation models
  * @inheritdoc
  */
 public function validate($attributes = null, $clearErrors = true)
 {
     $isValid = true;
     if (!parent::validate($attributes, $clearErrors)) {
         $isValid = false;
     }
     foreach ($this->_relations as $name => $models) {
         if (is_array($models)) {
             foreach ($models as $model) {
                 /** @var \yii\db\ActiveRecord $model */
                 if (!$model->validate(null, $clearErrors)) {
                     $isValid = false;
                     $this->addError($name, $model->getErrors());
                 }
             }
         } else {
             /** @var \yii\db\ActiveRecord $models */
             if (!$models->validate(null, $clearErrors)) {
                 $isValid = false;
                 $this->addError($name, $models->getErrors());
             }
         }
     }
     return $isValid;
 }
Beispiel #10
0
 /**
  * Присваивает выбранное значение из справочника модели, в сессии.
  * При выборе значения из справочника, значение присваивается в сессию предыдущей хлебной крошки, для формы, с которой был открыт справочник.
  * @param bool $RedirectPreviousUrl
  * @param ActiveRecord $ActiveRecord Модель к которой присваивается знаечния из справочника.
  * @param string $AttributeForeignID Имя атрибута
  * @return string
  */
 public static function AssignToModelFromGrid($RedirectPreviousUrl = True, $ActiveRecord = NULL, $AttributeForeignID = NULL)
 {
     if (Yii::$app->request->isAjax) {
         $LastBC = Proc::GetLastBreadcrumbsFromSession();
         $assigndata = filter_input(INPUT_POST, 'assigndata');
         $foreign = isset($LastBC['dopparams']['foreign']) ? $LastBC['dopparams']['foreign'] : '';
         if (!empty($foreign) && !empty($assigndata)) {
             $BC = Proc::GetBreadcrumbsFromSession();
             end($BC);
             prev($BC);
             $BC[key($BC)]['dopparams'][$foreign['model']][$foreign['field']] = $assigndata;
             $session = new Session();
             $session->open();
             $session['breadcrumbs'] = $BC;
             $session->close();
             if ($ActiveRecord instanceof ActiveRecord && is_string($AttributeForeignID)) {
                 $field = $LastBC['dopparams']['foreign']['field'];
                 if ($ActiveRecord->formName() === $LastBC['dopparams']['foreign']['model']) {
                     $ActiveRecord->{$field} = $assigndata;
                     $ActiveRecord->{$AttributeForeignID} = $foreign['id'];
                     if ($ActiveRecord->validate()) {
                         $ActiveRecord->save(false);
                     }
                 }
             }
             if ($RedirectPreviousUrl) {
                 Yii::$app->response->redirect(Proc::GetPreviousURLBreadcrumbsFromSession());
             }
         } else {
             return 'error foreign or assigndata empty AssignToModelFromGrid()';
         }
     }
 }
Beispiel #11
0
 public function validate($attributeNames = null, $clearErrors = true)
 {
     foreach ($this->getRelatedRecords() as $relationName => $relatedRecord) {
         $relation = $this->getRelation($relationName);
         if ($relation->multiple) {
             if (empty($relatedRecord)) {
                 continue;
             }
             /** @var Model[] $relatedRecord */
             $attributes = $relatedRecord[0]->getAttributes();
             foreach ($relation->link as $key => $value) {
                 unset($attributes[$key]);
             }
             if (!$this->validateMultiple($relatedRecord, $attributes)) {
                 foreach ($relatedRecord as $related) {
                     $this->addError($relationName, $related->getErrors());
                 }
                 return false;
             }
         } else {
             if (!$relatedRecord instanceof Model) {
                 continue;
             }
             /** @var Model $relatedRecord */
             if (!$relatedRecord->validate()) {
                 $this->addError($relationName, $relatedRecord->getErrors());
                 return false;
             }
         }
     }
     return parent::validate($attributeNames, $clearErrors);
 }