Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[TwigBridge][Validator] Add the Twig constraint and its validator#58805

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
fabpot merged 1 commit intosymfony:7.3fromsfmok:twig-validator
Mar 28, 2025

Conversation

sfmok
Copy link
Contributor

@sfmoksfmok commentedNov 7, 2024
edited by OskarStark
Loading

QA
Branch?7.3
Bug fix?no
New feature?yes
Deprecations?no
IssuesNo
LicenseMIT

Add a new constraint for validating Twig content (inspired from Yaml constraint)

OskarStark, simondaigre, sadok-f, and andreybolonin reacted with rocket emoji
@carsonbotcarsonbot added this to the7.2 milestoneNov 7, 2024
@sfmoksfmok changed the titleAdd the twig constraint and its validator[Validator] Add the twig constraint and its validatorNov 7, 2024
// Another syntax error example (unclosed variable)
['Hello {{ name', 'Unexpected token "end of template" ("end of print statement" expected) at line 1.', 1],
// Unknown filter error
['Hello {{ name | unknown_filter }}', 'Unknown "unknown_filter" filter at line 1.', 1],
Copy link
Member

Choose a reason for hiding this comment

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

Here, with the default environment, all the following would be invalid template, even if they are on most of the Symfony docs page.

{%form_themeformresources %}
{{name |trans }}
{%trans_default_domaindomain %}
{{object|serialize(format ='json',context = []) }}
{{text|humanize }}
{{name |trans }}

So ... what does "Twig valid" mean ?

derrabus and sfmok reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Resolved by injecting the environment, as suggested by@derrabus#58805 (comment)

@carsonbotcarsonbot changed the title[Validator] Add the twig constraint and its validatorAdd the twig constraint and its validatorNov 8, 2024
@carsonbotcarsonbot changed the titleAdd the twig constraint and its validator[TwigBridge][Validator] Add the twig constraint and its validatorNov 8, 2024
@stof
Copy link
Member

stof commentedNov 8, 2024

What is the use case for this ?

smnandre reacted with thumbs up emoji

@sfmok
Copy link
ContributorAuthor

What is the use case for this ?

@stof the constraint is intended to validate the Twig syntax of user-customized templates (e.g., emails, reports, widgets) to prevent syntax-related runtime errors.

@derrabus
Copy link
Member

The low-deps test failure appears to be caused by your changes.

// Another syntax error example (unclosed variable)
['Hello {{ name', 'Unexpected token "end of template" ("end of print statement" expected) at line 1.', 1],
// Unknown filter error
['Hello {{ name | unknown_filter }}', 'Unknown "unknown_filter" filter at line 1.', 1],
Copy link
Member

Choose a reason for hiding this comment

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

Yaml is a very defined syntax.

Here this is not the syntax that is validated, but the runtime Twig configuration.

And i still think it should not be exposed like this.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

I agree, but the thrown exception indicates a syntax error.
Seehttps://github.com/twigphp/Twig/blob/3.x/src/Error/SyntaxError.php

smnandre reacted with thumbs up emoji

$value = (string) $value;

$prevErrorHandler = set_error_handler(static function ($level, $message, $file, $line) use (&$prevErrorHandler) {
Copy link
Member

Choose a reason for hiding this comment

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

A deprecated function is not valid ?

Copy link
Member

Choose a reason for hiding this comment

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

If you don't want deprecated Twig syntax to be used in your templates, why not? But we should probably make this behavior configurable.

smnandre reacted with thumbs up emoji
Copy link
Member

Choose a reason for hiding this comment

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

As Twig is not aligned with Symfony release cycle, this seems a bit fragile to me, but why not with a flag, indeed.

derrabus reacted with thumbs up emoji
Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Good point. I've added theskipDeprecations option.

smnandre reacted with thumbs up emoji
['Hello {{ name }}'],
['{% if condition %}Yes{% else %}No{% endif %}'],
['{# Comment #}'],
['Hello {{ "world" | upper }}'],
Copy link
Member

@fabpotfabpotNov 10, 2024
edited by OskarStark
Loading

Choose a reason for hiding this comment

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

Suggested change
['Hello {{ "world" |upper }}'],
['Hello {{ "world"|upper }}'],

And everywhere else where you're using the| operator.

Copy link
ContributorAuthor

Choose a reason for hiding this comment

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

Done

@sfmoksfmokforce-pushed thetwig-validator branch 4 times, most recently fromf116b44 toaab17ecCompareNovember 10, 2024 17:13
@fabpotfabpot modified the milestones:7.2,7.3Nov 20, 2024
@sfmoksfmok requested a review fromfabpotNovember 30, 2024 14:16
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.2
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
7.2
7.3

7.2
---

* Added support for the new `twig` validator (from Twig Bridge)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
*Added support for thenew`twig` validator (from Twig Bridge)
*Add support for the`twig` validator

@@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate passing a tag to the constructor of `FormThemeNode`
* Add the `Twig` constraint for validating Twig content
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* Add the`Twig` constraint for validating Twigcontent
* Add the`Twig` constraint for validating Twigtemplates

@@ -5,6 +5,7 @@ CHANGELOG
---

* Deprecate passing a tag to the constructor of `FormThemeNode`
* Add the `Twig` constraint for validating Twig content
Copy link
Member

Choose a reason for hiding this comment

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

Should be moved to a new 7.3 section.

@OskarStarkOskarStark changed the title[TwigBridge][Validator] Add the twig constraint and its validator[TwigBridge][Validator] Add the Twig constraint and its validatorMar 26, 2025

#[HasNamedArguments]
public function __construct(
public string $message = 'This value is not valid Twig.',
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
public string$message ='This value is not valid Twig.',
public string$message ='This value is notavalid Twig template.',

@fabpot
Copy link
Member

Thank you@sfmok.

@fabpotfabpot merged commit6ae4c85 intosymfony:7.3Mar 28, 2025
2 of 3 checks passed
@sfmoksfmok deleted the twig-validator branchApril 6, 2025 19:01
javiereguiluz added a commit to symfony/symfony-docs that referenced this pull requestApr 10, 2025
…fmok)This PR was merged into the 7.3 branch.Discussion----------[Validator] Add docs for bridge twig validator#20836This pull request adds documentation for the new Twig Validator constraint introduced in the Twig Bridge.Changes:- Added a page to document the Twig validator constraint, including:- Installation instructions for the symfony/twig-bridge package.- Usage of the constraint in Symfony applications.- Detailed options available, including `message` and `skipDeprecations`.- Updated `index.rst` to include the new page under the "**Topics**" section for the **Twig Bridge.**Related Issue:[Issue#20836 - Add docs for Twig validator](#20836)Related PR:[Symfony PR #58805](symfony/symfony#58805) – Introduced the Twig validator constraint in the Twig Bridge.Commits-------ac9e20c Add docs for bridge twig validator#20836
@stof
Copy link
Member

The default error message needs to be added in the translation files, so that it gets translated in the project.

fabpot added a commit that referenced this pull requestApr 30, 2025
…bbuh)This PR was merged into the 6.4 branch.Discussion----------[Validator] add translations for the Twig constraint| Q             | A| ------------- | ---| Branch?       | 6.4| Bug fix?      | no| New feature?  | no| Deprecations? | no| Issues        || License       | MITadd missing translation file entries for#58805Commits-------f12ba2e add translations for the Twig constraint
@fabpotfabpot mentioned this pull requestMay 2, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@smnandresmnandresmnandre left review comments

@fabpotfabpotfabpot requested changes

@ycerutoycerutoAwaiting requested review from ycerutoyceruto is a code owner

@derrabusderrabusAwaiting requested review from derrabus

Assignees
No one assigned
Projects
None yet
Milestone
7.3
Development

Successfully merging this pull request may close these issues.

6 participants
@sfmok@stof@derrabus@fabpot@smnandre@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp