0

I have a junction table Znw with 2 ids (a_id & wz_id), and a form with 2 select boxes according. I would like to check in the rules, if fieldn of the corresponding model a have the same value as field of the corresponding model wz.

Rules:

[['a_id', 'wz_id'], 'compareFields', 'on' => 'createwz', 'params' => []],

custom validation function (now only to check if the correct value is found):

public function compareFields($attribute, $params) {
    $this->addError($attribute, $this->a->fieldn);
    return false;
}

relation a:

public function getA() {
    return $this->hasOne(\app\models\A::className(), ['id' => 'a_id']);
}

this relation is otherwise working well in views etc.

relation wz:

public function getWz() {
    return $this->hasOne(\app\models\Wz::className(), ['id' => 'wz_id']);
;}

controller:

public function actionCreatewz() {
    $model = new Znw;
    $a = A::find()->where(['id' => $_GET['Znw']['a_id']])->one();

    $model->scenario = 'createwz';

    try {
        if ($model->load($_POST) && $model->save()) {
            return $this->redirect(Url::previous());
        } elseif (!\Yii::$app->request->isPost) {
            $model->load($_GET);
        }
    } catch (\Exception $e) {
        $msg = (isset($e->errorInfo[2])) ? $e->errorInfo[2] : $e->getMessage();
        $model->addError('_exception', $msg);
    }
    return $this->render('createwz', [
                'model' => $model,
                'a' => $a,
    ]);
}

if I'm adding $this->wz->field to the custom validation function, it shows the right value. If I'm adding $this->a->fieldn, then I'm getting the following error:

Trying to get property of non-object

however, in the $_POST request both ids (a_id & wz_id) are there with the right value, so I should be able to see corresponding a model attribute. Why am I getting this error instead? Can you please point me to the right direction? Thanks in advance!

user2511599
  • 796
  • 1
  • 13
  • 38
  • Could you show us more code of controller? – Paul Dec 07 '17 at 09:01
  • From the view of your code and the error message, I guess that the attribute `a_id` of `$model` doesn't be assigned properly. It might be the results that you didn't make `a_id` safe in the scenario of `createwz`. You could verify this by `\Yii::warning($this->a_id)` in `compareFields`. – Paul Dec 07 '17 at 09:19
  • you mean like `$this->addError($attribute, \Yii::warning($this->a_id));` ? is empty. – user2511599 Dec 07 '17 at 09:32
  • Does `$model->a_id` has value before validate? – Paul Dec 07 '17 at 09:35
  • at least in the `$_POST` request it has a value, and after that in the `$model->load($_POST)` it's assigned correctly, so after that I see in `print_r($model)`: [_attributes:yii\db\BaseActiveRecord:private] => Array ( [a_id] => 34782 [wz_id] => 827 ) – user2511599 Dec 07 '17 at 09:55
  • but there is a strange thing also (maybe only for me): `[_related:yii\db\BaseActiveRecord:private] => Array ( [a] => )` it's empty. Is it a problem? Strangely I don't see such `related` for `wz`... – user2511599 Dec 07 '17 at 09:57
  • That means there is no record being found in model `A` with id equals 34782. – Paul Dec 07 '17 at 10:00
  • There is definitely a record with this id in `A`, because on the form the select shows already what in Table `a` can be found. – user2511599 Dec 07 '17 at 10:52
  • `$this->addError($attribute, \Yii::warning($this->a_id));` is not working. also not with `wz_id` – user2511599 Dec 07 '17 at 11:57
  • Just `\Yii::warning($this->a_id);` as a statement, then open the log file seeing what within. – Paul Dec 07 '17 at 12:03
  • you should show more code for the model where you are running the validation and have declared the relations there must be something wrong there. – Muhammad Omer Aslam Dec 07 '17 at 21:26
  • I have no clue what else could I show that could be relevant. There is not really much more. – user2511599 Dec 08 '17 at 08:21

0 Answers0