Skip to content

Commit ae314c1

Browse files
author
xiajianjun
committed
fix bug,add some comment
1 parent 701b27f commit ae314c1

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

src/ValidatorTrait.php

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,11 @@ public function beforeValidate(){}
119119
* @author inhere
120120
* @date 2015-08-11
121121
* @param array $onlyChecked 只检查一部分属性
122-
* @param boolean $hasErrorStop
122+
* @param boolean $hasErrorStop 出现错误即停止验证
123123
* @return static
124124
* @throws \RuntimeException
125125
*/
126-
public function validate(array $onlyChecked = [],$hasErrorStop=null)
126+
public function validate(array $onlyChecked = [], $hasErrorStop=false)
127127
{
128128
if ( !property_exists($this, 'data') ) {
129129
throw new \InvalidArgumentException('Must be defined property \'data (array)\' in the classes used.');
@@ -133,8 +133,7 @@ public function validate(array $onlyChecked = [],$hasErrorStop=null)
133133
return $this;
134134
}
135135

136-
$this->beforeValidate();
137-
$this->clearErrors();
136+
$this->clearErrors()->beforeValidate();
138137
$this->hasErrorStop($hasErrorStop);
139138

140139
// 循环规则
@@ -216,12 +215,17 @@ protected function doValidate($data, $name, $validator, $copy)
216215
if ($result && $validator !== 'required') {
217216
array_unshift($copy, $data[$name]);// 压入当前属性值
218217

218+
// if it's a closure
219219
if ( is_callable($validator) && $validator instanceof \Closure) {
220220
$result = call_user_func_array($validator, $copy);
221221
$validator = 'callback';
222+
223+
// if it is a method of the subclass.
222224
} elseif ( is_string($validator) && method_exists($this, $validator) ) {
223225

224226
$result = call_user_func_array( [ $this, $validator ] , $copy);
227+
228+
// it's a method of the class 'ValidatorList'
225229
} elseif ( is_callable([ValidatorList::class, $validator]) ) {
226230

227231
$result = call_user_func_array( [ ValidatorList::class, $validator ] , $copy);
@@ -260,19 +264,22 @@ protected function collectRules()
260264

261265
//////////////////////////////////// error info ////////////////////////////////////
262266

267+
/**
268+
* @return $this
269+
*/
263270
public function clearErrors()
264271
{
265272
$this->_errors = [];
273+
274+
return $this;
266275
}
267276

268277
/**
269-
* @param null|bool $val
278+
* @param bool $val
270279
*/
271280
public function hasErrorStop($val)
272281
{
273-
if ($val !== null ) {
274-
$this->_hasErrorStop = (bool)$val;
275-
}
282+
$this->_hasErrorStop = (bool)$val;
276283
}
277284

278285
/**
@@ -296,7 +303,7 @@ public function fail()
296303
*/
297304
public function addError($attr, $msg)
298305
{
299-
$this->_errors[$attr] = $msg;
306+
$this->_errors[] = [$attr, $msg];
300307
}
301308

302309
public function getErrors()
@@ -308,26 +315,30 @@ public function getErrors()
308315
* 得到第一个错误信息
309316
* @author inhere
310317
* @date 2015-09-27
311-
* @return array
318+
* @param bool $onlyMsg
319+
* @return array|string
312320
*/
313-
public function firstError()
321+
public function firstError($onlyMsg=true)
314322
{
315323
$e = $this->_errors;
324+
$first = array_shift($e);
316325

317-
return array_values(array_shift($e))[0];
326+
return $onlyMsg ? array_values($first)[0] : $first;
318327
}
319328

320329
/**
321330
* 得到最后一个错误信息
322331
* @author inhere
323332
* @date 2015-09-27
324-
* @return array
333+
* @param bool $onlyMsg
334+
* @return array|string
325335
*/
326-
public function lastError()
336+
public function lastError($onlyMsg=true)
327337
{
328338
$e = $this->_errors;
339+
$last = array_pop($e);
329340

330-
return array_values(array_pop($e))[0];
341+
return $onlyMsg ? array_values($last)[0] : $last;
331342
}
332343

333344
/**

0 commit comments

Comments
 (0)