Skip to content

Commit 8644f23

Browse files
authored
Added missing methods (accepted_if, ascii, date_equals...) to validation messages. (#7150)
1 parent 9720193 commit 8644f23

File tree

6 files changed

+572
-7
lines changed

6 files changed

+572
-7
lines changed

publish/en/validation.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222
*/
2323

2424
'accepted' => 'The :attribute must be accepted.',
25+
'accepted_if' => 'The :attribute must be accepted when :other is :value.',
2526
'active_url' => 'The :attribute is not a valid URL.',
2627
'after' => 'The :attribute must be a date after :date.',
2728
'after_or_equal' => 'The :attribute must be a date after or equal to :date.',
2829
'alpha' => 'The :attribute may only contain letters.',
2930
'alpha_dash' => 'The :attribute may only contain letters, numbers, and dashes.',
3031
'alpha_num' => 'The :attribute may only contain letters and numbers.',
3132
'array' => 'The :attribute must be an array.',
33+
'ascii' => 'The :attribute must only contain single-byte alphanumeric characters and symbols.',
3234
'before' => 'The :attribute must be a date before :date.',
3335
'before_or_equal' => 'The :attribute must be a date before or equal to :date.',
3436
'between' => [
@@ -40,14 +42,20 @@
4042
'boolean' => 'The :attribute field must be true or false.',
4143
'confirmed' => 'The :attribute confirmation does not match.',
4244
'date' => 'The :attribute is not a valid date.',
45+
'date_equals' => 'The :attribute must be a date equal to :date.',
4346
'date_format' => 'The :attribute does not match the format :format.',
4447
'decimal' => 'The :attribute must have :decimal decimal places.',
48+
'declined' => 'The :attribute must be declined.',
49+
'declined_if' => 'The :attribute must be declined when :other is :value.',
4550
'different' => 'The :attribute and :other must be different.',
4651
'digits' => 'The :attribute must be :digits digits.',
4752
'digits_between' => 'The :attribute must be between :min and :max digits.',
4853
'dimensions' => 'The :attribute has invalid image dimensions.',
4954
'distinct' => 'The :attribute field has a duplicate value.',
55+
'doesnt_end_with' => 'The :attribute must not end with one of the following: :values.',
56+
'doesnt_start_with' => 'The :attribute must not start with one of the following: :values.',
5057
'email' => 'The :attribute must be a valid email address.',
58+
'ends_with' => 'The :attribute must end with one of the following: :values.',
5159
'exists' => 'The selected :attribute is invalid.',
5260
'file' => 'The :attribute must be a file.',
5361
'filled' => 'The :attribute field is required.',
@@ -72,6 +80,7 @@
7280
'ipv6' => 'The :attribute must be a valid IPv6 address.',
7381
'json' => 'The :attribute must be a valid JSON string.',
7482
'list' => 'The :attribute must be a list.',
83+
'lowercase' => 'The :attribute must be lowercase.',
7584
'lt' => [
7685
'numeric' => 'The :attribute must be less than :value',
7786
'file' => 'The :attribute must be less than :value kb',
@@ -90,6 +99,7 @@
9099
'string' => 'The :attribute may not be greater than :max characters.',
91100
'array' => 'The :attribute may not have more than :max items.',
92101
],
102+
'max_digits' => 'The :attribute must not have more than :max digits.',
93103
'mimes' => 'The :attribute must be a file of type: :values.',
94104
'mimetypes' => 'The :attribute must be a file of type: :values.',
95105
'min' => [
@@ -98,6 +108,13 @@
98108
'string' => 'The :attribute must be at least :min characters.',
99109
'array' => 'The :attribute must have at least :min items.',
100110
],
111+
'min_digits' => 'The :attribute must have at least :min digits.',
112+
'missing' => 'The :attribute must be missing.',
113+
'missing_if' => 'The :attribute must be missing when :other is :value.',
114+
'missing_unless' => 'The :attribute must be missing unless :other is :value.',
115+
'missing_with' => 'The :attribute must be missing when :values is present.',
116+
'missing_with_all' => 'The :attribute must be missing when :values are present.',
117+
'multiple_of' => 'The :attribute must be a multiple of :value.',
101118
'not_in' => 'The selected :attribute is invalid.',
102119
'not_regex' => 'The :attribute cannot match a given regular rule.',
103120
'numeric' => 'The :attribute must be a number.',
@@ -128,7 +145,9 @@
128145
'timezone' => 'The :attribute must be a valid zone.',
129146
'unique' => 'The :attribute has already been taken.',
130147
'uploaded' => 'The :attribute failed to upload.',
148+
'uppercase' => 'The :attribute must be uppercase.',
131149
'url' => 'The :attribute format is invalid.',
150+
'ulid' => 'The :attribute must be a valid ULID.',
132151
'uuid' => 'The :attribute is invalid UUID.',
133152
'max_if' => [
134153
'numeric' => 'The :attribute may not be greater than :max when :other is :value.',

src/Concerns/FormatsMessages.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,24 @@ public function getDisplayableValue(string $attribute, $value): string
9494
return $this->customValues[$attribute][$value];
9595
}
9696

97+
if (is_array($value)) {
98+
return 'array';
99+
}
100+
97101
$key = "validation.values.{$attribute}.{$value}";
98102

99103
if (($line = $this->translator->trans($key)) !== $key) {
100104
return $line;
101105
}
102106

107+
if (is_bool($value)) {
108+
return $value ? 'true' : 'false';
109+
}
110+
111+
if (is_null($value)) {
112+
return 'empty';
113+
}
114+
103115
return (string) $value;
104116
}
105117

src/Concerns/ReplacesAttributes.php

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,30 @@
1616

1717
trait ReplacesAttributes
1818
{
19+
/**
20+
* Replace all place-holders for the accepted_if rule.
21+
*/
22+
protected function replaceAcceptedIf(string $message, string $attribute, string $rule, array $parameters): string
23+
{
24+
$parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));
25+
26+
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);
27+
28+
return str_replace([':other', ':value'], $parameters, $message);
29+
}
30+
31+
/**
32+
* Replace all place-holders for the declined_if rule.
33+
*/
34+
protected function replaceDeclinedIf(string $message, string $attribute, string $rule, array $parameters): string
35+
{
36+
$parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));
37+
38+
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);
39+
40+
return str_replace([':other', ':value'], $parameters, $message);
41+
}
42+
1943
/**
2044
* Replace all place-holders for the between rule.
2145
*/
@@ -78,6 +102,14 @@ protected function replaceMin(string $message, string $attribute, string $rule,
78102
return str_replace(':min', $parameters[0], $message);
79103
}
80104

105+
/**
106+
* Replace all place-holders for the min digits rule.
107+
*/
108+
protected function replaceMinDigits(string $message, string $attribute, string $rule, array $parameters): string
109+
{
110+
return str_replace(':min', $parameters[0], $message);
111+
}
112+
81113
/**
82114
* Replace all place-holders for the max rule.
83115
*/
@@ -86,6 +118,61 @@ protected function replaceMax(string $message, string $attribute, string $rule,
86118
return str_replace(':max', $parameters[0], $message);
87119
}
88120

121+
/**
122+
* Replace all place-holders for the max digits rule.
123+
*/
124+
protected function replaceMaxDigits(string $message, string $attribute, string $rule, array $parameters): string
125+
{
126+
return str_replace(':max', $parameters[0], $message);
127+
}
128+
129+
/**
130+
* Replace all place-holders for the missing_if rule.
131+
*/
132+
protected function replaceMissingIf(string $message, string $attribute, string $rule, array $parameters): string
133+
{
134+
$parameters[1] = $this->getDisplayableValue($parameters[0], Arr::get($this->data, $parameters[0]));
135+
136+
$parameters[0] = $this->getDisplayableAttribute($parameters[0]);
137+
138+
return str_replace([':other', ':value'], $parameters, $message);
139+
}
140+
141+
/**
142+
* Replace all place-holders for the missing_unless rule.
143+
*/
144+
protected function replaceMissingUnless(string $message, string $attribute, string $rule, array $parameters): string
145+
{
146+
return str_replace([':other', ':value'], [
147+
$this->getDisplayableAttribute($parameters[0]),
148+
$this->getDisplayableValue($parameters[0], $parameters[1]),
149+
], $message);
150+
}
151+
152+
/**
153+
* Replace all place-holders for the missing_with rule.
154+
*/
155+
protected function replaceMissingWith(string $message, string $attribute, string $rule, array $parameters): string
156+
{
157+
return str_replace(':values', implode(' / ', $this->getAttributeList($parameters)), $message);
158+
}
159+
160+
/**
161+
* Replace all place-holders for the missing_with_all rule.
162+
*/
163+
protected function replaceMissingWithAll(string $message, string $attribute, string $rule, array $parameters): string
164+
{
165+
return $this->replaceMissingWith($message, $attribute, $rule, $parameters);
166+
}
167+
168+
/**
169+
* Replace all place-holders for the multiple_of rule.
170+
*/
171+
protected function replaceMultipleOf(string $message, string $attribute, string $rule, array $parameters): string
172+
{
173+
return str_replace(':value', $parameters[0] ?? '', $message);
174+
}
175+
89176
/**
90177
* Replace all place-holders for the in rule.
91178
*/
@@ -326,6 +413,18 @@ protected function replaceEndsWith(string $message, string $attribute, string $r
326413
return str_replace(':values', implode(', ', $parameters), $message);
327414
}
328415

416+
/**
417+
* Replace all place-holders for the doesnt_end_with rule.
418+
*/
419+
protected function replaceDoesntEndWith(string $message, string $attribute, string $rule, array $parameters): string
420+
{
421+
foreach ($parameters as &$parameter) {
422+
$parameter = $this->getDisplayableValue($attribute, $parameter);
423+
}
424+
425+
return str_replace(':values', implode(', ', $parameters), $message);
426+
}
427+
329428
/**
330429
* Replace all place-holders for the starts_with rule.
331430
*/
@@ -338,6 +437,18 @@ protected function replaceStartsWith(string $message, string $attribute, string
338437
return str_replace(':values', implode(', ', $parameters), $message);
339438
}
340439

440+
/**
441+
* Replace all place-holders for the doesnt_start_with rule.
442+
*/
443+
protected function replaceDoesntStartWith(string $message, string $attribute, string $rule, array $parameters): string
444+
{
445+
foreach ($parameters as &$parameter) {
446+
$parameter = $this->getDisplayableValue($attribute, $parameter);
447+
}
448+
449+
return str_replace(':values', implode(', ', $parameters), $message);
450+
}
451+
341452
/**
342453
* Replace all place-holders for the prohibited_with rule.
343454
* @param array<int,string> $parameters

src/Concerns/ValidatesAttributes.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2009,4 +2009,15 @@ protected function shouldBeNumeric(string $attribute, string $rule)
20092009
$this->numericRules[] = $rule;
20102010
}
20112011
}
2012+
2013+
/**
2014+
* Trim the value if it is a string.
2015+
*
2016+
* @param mixed $value
2017+
* @return mixed
2018+
*/
2019+
protected function trim($value)
2020+
{
2021+
return is_string($value) ? trim($value) : $value;
2022+
}
20122023
}

src/Validator.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -136,18 +136,19 @@ class Validator implements ValidatorContract
136136
* The validation rules that imply the field is required.
137137
*/
138138
protected array $implicitRules = [
139-
'Required', 'Filled', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout',
140-
'RequiredWithoutAll', 'RequiredIf', 'RequiredUnless', 'Accepted', 'Present',
139+
'Required', 'Filled', 'Missing', 'MissingIf', 'MissingUnless', 'MissingWith',
140+
'MissingWithAll', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll',
141+
'RequiredIf', 'RequiredUnless', 'Accepted', 'AcceptedIf', 'Declined', 'DeclinedIf', 'Present',
141142
];
142143

143144
/**
144145
* The validation rules which depend on other fields as parameters.
145146
*/
146147
protected array $dependentRules = [
147-
'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll',
148-
'RequiredIf', 'RequiredUnless', 'Confirmed', 'Same', 'Different', 'Unique',
149-
'Before', 'After', 'BeforeOrEqual', 'AfterOrEqual', 'Gt', 'Lt', 'Gte', 'Lte',
150-
'Prohibits', 'ExcludeIf', 'ExcludeUnless', 'ExcludeWith', 'ExcludeWithout',
148+
'AcceptedIf', 'DeclinedIf', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout',
149+
'RequiredWithoutAll', 'RequiredIf', 'RequiredUnless', 'Confirmed', 'Same', 'Different',
150+
'Unique', 'Before', 'After', 'BeforeOrEqual', 'AfterOrEqual', 'Gt', 'Lt', 'Gte', 'Lte',
151+
'Prohibits', 'ExcludeIf', 'ExcludeUnless', 'ExcludeWith', 'ExcludeWithout', 'MissingIf', 'MissingUnless', 'MissingWith', 'MissingWithAll',
151152
];
152153

153154
/**

0 commit comments

Comments
 (0)