Controller类增加batchValidate属性 validate方法增加batch参数,用于设置是否批量验证

异常类改进
This commit is contained in:
thinkphp
2016-06-13 11:09:42 +08:00
parent 00b0c0f229
commit b6b8b6dcb4
12 changed files with 85 additions and 12 deletions

View File

@@ -28,6 +28,8 @@ class Controller
protected $request; protected $request;
// 验证失败是否抛出异常 // 验证失败是否抛出异常
protected $failException = false; protected $failException = false;
// 是否批量验证
protected $batchValidate = false;
/** /**
* 前置操作方法列表 * 前置操作方法列表
@@ -162,10 +164,11 @@ class Controller
* @param array $data 数据 * @param array $data 数据
* @param string|array $validate 验证器名或者验证规则数组 * @param string|array $validate 验证器名或者验证规则数组
* @param array $message 提示信息 * @param array $message 提示信息
* @param bool $batch 是否批量验证
* @param mixed $callback 回调方法(闭包) * @param mixed $callback 回调方法(闭包)
* @return true|string|array * @return true|string|array
*/ */
protected function validate($data, $validate, $message = [], $callback = null) protected function validate($data, $validate, $message = [], $batch = false, $callback = null)
{ {
if (is_array($validate)) { if (is_array($validate)) {
$v = Loader::validate(); $v = Loader::validate();
@@ -180,12 +183,16 @@ class Controller
$v->scene($scene); $v->scene($scene);
} }
} }
// 是否批量验证
if($batch || $this->batchValidate){
$v->batch(true);
}
if (is_array($message)) { if (is_array($message)) {
$v->message($message); $v->message($message);
} }
if (is_callable($callback)) { if ($callback && is_callable($callback)) {
call_user_func_array($callback, [$v, &$data]); call_user_func_array($callback, [$v, &$data]);
} }

View File

@@ -291,7 +291,7 @@ class Loader
if (class_exists($class)) { if (class_exists($class)) {
$model = new $class(); $model = new $class();
} else { } else {
throw new ClassNotFoundException('class [ ' . $class . ' ] not exists'); throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class);
} }
} }
$_model[$name . $layer] = $model; $_model[$name . $layer] = $model;
@@ -327,7 +327,7 @@ class Loader
} elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) { } elseif ($empty && class_exists($emptyClass = self::parseClass($module, $layer, $empty, $appendSuffix))) {
return new $emptyClass(Request::instance()); return new $emptyClass(Request::instance());
} else { } else {
throw new ClassNotFoundException('class [ ' . $class . ' ] not exists'); throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class);
} }
} }
@@ -364,7 +364,7 @@ class Loader
if (class_exists($class)) { if (class_exists($class)) {
$validate = new $class; $validate = new $class;
} else { } else {
throw new ClassNotFoundException('class [ ' . $class . ' ] not exists'); throw new ClassNotFoundException('class [ ' . $class . ' ] not exists', $class);
} }
} }
$_instance[$name . $layer] = $validate; $_instance[$name . $layer] = $validate;
@@ -428,7 +428,7 @@ class Loader
$_instance[$identify] = $o; $_instance[$identify] = $o;
} }
} else { } else {
throw new ClassNotFoundException('class not exist :' . $class); throw new ClassNotFoundException('class not exist :' . $class, $class);
} }
} }
return $_instance[$identify]; return $_instance[$identify];

View File

@@ -94,7 +94,7 @@ class Session
// 检查驱动类 // 检查驱动类
if (!class_exists($class) || !session_set_save_handler(new $class($config))) { if (!class_exists($class) || !session_set_save_handler(new $class($config))) {
throw new ClassNotFoundException('error session handler'); throw new ClassNotFoundException('error session handler', $class);
} }
} }
if ($isDoStart) { if ($isDoStart) {

View File

@@ -1068,7 +1068,7 @@ class Template
$this->includeFile[$template] = filemtime($template); $this->includeFile[$template] = filemtime($template);
return $template; return $template;
} else { } else {
throw new TemplateNotFoundException('template not exist:' . $template); throw new TemplateNotFoundException('template not exist:' . $template, $template);
} }
} }

View File

@@ -1723,7 +1723,11 @@ class Query
} }
} }
} elseif (!empty($options['fail'])) { } elseif (!empty($options['fail'])) {
throw new DbException('Data not Found', $options, $sql); if(!empty($this->model)){
throw new ModelNotFoundException('Data not Found', $this->model, $options);
}else{
throw new DataNotFoundException('Data not Found', $options['table'], $options);
}
} }
return $resultSet; return $resultSet;
} }

View File

@@ -31,5 +31,13 @@ class DataNotFoundException extends DbException
$this->setData('Database Config', $config); $this->setData('Database Config', $config);
} }
/**
* 获取数据表名
* @access public
* @return string
*/
public function getTable()
{
return $this->table;
}
} }

View File

@@ -30,6 +30,11 @@ class ModelNotFoundException extends DbException
$this->setData('Database Config', $config); $this->setData('Database Config', $config);
} }
/**
* 获取模型类名
* @access public
* @return string
*/
public function getModel() public function getModel()
{ {
return $this->model; return $this->model;

View File

@@ -13,4 +13,20 @@ namespace think\exception;
class ClassNotFoundException extends \RuntimeException class ClassNotFoundException extends \RuntimeException
{ {
protected $class;
public function __construct($message,$class='')
{
$this->message = $message;
$this->class = $class;
}
/**
* 获取类名
* @access public
* @return string
*/
public function getClass()
{
return $this->class;
}
} }

View File

@@ -13,4 +13,21 @@ namespace think\exception;
class TemplateNotFoundException extends \RuntimeException class TemplateNotFoundException extends \RuntimeException
{ {
protected $template;
public function __construct($message,$template='')
{
$this->message = $message;
$this->template = $template;
}
/**
* 获取模板文件
* @access public
* @return string
*/
public function getTemplate()
{
return $this->template;
}
} }

View File

@@ -13,4 +13,20 @@ namespace think\exception;
class ValidateException extends \RuntimeException class ValidateException extends \RuntimeException
{ {
protected $error;
public function __construct($error)
{
$this->error = $error;
}
/**
* 获取验证错误信息
* @access public
* @return array|string
*/
public function getError()
{
return $this->error;
}
} }

View File

@@ -62,7 +62,7 @@ class Php
} }
// 模板不存在 抛出异常 // 模板不存在 抛出异常
if (!is_file($template)) { if (!is_file($template)) {
throw new TemplateNotFoundException('template file not exists:' . $template); throw new TemplateNotFoundException('template file not exists:' . $template, $template);
} }
// 记录视图信息 // 记录视图信息
APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');

View File

@@ -72,7 +72,7 @@ class Think
} }
// 模板不存在 抛出异常 // 模板不存在 抛出异常
if (!is_file($template)) { if (!is_file($template)) {
throw new TemplateNotFoundException('template file not exists:' . $template); throw new TemplateNotFoundException('template file not exists:' . $template, $template);
} }
// 记录视图信息 // 记录视图信息
APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info'); APP_DEBUG && Log::record('[ VIEW ] ' . $template . ' [ ' . var_export(array_keys($data), true) . ' ]', 'info');