99namespace Inhere \Validate \Traits ;
1010
1111use Inhere \Validate \Helper ;
12+ use Inhere \Validate \Validator \Messages ;
1213use Inhere \Validate \Validators ;
1314
1415/**
1920trait ErrorMessageTrait
2021{
2122 /**
22- * 默认的错误提示信息
23+ * error messages map
2324 * @var array
2425 */
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 = [];
10727
10828 /**
10929 * attribute field translate list
@@ -318,26 +238,25 @@ public function isStopOnError(): bool
318238 return $ this ->_stopOnError ;
319239 }
320240
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+
321248 /*******************************************************************************
322249 * Error Messages
323250 ******************************************************************************/
324251
325- /**
326- * @return array
327- */
328- public static function getDefaultMessages (): array
329- {
330- return self ::$ messages ;
331- }
332-
333252 /**
334253 * @param string $key
335- * @param string|array $msg
254+ * @param string|array $message
336255 */
337- public static function setDefaultMessage (string $ key , $ msg )
256+ public function setMessage (string $ key , $ message )
338257 {
339- if ($ key && $ msg ) {
340- self :: $ messages [$ key ] = $ msg ;
258+ if ($ key && $ message ) {
259+ $ this -> _messages [$ key ] = $ message ;
341260 }
342261 }
343262
@@ -346,7 +265,7 @@ public static function setDefaultMessage(string $key, $msg)
346265 */
347266 public function getMessages (): array
348267 {
349- return \array_merge ( self :: getDefaultMessages (), $ this ->messages ()) ;
268+ return $ this ->_messages ;
350269 }
351270
352271 /**
@@ -356,7 +275,7 @@ public function getMessages(): array
356275 public function setMessages (array $ messages ): self
357276 {
358277 foreach ($ messages as $ key => $ value ) {
359- self :: setDefaultMessage ($ key , $ value );
278+ $ this -> setMessage ($ key , $ value );
360279 }
361280
362281 return $ this ;
@@ -372,49 +291,68 @@ public function setMessages(array $messages): self
372291 */
373292 public function getMessage ($ validator , string $ field , array $ args = [], $ message = null ): string
374293 {
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+ ];
377298
378- // get message from default dict.
299+ // get message from built in dict.
379300 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 );
390308 }
309+ } else {
310+ $ message = (string )$ message ;
311+ }
391312
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 ];
395317 }
396318
397319 if (\is_string ($ message ) && false === \strpos ($ message , '{ ' )) {
398320 return $ message ;
399321 }
400322
401- $ params = [
402- '{attr} ' => $ this ->getTranslate ($ field )
403- ];
404-
405323 foreach ($ args as $ key => $ value ) {
406324 $ key = \is_int ($ key ) ? "value {$ key }" : $ key ;
407325 // build params
408326 $ params ['{ ' . $ key . '} ' ] = \is_array ($ value ) ? \implode (', ' , $ value ) : $ value ;
409327 }
410328
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 );
415353 }
416354
417- return \strtr ( $ message, $ params ) ;
355+ return $ message ;
418356 }
419357
420358 /**
@@ -425,7 +363,17 @@ public function getMessage($validator, string $field, array $args = [], $message
425363 public function setTranslates (array $ fieldTrans ): self
426364 {
427365 $ this ->_translates = $ fieldTrans ;
366+ return $ this ;
367+ }
428368
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 );
429377 return $ this ;
430378 }
431379
@@ -434,13 +382,7 @@ public function setTranslates(array $fieldTrans): self
434382 */
435383 public function getTranslates (): array
436384 {
437- static $ translates ;
438-
439- if (!$ translates ) {
440- $ translates = \array_merge ($ this ->translates (), $ this ->_translates );
441- }
442-
443- return $ translates ;
385+ return $ this ->_translates ;
444386 }
445387
446388 /**
0 commit comments