Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 6299143

Browse files
committed
chore: improve Country error message by including the iso code being used
1 parent 6b8c69e commit 6299143

File tree

5 files changed

+18
-6
lines changed

5 files changed

+18
-6
lines changed

docs/03x-rules-country.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Validates that a value is a valid country code.
55
```php
66
Country(
77
string $code = 'alpha-2',
8-
string $message = 'The "{{ name }}" value is not a valid country code, "{{ value }}" given.'
8+
string $message = 'The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.'
99
);
1010
```
1111

@@ -42,7 +42,7 @@ Available options:
4242

4343
### `message`
4444

45-
type: `string` default: `The "{{ name }}" value is not a valid country code, "{{ value }}" given.`
45+
type: `string` default: `The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.`
4646

4747
Message that will be shown if the input value is not a valid country code.
4848

src/ChainedValidatorInterface.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function choice(
3434

3535
public function country(
3636
string $code = 'alpha-2',
37-
string $message = 'The "{{ name }}" value is not a valid country code, "{{ value }}" given.'
37+
string $message = 'The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.'
3838
): ChainedValidatorInterface;
3939

4040
public function greaterThan(
@@ -70,6 +70,12 @@ public function range(
7070

7171
public function rule(RuleInterface $constraint): ChainedValidatorInterface;
7272

73+
public function timezone(
74+
int $timezoneGroup = \DateTimeZone::ALL,
75+
?string $countryCode = null,
76+
string $message = 'The "{{ name }}" value is not a valid timezone, "{{ value }}" given.'
77+
): ChainedValidatorInterface;
78+
7379
public function type(
7480
string|array $constraint,
7581
string $message = 'The "{{ name }}" value should be of type "{{ constraint }}", "{{ value }}" given.'

src/Rule/Country.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Country extends AbstractRule implements RuleInterface
1818

1919
public function __construct(
2020
private readonly string $code = self::ALPHA_2_CODE,
21-
private readonly string $message = 'The "{{ name }}" value is not a valid country code, "{{ value }}" given.'
21+
private readonly string $message = 'The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.'
2222
) {}
2323

2424
public function assert(mixed $value, string $name): void

src/StaticValidatorInterface.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public static function choice(
2424

2525
public static function country(
2626
string $code = 'alpha-2',
27-
string $message = 'The "{{ name }}" value is not a valid country code, "{{ value }}" given.'
27+
string $message = 'The "{{ name }}" value is not a valid "{{ code }}" country code, "{{ value }}" given.'
2828
): ChainedValidatorInterface;
2929

3030
public static function greaterThan(
@@ -60,6 +60,12 @@ public static function range(
6060

6161
public static function rule(RuleInterface $constraint): ChainedValidatorInterface;
6262

63+
public static function timezone(
64+
int $timezoneGroup = \DateTimeZone::ALL,
65+
?string $countryCode = null,
66+
string $message = 'The "{{ name }}" value is not a valid timezone, "{{ value }}" given.'
67+
): ChainedValidatorInterface;
68+
6369
public static function type(
6470
string|array $constraint,
6571
string $message = 'The "{{ name }}" value should be of type "{{ constraint }}", "{{ value }}" given.'

tests/CountryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static function provideRuleUnexpectedValueData(): \Generator
2828
public static function provideRuleFailureConditionData(): \Generator
2929
{
3030
$exception = CountryException::class;
31-
$message = '/The "(.*)" value is not a valid country code, "(.*)" given./';
31+
$message = '/The "(.*)" value is not a valid "(.*)" country code, "(.*)" given./';
3232

3333
yield 'default' => [new Country(), 'PRT', $exception, $message];
3434
yield 'alpha2' => [new Country(code: 'alpha-2'), 'PRT', $exception, $message];

0 commit comments

Comments
 (0)