我知道您可以使用 inline validator 验证单个属性比如:
['country', 'validateCountry']
public function validateCountry($attribute, $params)
{
if (!in_array($this->$attribute, ['USA', 'Web'])) {
$this->addError($attribute, 'The country must be either "USA" or "Web".');
}
}
但是我该如何将多个属性传递给验证器呢? ...还是应该在验证器中通过 $this
引用它们?
最佳答案
而不是直接访问额外的字段,例如使用 $this->email
,您可以将额外的属性作为 params
中的字段传递,例如 compareValidator
工作即
['username', 'customValidator', 'params' => ['extraFields' => 'email']]
public function customValidator($attribute, $params) {
//access extrafields using $this->{$params['extraFields']}
}
关于php - 在 Yii 2 的内联验证器中验证多个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27168370/