加入我們要對客戶端傳遞過來的參數`id`做校驗,校驗規則是`必須是正整數`,編碼如下:
~~~
class IDMustBePositiveInteger extends BaseValidate
{
protected $rule = [
'id' => 'require|isPositiveInt'
];
protected $message = [
'id' => 'id必須是正整數'
];
}
~~~
其中,`isPositiveInt`是基類中的方法,具體請看上一小節,寫在基類中是便于以后其他子類中可以直接調用。
* * * * *
在`Controller`層中,比方我們要刪除某id的數據,可以這么使用
~~~
public function delSomenID() {
(new IDMustBePositiveInteger())->goCheck();
...........................
}
~~~
只有客戶端傳遞過來的id符合要求才會繼續執行后面代碼,否則拋出異常。