9
9
namespace Inhere \Validate \Traits ;
10
10
11
11
use Inhere \Validate \Helper ;
12
+ use Inhere \Validate \Validator \Messages ;
12
13
use Inhere \Validate \Validators ;
13
14
14
15
/**
19
20
trait ErrorMessageTrait
20
21
{
21
22
/**
22
- * 默认的错误提示信息
23
+ * error messages map
23
24
* @var array
24
25
*/
25
- public static $ messages = [
26
- // 'int' 'integer'
27
- 'integer ' => '{attr} must be an integer! ' ,
28
- // 'num'
29
- 'number ' => [
30
- '{attr} must be an integer greater than 0! ' ,
31
- '{attr} must be an integer and minimum value is {min} ' ,
32
- '{attr} must be an integer and in the range {min} ~ {max} ' ,
33
- ],
34
- // 'bool', 'boolean',
35
- 'boolean ' => '{attr} must be is boolean! ' ,
36
- 'float ' => '{attr} must be is float! ' ,
37
- 'url ' => '{attr} is not a url address! ' ,
38
- 'email ' => '{attr} is not a email address! ' ,
39
- 'date ' => '{attr} is not a date format! ' ,
40
- 'dateFormat ' => '{attr} is not in a {value0} date format! ' ,
41
- 'ip ' => '{attr} is not IP address! ' ,
42
- 'ipv4 ' => '{attr} is not a IPv4 address! ' ,
43
- 'ipv6 ' => '{attr} is not a IPv6 address! ' ,
44
- 'required ' => 'parameter {attr} is required! ' ,
45
- 'length ' => [
46
- '{attr} length validation is not through! ' ,
47
- '{attr} must be an string/array and minimum length is {min} ' ,
48
- '{attr} must be an string/array and length range {min} ~ {max} ' ,
49
- ],
50
- // 'range', 'between'
51
- 'size ' => [
52
- '{attr} size validation is not through! ' ,
53
- '{attr} must be an integer/string/array and minimum value/length is {min} ' ,
54
- // '{attr} must be an integer/string/array and value/length range {min} ~ {max}',
55
- '{attr} must be in the range {min} ~ {max} ' ,
56
- ],
57
-
58
- // 'lengthEq', 'sizeEq'
59
- 'fixedSize ' => '{attr} length must is {value0} ' ,
60
-
61
- 'min ' => '{attr} minimum boundary is {value0} ' ,
62
- 'max ' => '{attr} maximum boundary is {value0} ' ,
63
-
64
- // 'in', 'enum',
65
- 'enum ' => '{attr} must in ({value0}) ' ,
66
- 'notIn ' => '{attr} cannot in ({value0}) ' ,
67
-
68
- 'string ' => [
69
- '{attr} must be a string ' ,
70
- '{attr} must be a string and minimum length be {min} ' ,
71
- '{attr} must be a string and length range must be {min} ~ {max} ' ,
72
- ],
73
-
74
- // 'regex', 'regexp',
75
- 'regexp ' => '{attr} does not match the {value0} conditions ' ,
76
-
77
- 'mustBe ' => '{attr} must be equals to {value0} ' ,
78
- 'notBe ' => '{attr} can not be equals to {value0} ' ,
79
-
80
- 'compare ' => '{attr} must be equals to {value0} ' ,
81
- 'same ' => '{attr} must be equals to {value0} ' ,
82
- 'equal ' => '{attr} must be equals to {value0} ' ,
83
-
84
- // 'different'
85
- 'notEqual ' => '{attr} can not be equals to {value0} ' ,
86
-
87
- 'isArray ' => '{attr} must be an array ' ,
88
- 'isMap ' => '{attr} must be an array and is key-value format ' ,
89
- 'isList ' => '{attr} must be an array of nature ' ,
90
- 'intList ' => '{attr} must be an array and value is all integers ' ,
91
- 'numList ' => '{attr} must be an array and value is all numbers ' ,
92
- 'strList ' => '{attr} must be an array and value is all strings ' ,
93
- 'arrList ' => '{attr} must be an array and value is all arrays ' ,
94
-
95
- 'each ' => '{attr} must be through the {value0} verify ' ,
96
- 'hasKey ' => '{attr} must be contains the key {value0} ' ,
97
- 'distinct ' => 'there should not be duplicate keys in the {attr} ' ,
98
-
99
- 'json ' => '{attr} must be an json string ' ,
100
-
101
- 'file ' => '{attr} must be an uploaded file ' ,
102
- 'image ' => '{attr} must be an uploaded image file ' ,
103
-
104
- 'callback ' => '{attr} don \'t pass the test and verify! ' ,
105
- '_ ' => '{attr} validation is not through! ' ,
106
- ];
26
+ private $ _messages = [];
107
27
108
28
/**
109
29
* attribute field translate list
@@ -318,26 +238,25 @@ public function isStopOnError(): bool
318
238
return $ this ->_stopOnError ;
319
239
}
320
240
241
+ protected function prepareValidation ()
242
+ {
243
+ $ this ->_translates = \array_merge ($ this ->translates (), $ this ->_translates );
244
+ // error message
245
+ $ this ->_messages = \array_merge ($ this ->messages (), $ this ->_messages );
246
+ }
247
+
321
248
/*******************************************************************************
322
249
* Error Messages
323
250
******************************************************************************/
324
251
325
- /**
326
- * @return array
327
- */
328
- public static function getDefaultMessages (): array
329
- {
330
- return self ::$ messages ;
331
- }
332
-
333
252
/**
334
253
* @param string $key
335
- * @param string|array $msg
254
+ * @param string|array $message
336
255
*/
337
- public static function setDefaultMessage (string $ key , $ msg )
256
+ public function setMessage (string $ key , $ message )
338
257
{
339
- if ($ key && $ msg ) {
340
- self :: $ messages [$ key ] = $ msg ;
258
+ if ($ key && $ message ) {
259
+ $ this -> _messages [$ key ] = $ message ;
341
260
}
342
261
}
343
262
@@ -346,7 +265,7 @@ public static function setDefaultMessage(string $key, $msg)
346
265
*/
347
266
public function getMessages (): array
348
267
{
349
- return \array_merge ( self :: getDefaultMessages (), $ this ->messages ()) ;
268
+ return $ this ->_messages ;
350
269
}
351
270
352
271
/**
@@ -356,7 +275,7 @@ public function getMessages(): array
356
275
public function setMessages (array $ messages ): self
357
276
{
358
277
foreach ($ messages as $ key => $ value ) {
359
- self :: setDefaultMessage ($ key , $ value );
278
+ $ this -> setMessage ($ key , $ value );
360
279
}
361
280
362
281
return $ this ;
@@ -372,49 +291,68 @@ public function setMessages(array $messages): self
372
291
*/
373
292
public function getMessage ($ validator , string $ field , array $ args = [], $ message = null ): string
374
293
{
375
- $ rawName = \is_string ($ validator ) ? $ validator : 'callback ' ;
376
- $ validator = Validators::getRealName ($ rawName );
294
+ $ rawName = \is_string ($ validator ) ? $ validator : 'callback ' ;
295
+ $ params = [
296
+ '{attr} ' => $ this ->getTranslate ($ field )
297
+ ];
377
298
378
- // get message from default dict.
299
+ // get message from built in dict.
379
300
if (!$ message ) {
380
- // allow define a message for a validator. eg: 'username.required' => 'some message ...'
381
- $ fullKey = $ field . '. ' . $ rawName ;
382
- $ messages = $ this ->getMessages ();
383
-
384
- if (isset ($ messages [$ fullKey ])) {
385
- $ message = $ messages [$ fullKey ];
386
- } elseif (isset ($ messages [$ rawName ])) {
387
- $ message = $ messages [$ rawName ];
388
- } else {
389
- $ message = $ messages [$ validator ] ?? $ messages ['_ ' ];
301
+ $ message = $ this ->findMessage ($ field , $ rawName ) ?: Messages::getDefault ();
302
+ // is array. It's defined multi error messages
303
+ } elseif (\is_array ($ message )) {
304
+ $ message = $ message [$ rawName ] ?? $ this ->findMessage ($ field , $ rawName );
305
+
306
+ if (!$ message ) { // use default
307
+ return \strtr (Messages::getDefault (), $ params );
390
308
}
309
+ } else {
310
+ $ message = (string )$ message ;
311
+ }
391
312
392
- // is array. It's defined multi error messages
393
- } elseif (\is_array ($ message ) && isset ($ message [$ rawName ])) {
394
- $ message = $ message [$ rawName ];
313
+ /** @see Messages::$messages['size'] */
314
+ if (\is_array ($ message )) {
315
+ $ msgKey = \count ($ params ) - 1 ;
316
+ $ message = $ message [$ msgKey ] ?? $ message [0 ];
395
317
}
396
318
397
319
if (\is_string ($ message ) && false === \strpos ($ message , '{ ' )) {
398
320
return $ message ;
399
321
}
400
322
401
- $ params = [
402
- '{attr} ' => $ this ->getTranslate ($ field )
403
- ];
404
-
405
323
foreach ($ args as $ key => $ value ) {
406
324
$ key = \is_int ($ key ) ? "value {$ key }" : $ key ;
407
325
// build params
408
326
$ params ['{ ' . $ key . '} ' ] = \is_array ($ value ) ? \implode (', ' , $ value ) : $ value ;
409
327
}
410
328
411
- // @see self::$messages['size']
412
- if (\is_array ($ message )) {
413
- $ msgKey = \count ($ params ) - 1 ;
414
- $ message = $ message [$ msgKey ] ?? $ message [0 ];
329
+ return \strtr ($ message , $ params );
330
+ }
331
+
332
+ /**
333
+ * @param string $field
334
+ * @param string $rawName
335
+ * @return string|array
336
+ */
337
+ protected function findMessage (string $ field , string $ rawName )
338
+ {
339
+ // allow define a message for a validator.
340
+ // eg: 'username.required' => 'some message ...'
341
+ $ fullKey = $ field . '. ' . $ rawName ;
342
+ $ realName = Validators::getRealName ($ rawName );
343
+
344
+ if (isset ($ this ->_messages [$ fullKey ])) {
345
+ $ message = $ this ->_messages [$ fullKey ];
346
+ // eg 'required' => 'some message ...'
347
+ } elseif (isset ($ this ->_messages [$ rawName ])) {
348
+ $ message = $ this ->_messages [$ rawName ];
349
+ } elseif (isset ($ this ->_messages [$ realName ])) {
350
+ $ message = $ this ->_messages [$ realName ];
351
+ } else { // get from default
352
+ $ message = Messages::get ($ realName );
415
353
}
416
354
417
- return \strtr ( $ message, $ params ) ;
355
+ return $ message ;
418
356
}
419
357
420
358
/**
@@ -425,7 +363,17 @@ public function getMessage($validator, string $field, array $args = [], $message
425
363
public function setTranslates (array $ fieldTrans ): self
426
364
{
427
365
$ this ->_translates = $ fieldTrans ;
366
+ return $ this ;
367
+ }
428
368
369
+ /**
370
+ * add the attrs translation data
371
+ * @param array $fieldTrans
372
+ * @return $this
373
+ */
374
+ public function addTranslates (array $ fieldTrans ): self
375
+ {
376
+ $ this ->_translates = \array_merge ($ this ->_translates , $ fieldTrans );
429
377
return $ this ;
430
378
}
431
379
@@ -434,13 +382,7 @@ public function setTranslates(array $fieldTrans): self
434
382
*/
435
383
public function getTranslates (): array
436
384
{
437
- static $ translates ;
438
-
439
- if (!$ translates ) {
440
- $ translates = \array_merge ($ this ->translates (), $ this ->_translates );
441
- }
442
-
443
- return $ translates ;
385
+ return $ this ->_translates ;
444
386
}
445
387
446
388
/**
0 commit comments