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,特別重要,意思是值爲空的時候不允許跳過驗證。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章