Problem/Motivation

From #3027835-4: [symfony 4] Argument 1 passed to Symfony\Component\HttpFoundation\Response::setCharset() must be of the type string, null given:

The Symfony\Component\Validator\Constraints\Email::$strict property is deprecated since Symfony 4.1. Use Symfony\Component\Validator\Constraints\Email::mode="strict" instead.

Proposed resolution

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

Gábor Hojtsy created an issue. See original summary.

gábor hojtsy’s picture

Issue summary: View changes
berdir’s picture

Note from slack discussion:

This is a symfony 4 deprecation, we might not be able to do anything about that if we want to remain compatible with Symfony 3 but would have to add that to the excluded deprecation message list instead.

catch’s picture

Status: Active » Needs review
StatusFileSize
new159.93 KB
new534 bytes

So..

Drupal\Core\Validation\Plugin\Validation\Constraint\Email

does static $strict = TRUE;

Symfony's EmailValidator constructor looks for the static property on the constraint, and if it's there triggers the deprecation error. Symfony 3 doesn't support static $mode = 'strict'; which is the Symfony 4.1 way to do it.

However, it's not a problem if we set $mode there for Symfony 4 compatibility too, so we can add that to the existing class for forwards compatibility.

This leaves two options:

1. Override Symfony's EmailValidator to not trigger the deprecation when $strict is set, so that we can support both Symfony 3 and Symfony 4 without a deprecation message.

2. What Berdir says in #1

However I also want to check that we have test coverage that enforces we're actually using the 'strict' option, so here's a couple of patches that simply do the upgrade for Symfony 4 without taking into account the Symfony 3 API.

catch’s picture

StatusFileSize
new160.08 KB
new691 bytes

Ignore half the comment above, somehow convinced myself it was a static property for no reason...

Since it's not, we can just override the constraint constructor and conditionally define it I think. Quick patch for that to see how it goes.

The last submitted patch, 4: 3027871-symfony-4.patch, failed testing. View results

The last submitted patch, 5: 3027871-combined.patch, failed testing. View results

The last submitted patch, 4: 3027871-symfony-4-combined.patch, failed testing. View results

alexpott’s picture

I think we need to call the parent constructor so that \Symfony\Component\Validator\Constraint::__construct is called. We need to navigate https://github.com/symfony/validator/blob/master/Constraints/Email.php to not trigger a deprecation but that should be possible.

alexpott’s picture

Status: Needs review » Needs work

For #9

catch’s picture

Status: Needs work » Needs review
StatusFileSize
new1.08 KB
new1.12 KB
new1.15 KB

OK two versions (edit: actually three because I cross-posted before seeing the review and already had new patch written, but ignore the -9 patch):

1. Puts the constructor call up top then hard-codes the property as in the above patch.

2. Just uses the $options array to set the method via Constraint::__construct() which is further from our current code but closer to what I think Symfony actually wants us to do.

alexpott’s picture

I agree that 3027871-11-constructor.patch is closer to what Symfony envisages as there is specific code in the constructor to set object properties. Looks good to me. Let's see what the bot says.

One thought:

+++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EmailConstraint.php
@@ -16,7 +16,25 @@
+    else {
+      $options['strict'] = TRUE;
+    }

strictly I think we should only be setting this if it is not already set since atm if you passed ['strict' => FALSE] to the constructor is would override our <code>public $strict = TRUE; and we probably want to maintain that behaviour.

The last submitted patch, 11: 3027871-9.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

The last submitted patch, 11: 3027871-11.patch, failed testing. View results
- codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

catch’s picture

StatusFileSize
new1.2 KB

#12 is a good point, was going out of my way to do the opposite but you're right.

alexpott’s picture

Status: Needs review » Reviewed & tested by the community

#15 looks great.

catch’s picture

+++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EmailConstraint.php
@@ -16,7 +16,26 @@
+    // < 4.2. This compatibility later can be removed once Drupal requires

typo: s/later/layer/g - no time to re-roll now but could be fixed on commit.

alexpott’s picture

StatusFileSize
new937 bytes
new1.2 KB

Fixed #17 to make life simple.

jibran’s picture

Patch looks good. Nice work!

+++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EmailConstraint.php
@@ -16,7 +16,26 @@
+    // < 4.2. This compatibility layer can be removed once Drupal requires
+    // Symfony 4.2 or higher.

Let's add the issue ID.

catch’s picture

Do you mean this issue ID? If so I think git blame is enough. If not do you mean requiring Symfony 4? I don't think we have a specific issue for that yet but we could open one against 9.x maybe?

jibran’s picture

If not do you mean requiring Symfony 4?

Yes, let's create one.

catch’s picture

Status: Reviewed & tested by the community » Needs work

Took a look and we do actually have one #3009219: Update Symfony to 4.4 in Drupal 9.0. CNW to add a link to that in the comment, it'll make it easier to grep for some of these things when the time comes.

jibran’s picture

Status: Needs work » Needs review
StatusFileSize
new878 bytes
new1.24 KB

Here you go.

voleger’s picture

Status: Needs review » Reviewed & tested by the community
+++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/EmailConstraint.php
@@ -25,7 +25,7 @@ public function __construct($options = []) {
-    // Symfony 4.2 or higher.
+    // Symfony 4.2 or higher in https://www.drupal.org/node/3009219.

Yep, link to #3009219: Update Symfony to 4.4 in Drupal 9.0 added.

gábor hojtsy’s picture

Looks wonderful :)

gábor hojtsy’s picture

Status: Reviewed & tested by the community » Fixed

Had this problem, fixed on commit:

FILE: ...Core/Validation/Plugin/Validation/Constraint/EmailConstraint.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 37 | ERROR | [x] Expected 1 blank line after function; 2 found
----------------------------------------------------------------------

  • Gábor Hojtsy committed cb244d0 on 8.7.x
    Issue #3027871 by catch, alexpott, jibran, Gábor Hojtsy, Berdir: [...
gábor hojtsy’s picture

Title: [symfony 4] Symfony\Component\Validator\Constraints\Email::$strict property is deprecated since Symfony 4.1 » [symfony 5] Symfony\Component\Validator\Constraints\Email::$strict property is deprecated since Symfony 4.1
Issue tags: +Symfony 5

This is actually Symfony 5 compatibility :)

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.