/** * @inheritdoc */ public function run() { foreach ($this->model->getErrors() as $attribute => $messages) { foreach ($messages as $key => $message) { $this->options['id'] = 'error-' . $attribute . '-' . $key; echo \yii\bootstrap\Alert::widget(['body' => $message, 'closeButton' => $this->closeButton, 'options' => $this->options]); } } }
/** * @param ActiveRecord $model */ public function addErrorMessagesFromModel(ActiveRecord $model) { foreach ($model->getErrors() as $fieldWithErrors) { foreach ($fieldWithErrors as $error) { $this->addMessage(null, $error, Message::ALERT); } } }
/** * @param \yii\db\ActiveRecord $Model * @param string $default_attribute */ public function populateErrors(\yii\db\ActiveRecord $Model, $default_attribute) { $errors = $Model->getErrors(); foreach ($errors as $key => $messages) { $attribute = $key; if (false === $this->hasProperty($attribute)) { if (!method_exists($this, 'hasAttribute')) { $attribute = $default_attribute; } elseif (false === $this->hasAttribute($attribute)) { $attribute = $default_attribute; } } foreach ($messages as $mes) { $this->addError($attribute, $mes); } } }
/** * @param ActiveRecord $model * @param string $defaultAttribute * @param array $attributesMap */ public function populateErrors(ActiveRecord $model, $defaultAttribute, $attributesMap = []) { /** @var Model $this */ $errors = $model->getErrors(); foreach ($errors as $attribute => $messages) { $attribute = isset($attributesMap[$attribute]) ? $attributesMap[$attribute] : $attribute; if (false === $this->hasProperty($attribute)) { if (!method_exists($this, 'hasAttribute')) { $attribute = $defaultAttribute; } elseif (false === $this->hasAttribute($attribute)) { $attribute = $defaultAttribute; } } foreach ($messages as $mes) { $this->addError($attribute, $mes); } } }
/** * Converts the model validation errors to a readable format an throws it as an exception * @param ActiveRecord $model * @throws Exception */ protected function throwInvalidModelException($model) { $errorMessage = Yii::t('language', 'Invalid model "{model}":', ['model' => $model->className()]); foreach ($model->getErrors() as $attribute => $errors) { $errorMessage .= "\n {$attribute}: " . join(', ', $errors); } throw new Exception($errorMessage); }
/** * Run Create/Read/Update/Delete action * * Set error flash and throw Exception, if action failed * Return nothing if action succeed * * @param \yii\db\ActiveRecord|\yii\db\ActiveRecord[] $model * @return void * @throws \Exception */ protected function runCrudAction($model) { if ($model instanceof Model) { if (!call_user_func([$model, $this->modelAction])) { if ($model->hasErrors()) { $this->flashError = []; foreach ($model->getErrors() as $errors) { $this->flashError = array_merge($this->flashError, $errors); } } throw new \Exception($this->flashError ? VarDumper::dumpAsString($this->flashError) : 'Unknown error'); } return; } foreach ($model as $concreteModel) { $this->runCrudAction($concreteModel); } }
/** * @param ActiveRecord $model * @return ActiveRecord * @throws \yii\web\ServerErrorHttpException */ protected function chainDelete($model) { if (!$model->delete()) { if (YII_DEBUG) { Yii::$app->response->statusCode = 500; Yii::$app->response->format = Response::FORMAT_JSON; var_dump($model->getErrors()); Yii::$app->end(); } throw new ServerErrorHttpException('Do not able to save the model.'); } return $model; }
/** * @param \yii\db\ActiveRecord $model */ public function addValidationErrors($model) { $keyValue = $this->composeKey($model->getPrimaryKey(true)); $this->_errors['validation'][$keyValue] = $model->getErrors(); }