Skip to content

Commit 1d544bf

Browse files
committed
update some logic for error message
1 parent 9fdfb2a commit 1d544bf

11 files changed

+249
-174
lines changed

src/FieldValidation.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace Inhere\Validate;
1010

11-
use Inhere\Validate\Filter\Filters;
1211
use Inhere\Validate\Traits\StringRulesTrait;
1312

1413
/**

src/Filter/DataFilteringTrait.php renamed to src/Filter/FilteringTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99
namespace Inhere\Validate\Filter;
1010

1111
/**
12-
* Trait DataFilteringTrait
12+
* Trait FilteringTrait
1313
* @package Inhere\Validate\Filter
1414
*/
15-
trait DataFilteringTrait
15+
trait FilteringTrait
1616
{
1717
/** @var array user custom filters */
1818
private $_filters = [];
@@ -95,7 +95,7 @@ protected function callStringCallback(string $filter, ...$args)
9595

9696
// if $filter is a custom by addFiler()
9797
if ($callback = $this->getFilter($filter)) {
98-
$value = $callback(...$args);
98+
$value = $callback(...$args);
9999
// if $filter is a custom method of the subclass.
100100
} elseif (\method_exists($this, $filter . 'Filter')) {
101101
$filter .= 'Filter';
@@ -151,7 +151,7 @@ public function addFilter(string $name, callable $filter): self
151151
/**
152152
* @param string $name
153153
* @param callable $filter
154-
* @return DataFilteringTrait
154+
* @return FilteringTrait
155155
*/
156156
public function setFilter(string $name, callable $filter)
157157
{

src/Filter/Filtration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
class Filtration
2323
{
24-
use DataFilteringTrait;
24+
use FilteringTrait;
2525

2626
/** @var array raw data */
2727
private $_data;

src/Traits/ErrorMessageTrait.php

Lines changed: 71 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace Inhere\Validate\Traits;
1010

1111
use Inhere\Validate\Helper;
12+
use Inhere\Validate\Validator\Messages;
1213
use Inhere\Validate\Validators;
1314

1415
/**
@@ -19,91 +20,10 @@
1920
trait 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
/**

src/Validator/ScopedValidatorsTrait.php renamed to src/Traits/ScopedValidatorsTrait.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,17 @@
66
* Time: 11:26
77
*/
88

9-
namespace Inhere\Validate\Validator;
9+
namespace Inhere\Validate\Traits;
1010

1111
use Inhere\Validate\Filter\Filters;
12+
use Inhere\Validate\Helper;
1213
use Inhere\Validate\Validators;
1314

1415
/**
1516
* trait ScopedValidatorsTrait - deps the current validation instance.
1617
* - user custom validators
1718
* - some validators of require context data.
18-
* @package Inhere\Validate\Validator
19+
* @package Inhere\Validate\Traits
1920
*/
2021
trait ScopedValidatorsTrait
2122
{
@@ -66,9 +67,8 @@ public function setValidator(string $name, callable $callback, string $message =
6667
$this->_validators[$name] = $callback;
6768

6869
if ($message) {
69-
self::setDefaultMessage($name, $message);
70+
$this->setMessage($name, $message);
7071
}
71-
7272
return $this;
7373
}
7474

@@ -78,11 +78,7 @@ public function setValidator(string $name, callable $callback, string $message =
7878
*/
7979
public function getValidator(string $name)
8080
{
81-
if (isset($this->_validators[$name])) {
82-
return $this->_validators[$name];
83-
}
84-
85-
return null;
81+
return $this->_validators[$name] ?? null;
8682
}
8783

8884
/**

src/Traits/StringRulesTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ protected function parseRule(string $rule, array $row): array
121121
}
122122
break;
123123
default:
124-
$row = \array_merge($row, Filters::explode($args));
124+
$row = \array_merge($row, Filters::explode($args));
125125
break;
126126
}
127127

0 commit comments

Comments
 (0)