OK, I believe I will have many custom validations so I have decided to create a standalone validation class as per Yii Standalone Validation Doc.
This particular validator is to make sure either company_name or name is filled in, so Either Required.
I have created the class in app\components\validators\BothRequired.php
<?php
namespace app\components\validators;
use Yii;
use yii\validators\Validator;
class BothRequired extends Validator
{
public function validateAttribute($model, $attribute)
{
//validation code here
}
}
Here is the model
public function rules()
{
return [
['company_name', BothRequired::className(), 'skipOnEmpty' => false, ],
];
}
However this validation requires some params to be passed to the validation in this example I need to send the second attribute that is needed to be checked against. I cannot seem to work out how to do this, if I create the validation rule in the model itself then I can pass $params but i dont know how to pass to this standalone class.
Also on a further note, what would be a lot better for me is if I could have a class with all my custom validators in it rather than a file per validator.
Any ideas?
Regards
'attribute' => <value>in the rule (like the 'skipOnEmpty' one, which I bet is an attribute ofValidator. That seems like the kind of thing Yii would do. - gandaliter