Yii2.0中rules验证的调试

时间:2019-05-20 01:45:15   收藏:0   阅读:150
通过表单模型做接口,验证返回的错误明确错误信息如下:

/**

  • 提取rules规则验证错误的信息
  • @param [type] $model
  • @return void
    */
    public static function getModelError($model) {
    if(empty($model)) return true;
    $errors = $model->getErrors(); //得到所有的错误信息

    if(!is_array($errors)){
        return true;
    }
    
    $firstError = array_shift($errors);
    if(!is_array($firstError)) {
        return true;
    }
    return array_shift($firstError);

    }

调试的过程中通过打断点到$this->save() load() validate(),然后查看获取的
$this->errors、 $model->getErrors()会获取到详细的错误信息以方便一下步的调试

原文:https://blog.51cto.com/13238147/2397108

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!