Skip to content

Commit 2f7223b

Browse files
authored
Merge pull request #80 from phenixphp/feature/rule-error-messages
Rule error messages
2 parents 10bb6a1 + b7a5f97 commit 2f7223b

File tree

95 files changed

+1468
-9
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+1468
-9
lines changed

src/Validation/Contracts/Rule.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,6 @@ public function setField(string $field): self;
1313
public function setData(Dot|array $data): self;
1414

1515
public function passes(): bool;
16+
17+
public function message(): string|null;
1618
}

src/Validation/Rules/Between.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,23 @@ public function passes(): bool
2121

2222
return $value >= $this->min && $value <= $this->max;
2323
}
24+
25+
public function message(): string|null
26+
{
27+
$value = $this->data->get($this->field) ?? null;
28+
$type = gettype($value);
29+
30+
$key = match ($type) {
31+
'string' => 'validation.between.string',
32+
'array' => 'validation.between.array',
33+
'object' => 'validation.between.file',
34+
default => 'validation.between.numeric',
35+
};
36+
37+
return trans($key, [
38+
'field' => $this->getFieldForHumans(),
39+
'min' => $this->min,
40+
'max' => $this->max,
41+
]);
42+
}
2443
}

src/Validation/Rules/Confirmed.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,12 @@ public function passes(): bool
2020
&& $confirmation !== null
2121
&& $original === $confirmation;
2222
}
23+
24+
public function message(): string|null
25+
{
26+
return trans('validation.confirmed', [
27+
'field' => $this->getFieldForHumans(),
28+
'other' => $this->confirmationField,
29+
]);
30+
}
2331
}

src/Validation/Rules/Dates/After.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ public function passes(): bool
1212
{
1313
return Date::parse($this->getValue())->greaterThan($this->date);
1414
}
15+
16+
public function message(): string|null
17+
{
18+
return trans('validation.date.after', ['field' => $this->getFieldForHumans()]);
19+
}
1520
}

src/Validation/Rules/Dates/AfterOrEqual.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ public function passes(): bool
1212
{
1313
return Date::parse($this->getValue())->greaterThanOrEqualTo($this->date);
1414
}
15+
16+
public function message(): string|null
17+
{
18+
return trans('validation.date.after_or_equal', ['field' => $this->getFieldForHumans()]);
19+
}
1520
}

src/Validation/Rules/Dates/AfterOrEqualTo.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ public function passes(): bool
1515

1616
return Date::parse($date)->greaterThanOrEqualTo($relatedDate);
1717
}
18+
19+
public function message(): string|null
20+
{
21+
return trans('validation.date.after_or_equal_to', [
22+
'field' => $this->getFieldForHumans(),
23+
'other' => $this->relatedField,
24+
]);
25+
}
1826
}

src/Validation/Rules/Dates/AfterTo.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ public function passes(): bool
1515

1616
return Date::parse($date)->greaterThan($relatedDate);
1717
}
18+
19+
public function message(): string|null
20+
{
21+
return trans('validation.date.after_to', [
22+
'field' => $this->getFieldForHumans(),
23+
'other' => $this->relatedField,
24+
]);
25+
}
1826
}

src/Validation/Rules/Dates/Before.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ public function passes(): bool
1212
{
1313
return Date::parse($this->getValue())->lessThan($this->date);
1414
}
15+
16+
public function message(): string|null
17+
{
18+
return trans('validation.date.before', ['field' => $this->getFieldForHumans()]);
19+
}
1520
}

src/Validation/Rules/Dates/BeforeOrEqual.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ public function passes(): bool
1212
{
1313
return Date::parse($this->getValue())->lessThanOrEqualTo($this->date);
1414
}
15+
16+
public function message(): string|null
17+
{
18+
return trans('validation.date.before_or_equal', ['field' => $this->getFieldForHumans()]);
19+
}
1520
}

src/Validation/Rules/Dates/BeforeOrEqualTo.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,12 @@ public function passes(): bool
1515

1616
return Date::parse($date)->lessThanOrEqualTo($relatedDate);
1717
}
18+
19+
public function message(): string|null
20+
{
21+
return trans('validation.date.before_or_equal_to', [
22+
'field' => $this->getFieldForHumans(),
23+
'other' => $this->relatedField,
24+
]);
25+
}
1826
}

0 commit comments

Comments
 (0)