Answer the question
In order to leave comments, you need to log in
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,
]);
}
Answer the question
In order to leave comments, you need to log in
You just have to read the documentation. The addRule method takes three arguments
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question