validate4.7小结获取不到自定义的非法信息?
来源:4-7 工欲善其事必先利器:构建接口参数校验层
 
			leeu
2019-06-12
我调用了自定义的goCheck()方法之后如果是id是整数就返回了true但是不是正整数的时候就没有抛出正确的异常比如id必须是正整数却抛出了一个数字0我打断点$this->error属性也是0这是怎么回事
id是整数的时候
id不是整数
代码如下
banner.php
public function getBanner($id){
    (new IDMustBePositiveInt()) -> goCheck();
   }
IDMustBePositiveInt.php
<?php
/**
 * Created by PhpStorm.
 * User: li
 * Date: 2019/6/12
 * Time: 7:07
 */
namespace appalidate;
class IDMustBePositiveInt extends BaseValidate
{
    protected $rule = [
        'id' => 'require|isPositiveInteger'
    ];
    protected function isPositiveInteger($value,$rule = '', $data = '', $field = ''){
        if(is_numeric($value) && is_int($value + 0) && ($value + 0) > 0){
            return true;
        }else{
            return $field+'必须是正整数';
        }
    }
}
BaseValidate.php
<?php
/**
 * Created by PhpStorm.
 * User: li
 * Date: 2019/6/12
 * Time: 20:17
 */
namespace appalidate;
use thinkException;
use thinkRequest;
use thinkValidate;
class BaseValidate extends Validate
{
    public function goCheck(){
        //获取数据
        $request = Request::instance();
        $params = $request->param();
        //调用验证规则
        $ret = $this->check($params);
        if(!$ret){
            $error = $this->error;
            throw new Exception($error);
        }else{
            return true;
        }
    }
}
写回答
	1回答
- 
				  7七月 2019-06-13 这个看代码很难看出来,要调试。打个断点跟踪下应该就知道了。 032019-06-13
相似问题
