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,特别重要,意思是值为空的时候不允许跳过验证。
本文详细介绍了在PHP中实现自定义验证规则的方法,特别是在onsite_contact等于2时,如何确保customer_manager_name和customer_manager_phone字段不为空。通过具体代码示例,展示了如何使用'skipOnError'和'skipOnEmpty'属性来控制验证流程。
1077

被折叠的 条评论
为什么被折叠?



