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