@@ -119,11 +119,11 @@ public function beforeValidate(){}
119
119
* @author inhere
120
120
* @date 2015-08-11
121
121
* @param array $onlyChecked 只检查一部分属性
122
- * @param boolean $hasErrorStop
122
+ * @param boolean $hasErrorStop 出现错误即停止验证
123
123
* @return static
124
124
* @throws \RuntimeException
125
125
*/
126
- public function validate (array $ onlyChecked = [],$ hasErrorStop =null )
126
+ public function validate (array $ onlyChecked = [], $ hasErrorStop =false )
127
127
{
128
128
if ( !property_exists ($ this , 'data ' ) ) {
129
129
throw new \InvalidArgumentException ('Must be defined property \'data (array) \' in the classes used. ' );
@@ -133,8 +133,7 @@ public function validate(array $onlyChecked = [],$hasErrorStop=null)
133
133
return $ this ;
134
134
}
135
135
136
- $ this ->beforeValidate ();
137
- $ this ->clearErrors ();
136
+ $ this ->clearErrors ()->beforeValidate ();
138
137
$ this ->hasErrorStop ($ hasErrorStop );
139
138
140
139
// 循环规则
@@ -216,12 +215,17 @@ protected function doValidate($data, $name, $validator, $copy)
216
215
if ($ result && $ validator !== 'required ' ) {
217
216
array_unshift ($ copy , $ data [$ name ]);// 压入当前属性值
218
217
218
+ // if it's a closure
219
219
if ( is_callable ($ validator ) && $ validator instanceof \Closure) {
220
220
$ result = call_user_func_array ($ validator , $ copy );
221
221
$ validator = 'callback ' ;
222
+
223
+ // if it is a method of the subclass.
222
224
} elseif ( is_string ($ validator ) && method_exists ($ this , $ validator ) ) {
223
225
224
226
$ result = call_user_func_array ( [ $ this , $ validator ] , $ copy );
227
+
228
+ // it's a method of the class 'ValidatorList'
225
229
} elseif ( is_callable ([ValidatorList::class, $ validator ]) ) {
226
230
227
231
$ result = call_user_func_array ( [ ValidatorList::class, $ validator ] , $ copy );
@@ -260,19 +264,22 @@ protected function collectRules()
260
264
261
265
//////////////////////////////////// error info ////////////////////////////////////
262
266
267
+ /**
268
+ * @return $this
269
+ */
263
270
public function clearErrors ()
264
271
{
265
272
$ this ->_errors = [];
273
+
274
+ return $ this ;
266
275
}
267
276
268
277
/**
269
- * @param null| bool $val
278
+ * @param bool $val
270
279
*/
271
280
public function hasErrorStop ($ val )
272
281
{
273
- if ($ val !== null ) {
274
- $ this ->_hasErrorStop = (bool )$ val ;
275
- }
282
+ $ this ->_hasErrorStop = (bool )$ val ;
276
283
}
277
284
278
285
/**
@@ -296,7 +303,7 @@ public function fail()
296
303
*/
297
304
public function addError ($ attr , $ msg )
298
305
{
299
- $ this ->_errors [$ attr ] = $ msg ;
306
+ $ this ->_errors [] = [ $ attr , $ msg] ;
300
307
}
301
308
302
309
public function getErrors ()
@@ -308,26 +315,30 @@ public function getErrors()
308
315
* 得到第一个错误信息
309
316
* @author inhere
310
317
* @date 2015-09-27
311
- * @return array
318
+ * @param bool $onlyMsg
319
+ * @return array|string
312
320
*/
313
- public function firstError ()
321
+ public function firstError ($ onlyMsg = true )
314
322
{
315
323
$ e = $ this ->_errors ;
324
+ $ first = array_shift ($ e );
316
325
317
- return array_values (array_shift ( $ e )) [0 ];
326
+ return $ onlyMsg ? array_values ($ first ) [0 ] : $ first ;
318
327
}
319
328
320
329
/**
321
330
* 得到最后一个错误信息
322
331
* @author inhere
323
332
* @date 2015-09-27
324
- * @return array
333
+ * @param bool $onlyMsg
334
+ * @return array|string
325
335
*/
326
- public function lastError ()
336
+ public function lastError ($ onlyMsg = true )
327
337
{
328
338
$ e = $ this ->_errors ;
339
+ $ last = array_pop ($ e );
329
340
330
- return array_values (array_pop ( $ e )) [0 ];
341
+ return $ onlyMsg ? array_values ($ last ) [0 ] : $ last ;
331
342
}
332
343
333
344
/**
0 commit comments