thinkphp验证器
thinkphp 验证器一般是验证表单,验证通过就执行操作,不通过就通过json返回错误信息。比一个个验证正则很方便
use think\Validate; //先导验证器类
$data = $_POST; //清除首尾空格 foreach ($data as $key => $value) { $data[$key] = trim($value); } //验证表单 $validate = new Validate([ 'name'=>'require|min:2|max:100', 'title'=>'require|min:2|max:100', 'old_price' =>'require|number', 'price'=>'require|number', 'stock'=>'require|number', 'warnstock'=>'require|number|<=:stock', //预警库存小于库存 'status'=>'require|in:0,1', 'brand_id'=>'require|number', 'goodstype_id'=>'require|number', 'detail'=>'require', ]); if (!$validate->check($data)) { $this->error($validate->getError()); }