yii2.0 rules 验证自定义规则

public function rules()
{
    return [
        [['customer_manager_name', 'customer_manager_phone'],'customValidation','skipOnError' => false, 'skipOnEmpty'=>false]
    ];
}
public function customValidation($attribute, $params){
    if ($this->onsite_contact == 2)
    {
        if ($this->customer_manager_name === ''){
            $this->addError($attribute, "客户经理姓名的值不可以为空.");
        }
        if ($this->customer_manager_phone === ''){
            $this->addError($attribute, "客户经理电话的值不可以为空.");
        }
    }
}

以上是自定义验证规则的用法,当onsite_contact的值为2时,必填其他两个字段。其中'customValidation','skipOnError' => false, 'skipOnEmpty'=>false,特别重要,意思是值为空的时候不允许跳过验证。

你可能感兴趣的:(yii2.0)