Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

afterValidate #5

Open
Skinka opened this issue Apr 29, 2015 · 1 comment
Open

afterValidate #5

Skinka opened this issue Apr 29, 2015 · 1 comment
Assignees
Labels

Comments

@Skinka
Copy link

Skinka commented Apr 29, 2015

/**
 * @return void
 */
public function afterValidate()
{
    if (!Model::validateMultiple($this->owner->{$this->translationRelation})) {
        $this->owner->addError($this->translationRelation);
         foreach ($this->owner->{$this->translationRelation} as $model) {
            /** @var ActiveRecord $model */
            if ($model->hasErrors()){
                $this->owner->addErrors($model->getErrors());
            }
        }
   }
}

А то если валидация не проходит возвращается пустая ошибка от релейшена! а так будут ошибки модели возвращаться.

@creocoder creocoder self-assigned this May 15, 2015
@yurii-github
Copy link

yurii-github commented Nov 20, 2017

    public function afterValidate()
    {
        /** @var ActiveRecord[] $translations */
        $translations = $this->owner->{$this->translationRelation};
        $attributeNames = null;
        if (!empty($translations)) { // get 1st, all models are same
            $attributeNames = array_keys($translations[0]->getAttributes());
            $attributeNames = array_combine($attributeNames, $attributeNames); // key = value
            if ($this->owner->getIsNewRecord()) {
                foreach ($this->owner->getRelation($this->translationRelation)->link as $a => $b) {
                    //skip FK checks, our record will be saved later and we trust Yii2 it will have correct ID
                    unset($attributeNames[$a]);
                }
            }
        }

        if (!Model::validateMultiple($translations, $attributeNames)) {
            $errors = [];
            foreach ($translations as $model) {
                if ($model->hasErrors()){
                    $errors = $model->getErrors();
                }
            }

            $this->owner->addError($this->translationRelation, $errors);
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants