8000 feature #48232 [Validator] Add `{{pattern}}` to `Regex` constraint vi… · symfony/symfony@96993d9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 96993d9

Browse files
committed
feature #48232 [Validator] Add {{pattern}} to Regex constraint violations (alamirault)
This PR was merged into the 6.3 branch. Discussion ---------- [Validator] Add `{{pattern}}` to `Regex` constraint violations | Q | A | ------------- | --- | Branch? | 6.3 | Bug fix? | no | New feature? | yes | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT | Doc PR | symfony/symfony-docs/pull/17436 This PR add parameter `pattern` to `Regex` constraints violations. This is useful for building message to technical users/consumers. (like limit, choices, min, max ... in other constraints ) Commits ------- 369b051 [Validator] Add pattern in Regex constraint violations
2 parents ed1e8be + 369b051 commit 96993d9

File tree

3 files changed

+4
-0
lines changed

3 files changed

+4
-0
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add `Uuid::TIME_BASED_VERSIONS` to match that a UUID being validated embeds a timestamp
8+
* Add the `pattern` parameter in violations of the `Regex` constraint
89

910
6.2
1011
---

src/Symfony/Component/Validator/Constraints/RegexValidator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function validate(mixed $value, Constraint $constraint)
4747
if ($constraint->match xor preg_match($constraint->pattern, $value)) {
4848
$this->context->buildViolation($constraint->message)
4949
->setParameter('{{ value }}', $this->formatValue($value))
50+
->setParameter('{{ pattern }}', $constraint->pattern)
5051
->setCode(Regex::REGEX_FAILED_ERROR)
5152
->addViolation();
5253
}

src/Symfony/Component/Validator/Tests/Constraints/RegexValidatorTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public function testInvalidValues($value)
118118

119119
$this->buildViolation('myMessage')
120120
->setParameter('{{ value }}', '"'.$value.'"')
121+
->setParameter('{{ pattern }}', '/^[0-9]+$/')
121122
->setCode(Regex::REGEX_FAILED_ERROR)
122123
->assertRaised();
123124
}
@@ -133,6 +134,7 @@ public function testInvalidValuesNamed($value)
133134

134135
$this->buildViolation('myMessage')
135136
->setParameter('{{ value }}', '"'.$value.'"')
137+
->setParameter('{{ pattern }}', '/^[0-9]+$/')
136138
->setCode(Regex::REGEX_FAILED_ERROR)
137139
->assertRaised();
138140
}

0 commit comments

Comments
 (0)
0