Skip to content

[Validator] Allow objects implementing __toString() to be used as violation messages #31083

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2019

Conversation

mdlutz24
Copy link
Contributor

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.

@mdlutz24 mdlutz24 changed the title Stringableviolations [Validator] Allow objects implementing __toString() to be used as violation messages Apr 12, 2019
@xabbuh xabbuh added this to the next milestone Apr 12, 2019
@alexpott
Copy link
Contributor

@michaelcullum and I talked a bit around this issue and whether or not we should be pushing for a Stringable pseudo-class in PHP - there are prior discussions - see https://externals.io/message/98424

@xabbuh
Copy link
Member

xabbuh commented Apr 13, 2019

I am 👎 on making this change as it calls for breaking existing code that do not perform explicit string casts on the returned value of getMessage().

@nicolas-grekas
Copy link
Member

What are the alternatives on your side @alexpott @mdlutz24?

@alexpott
Copy link
Contributor

@nicolas-grekas none of them are particularly pretty - https://www.drupal.org/project/drupal/issues/3029540 - we've explored quite a few there.

@ro0NL
Copy link
Contributor

ro0NL commented Apr 17, 2019

does the issue affect drupal users directly? (if not i think drupal core should just patch)

e.g.

* @param string $message The error message
* @param array $params The parameters substituted in the error message
*/
public function addViolation($message, array $params = []);

is the issue drupal users do add/buildViolation(t('message')) at this point? Is it possible for drupal to provide its own ExecutionContextInterface?

@nicolas-grekas nicolas-grekas changed the base branch from master to 4.4 July 30, 2019 19:22
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have to do this change. Let's be friendly to our users. It's the only pragmatic move.
Here are some CS changes before approving on my side.

@nicolas-grekas
Copy link
Member

(please rebase also, on branch 4.4)

@ro0NL
Copy link
Contributor

ro0NL commented Jul 30, 2019

note in 5.0 there are more typehints that could be blocking still;

public function addViolation(string $message, array $params = []);

public function __construct(ConstraintViolationList $violations, Constraint $constraint, string $message, array $parameters, $root, $propertyPath, $invalidValue, TranslatorInterface $translator, $translationDomain = null)

@nicolas-grekas
Copy link
Member

nicolas-grekas commented Jul 30, 2019

@ro0NL thanks, then let's add as a string or a stringable object to all places where this would block lazy messages, in 4.4. IMHO.

@mdlutz24 mdlutz24 force-pushed the stringableviolations branch from 59c08ed to 58424ea Compare July 30, 2019 20:59
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please have a look at tests :)

Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good on my side (just one very minor CS comment)
Thanks for being so reactive.

@nicolas-grekas
Copy link
Member

(rebase needed)

…lation messages

[Validator] updated changelog

[Validator] updated typehint for ConstraintViolationInterface::getMessage()

[Validator] fixed spacing issue inadvertantly added in previous commit

[Validator] fixed coding standard issues

[Validator] Address feedback

[Validator] Fix coding standard violation

[Validator] update tests

[Validator] Address feedback, Round 2

[Validator] Document ConstraintViolationBuilder::__construct()  parameter

[Validator] Update changelog

[Validator] Adjust parameter documentation order
@mdlutz24 mdlutz24 force-pushed the stringableviolations branch from a0da99d to 79f4dcd Compare August 8, 2019 17:19
@mdlutz24
Copy link
Contributor Author

mdlutz24 commented Aug 8, 2019

Rebased and squashed

@fabpot
Copy link
Member

fabpot commented Aug 9, 2019

Thank you @mdlutz24.

@fabpot fabpot merged commit 79f4dcd into symfony:4.4 Aug 9, 2019
fabpot added a commit that referenced this pull request Aug 9, 2019
… 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants