2 回答

TA貢獻1825條經驗 獲得超4個贊
//自定義驗證函數
protected function checkMobile($value)
{
# 01: 首先循環當前數據 每一項的值(value)
# 02: 之后將該值賦給 變量mobile
# 03: 之后使用寫驗證規則和錯誤信息
# 04: 之后將錯誤信息給$this->message();
# 05: 最后驗證 $this->check(驗證數據,驗證規則);
# 06: 最后判斷 如何為false 就返回錯誤信息 $this->getError();
foreach ($value as $item) {
$data['mobile'] = $item;
$rules = [
'mobile' => 'require|mobile',
];
$message = [
'mobile.require' => '手機號碼不得為空!',
'mobile.mobile' => '手機號碼格式錯誤!',
];
if (false === $this->message($message)->check($data, $rules)) {
return $this->getError();
} else {
return true;
}
}
}
使用的時候:
protected $rule = [ 'mobile' => 'require|checkMobile', ];
數據是:
$data = [ [ 'mobile'=> ['手機號碼1','手機號碼2'], ]; ];
- 2 回答
- 0 關注
- 2545 瀏覽
添加回答
舉報