Skip to content

Commit 5ce3a61

Browse files
committed
feature #31083 [Validator] Allow objects implementing __toString() to be used as violation messages (mdlutz24)
This PR was merged into the 4.4 branch. Discussion ---------- [Validator] Allow objects implementing __toString() to be used as violation messages | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | | License | MIT | Doc PR | I didn't see a doc on violations to update, but I'm happy to do documentation if somone can suggest the best place to do it. Currently in the Drupal project we use Translatable Markup object to hold most strings and currently pass them in as Constraint Violation messages. In Symfony 3 this works but with the added typehinting in Symfony 4, these markup objects are rendered into strings at the time of the violation creation. This causes any html in the message string to be considered unsafe by twig later in our rendering process. This pr explicitly allows objects implementing a __toString() method to be used as violation messages, and the violation will save and return the original stringable object. See https://www.drupal.org/project/drupal/issues/3029540 For our Drupal issue on the subject. Commits ------- 79f4dcd [Validator] Allow objects implementing __toString() to be used as violation messages
2 parents 6888e70 + 79f4dcd commit 5ce3a61

File tree

6 files changed

+60
-6
lines changed

6 files changed

+60
-6
lines changed

src/Symfony/Component/Validator/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ CHANGELOG
2020
`maxPropertyPath` options
2121
* added a new `notInRangeMessage` option to the `Range` constraint that will
2222
be used in the violation builder when both `min` and `max` are not null
23+
* added ability to use stringable objects as violation messages
2324

2425
4.3.0
2526
-----

src/Symfony/Component/Validator/ConstraintViolation.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ConstraintViolation implements ConstraintViolationInterface
3232
/**
3333
* Creates a new constraint violation.
3434
*
35-
* @param string $message The violation message
35+
* @param string $message The violation message as a string or a stringable object
3636
* @param string $messageTemplate The raw violation message
3737
* @param array $parameters The parameters to substitute in the
3838
* raw violation message
@@ -47,7 +47,7 @@ class ConstraintViolation implements ConstraintViolationInterface
4747
* @param string|null $code The error code of the violation
4848
* @param mixed $cause The cause of the violation
4949
*/
50-
public function __construct(?string $message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, $code = null, Constraint $constraint = null, $cause = null)
50+
public function __construct($message, ?string $messageTemplate, array $parameters, $root, ?string $propertyPath, $invalidValue, int $plural = null, $code = null, Constraint $constraint = null, $cause = null)
5151
{
5252
if (null === $message) {
5353
@trigger_error(sprintf('Passing a null message when instantiating a "%s" is deprecated since Symfony 4.4.', __CLASS__), E_USER_DEPRECATED);
@@ -58,6 +58,10 @@ public function __construct(?string $message, ?string $messageTemplate, array $p
5858
@trigger_error(sprintf('Not using a string as the error code in %s() is deprecated since Symfony 4.4. A type-hint will be added in 5.0.', __METHOD__), E_USER_DEPRECATED);
5959
}
6060

61+
if (!\is_string($message) && !(\is_object($message) && method_exists($message, '__toString'))) {
62+
throw new \TypeError('Constraint violation message should be a string or an object which implements the __toString() method.');
63+
}
64+
6165
$this->message = $message;
6266
$this->messageTemplate = $messageTemplate;
6367
$this->parameters = $parameters;

src/Symfony/Component/Validator/ConstraintViolationInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ interface ConstraintViolationInterface
3636
/**
3737
* Returns the violation message.
3838
*
39-
* @return string The violation message
39+
* @return string The violation message as a string or a stringable object
4040
*/
4141
public function getMessage();
4242

src/Symfony/Component/Validator/Context/ExecutionContextInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ interface ExecutionContextInterface
6464
/**
6565
* Adds a violation at the current node of the validation graph.
6666
*
67-
* @param string $message The error message
67+
* @param string $message The error message as a string or a stringable object
6868
* @param array $params The parameters substituted in the error message
6969
*/
7070
public function addViolation($message, array $params = []);
@@ -81,7 +81,7 @@ public function addViolation($message, array $params = []);
8181
* ->setTranslationDomain('number_validation')
8282
* ->addViolation();
8383
*
84-
* @param string $message The error message
84+
* @param string $message The error message as a string or a stringable object
8585
* @param array $parameters The parameters substituted in the error message
8686
*
8787
* @return ConstraintViolationBuilderInterface The violation builder

src/Symfony/Component/Validator/Tests/ConstraintViolationTest.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Validator\ConstraintViolation;
16+
use Symfony\Component\Validator\Tests\Fixtures\CustomArrayObject;
17+
use Symfony\Component\Validator\Tests\Fixtures\ToString;
1618

1719
class ConstraintViolationTest extends TestCase
1820
{
@@ -109,6 +111,52 @@ public function testToStringOmitsEmptyCodes()
109111
$this->assertSame($expected, (string) $violation);
110112
}
111113

114+
public function testMessageCanBeStringableObject()
115+
{
116+
$message = new ToString();
117+
$violation = new ConstraintViolation(
118+
$message,
119+
(string) $message,
120+
[],
121+
'Root',
122+
'property.path',
123+
null
124+
);
125+
126+
$expected = <<<'EOF'
127+
Root.property.path:
128+
toString
129+
EOF;
130+
$this->assertSame($expected, (string) $violation);
131+
$this->assertSame($message, $violation->getMessage());
132+
}
133+
134+
public function testMessageCannotBeArray()
135+
{
136+
$this->expectException(\TypeError::class);
137+
$violation = new ConstraintViolation(
138+
['cannot be an array'],
139+
'',
140+
[],
141+
'Root',
142+
'property.path',
143+
null
144+
);
145+
}
146+
147+
public function testMessageObjectMustBeStringable()
148+
{
149+
$this->expectException(\TypeError::class);
150+
$violation = new ConstraintViolation(
151+
new CustomArrayObject(),
152+
'',
153+
[],
154+
'Root',
155+
'property.path',
156+
null
157+
);
158+
}
159+
112160
/**
113161
* @group legacy
114162
* @expectedDeprecation Not using a string as the error code in Symfony\Component\Validator\ConstraintViolation::__construct() is deprecated since Symfony 4.4. A type-hint will be added in 5.0.

src/Symfony/Component/Validator/Violation/ConstraintViolationBuilder.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,10 @@ class ConstraintViolationBuilder implements ConstraintViolationBuilderInterface
4545
private $cause;
4646

4747
/**
48+
* @param string $message The error message as a string or a stringable object
4849
* @param TranslatorInterface $translator
4950
*/
50-
public function __construct(ConstraintViolationList $violations, Constraint $constraint, ?string $message, array $parameters, $root, string $propertyPath, $invalidValue, $translator, string $translationDomain = null)
51+
public function __construct(ConstraintViolationList $violations, Constraint $constraint, $message, array $parameters, $root, string $propertyPath, $invalidValue, $translator, string $translationDomain = null)
5152
{
5253
if (null === $message) {
5354
@trigger_error(sprintf('Passing a null message when instantiating a "%s" is deprecated since Symfony 4.4.', __CLASS__), E_USER_DEPRECATED);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy