H
H
hollanditkzn2017-08-31 18:31:47
Yii
hollanditkzn, 2017-08-31 18:31:47

How to create your own validation for certain conditions?

I have 2 models, I need validation to check for certain conditions, I can’t implement it. Validation should check the amount that came in should not exceed the amount that is indicated for payment, so that there is no change. The deposit amount is specified by the user. In the controller, here's how I implemented

public function actionFinancy($oplata, $sum)
    {
        $validat = new DynamicModel(compact('oplata', 'sum'));
        $validat->addRule([['oplata', 'sum'], 'number', 'when' => function($oplata, $sum){
            return $oplata <= $sum;
    }])->validate();

        if ($validat->hasErrors()){
            return 'Сумма превышает всего суммы';
        }
    }
public function actionDraft($id)
    {
        $model = $this->findModel($id);
        $financy = new Financy();
        $validate = $this->actionFinancy($model->oplata, $financy->sum);

        if ($financy->load(Yii::$app->request->post()) && $financy->validate()){
            if (!$financy->save()){
                print_r($financy->getErrors());
            } else {
                $model->fact_oplata = $model->fact_oplata + $financy->sum;
                if ($model->oplata === $model->fact_oplata){
                    $model->action = 0;
                    $model->save();
                    Yii::$app->session->addFlash('update', 'Заказ был закрыт');
                }
            }
            return $this->redirect(['admin']);
        }

        return $this->renderAjax('draft', [
            'model' => $model,
            'financy' => $financy,
        ]);
    }

I just get the error Missing argument 2 for yii\base\DynamicModel::addRule(), called in C:\OpenServer\domains\crm\frontend\controllers\ZakazController.php on line 990 and
defined

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
I
Ivan Koryukov, 2017-08-31
@MadridianFox

You just have to read the documentation. The addRule method takes three arguments

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question