- Scenario
$scenarios['sell'] = [ 'deposit'];
- Rule
['deposit', 'required', 'on' => 'sell',
'message' => 'Deposit cannot be blank',
'when' => function () {
if (Yii::$app->controller->id == 'transaction' && $this->deposite_date != null) {
return true;
}
}
]
I want to validate the deposit field only in transaction controller and I pass any value in the deposit_date field. otherwise I don't want to validate the deposit field. but the validation happens in other controller as well.
There is lot of code missing to understand how will be correct but I can try to make this.
First of all in controller action where you want to make this validation set some scenario. Example
Then in rules say that attribute is required on scenario that you created an no need for message because yii2 will generate for you (message you set if you need custom message text)
Hope that this helps.