Updated: Comment #N

Follow-up from #2226813: Create a specific Plugin annotation for Drupal Constraint plugins.

Problem/Motivation

The split of Constraints into a plugin and a separate Constraint Validation class is unneeded for us and confusing.

Proposed resolution

Create a base plugin that implements Symfony\Component\Validator\ConstraintValidatorInterface

Override public function validatedBy() in the base plugin class so it returns the same class name.

Remaining tasks

TODO

User interface changes

N/A

API changes

separate classes will be combined into one.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

pwolanin’s picture

Title: Combine Drupal Constrain and ConstrainValidation classes into one » Combine Drupal Constraint and ConstraintValidation classes into one
pwolanin’s picture

Issue tags: +sudden outbreak of common sense, +happy chx

tagging

pwolanin’s picture

Status: Active » Needs work
FileSize
1.3 KB

Here's a possible base class

pwolanin’s picture

FileSize
1.35 KB

Fix a couple items

fago’s picture

This moves us further away from symfony best practices, but I agree it makes more sense for Drupal. So let's see what we can do.

+++ b/core/lib/Drupal/Core/Validation/Plugin/Validation/Constraint/ConstraintBase.php
@@ -0,0 +1,43 @@
+  abstract public function validate($value, Constraint $constraint);

This seems reasonable. But actually, the passed constraint will be $this then. Can we make it so that this does not confuse devs, e.g. hide it away by forwarding to another method?

pwolanin’s picture

Status: Needs work » Needs review
FileSize
4.42 KB

An example conversion - let's see what fails.

pwolanin’s picture

FileSize
4.58 KB

Here's an example adding a different abstract method to make things easier still.

The last submitted patch, 6: 2226821-3.patch, failed testing.

pwolanin’s picture

FileSize
5.99 KB
10 KB

a few more conversions

fago’s picture

Having a separate method seems great as we can do the is null check also - we've got the constraints that do the null checking already so there is no point in *any* contrib constraint going with null values also.

#2110345: Simplify validation constraint implementations for fields is finally passing again and should be ready, so let's get it in first and base this on it?

Status: Needs review » Needs work

The last submitted patch, 9: 2226821-9.patch, failed testing.

pwolanin’s picture

Status: Needs work » Needs review
Issue tags: +DX (Developer Experience)
FileSize
12.34 KB
22.34 KB

Looking through core, there are a few cases that cant be combined, like \Drupal\Core\Validation\Plugin\Validation\Constraint\AllowedValuesConstraintValidator because it's extending a Symfony validator, and we can't make the plugin extend 2 different base classes.

Status: Needs review » Needs work

The last submitted patch, 12: 2226821-12.patch, failed testing.

pwolanin’s picture

Hmm, that broke a lot - errors like Fatal error: Call to a member function id() on a non-object

jamesquinton’s picture

Assigned: Unassigned » jamesquinton
Issue tags: +Needs reroll

Re-rolling patch

jamesquinton’s picture

Successfully rolled back the patch, now testing and debugging.

jamesquinton’s picture

Assigned: jamesquinton » Unassigned
Status: Needs work » Needs review
Issue tags: -Needs reroll
FileSize
22.33 KB

Re-rolled patch and successfully run tests.

Status: Needs review » Needs work

The last submitted patch, 17: 2226821-15.patch, failed testing.

jamesquinton’s picture

Assigned: Unassigned » jamesquinton

Hmmm - the tests pass locally. Will take another look at this.

pwolanin’s picture

So, looking at one of the failures in Drupal\aggregator\Tests\AddFeedTest, I think it's since ComplexDataConstrainValidator has this method:

 public function getRequiredOptions() {
    return array('properties');
  }

when we return the class name from validatedBy() that calls is instantiated without passing in any "properties" in the options.

This is kind of a mismatch since we are returning the class name rather than the existing instance. Seems like symfony should take an instance instead of a class name?

Not sure how to unwind this api.

jamesquinton’s picture

Assigned: jamesquinton » Unassigned
pwolanin’s picture

So, it seem like we cannot do this is a consistent way - when a constraint has a required option we get a fatal exception when trying to make that class also the constraint validator since symfony constructs an instance without passing in any options.

In addition, we can't combine them in cases where both the constraint and validator would need to extend another class.

So, while the need for 2 classes is annoying, I think having a inconsistent implementation would be worse.

pwolanin’s picture

Status: Needs work » Closed (won't fix)
fago’s picture

I see, that sucks :-/

pwolanin’s picture

Issue tags: -sudden outbreak of common sense, -happy chx

sad chx...

fago’s picture

Status: Closed (won't fix) » Active

#2235457: Use link field for shortcut entity has just added a LinkTypeConstraint in a single class. Thus re-opening this for discussion.

However, I'm not sure it's viable to do it as done by LinkTypeConstraint. Reasons:
- validate() receives another instance of the same class as argument, instead of reading from $this, that's weird.
- As soon as you've got a required option, this approach would stop working.

So instead, could we allow having a single constraint class/service with the help of pre-defined validator class acting as adapter? It could just invoke validate() on constraints implementing ValidatingConstraintInterface, which is basically ConstraintValidatorInterface minus the Constraint parameter. Then, you can read your settings from $this and it would continue to work even with required options, as no other instance will be created?

fago’s picture

Besides that I figured that symfony documents to support services as validators, but the validator and our code lacks this feature. Looking closer, I figured it's added in by symfony framework:

https://github.com/symfony/symfony/blob/d94d837e9ea75d76eeb0a43e0535a5e7...

So adding something like that in as well seems to be a good idea also.

Version: 8.0.x-dev » 8.1.x-dev

Drupal 8.0.6 was released on April 6 and is the final bugfix release for the Drupal 8.0.x series. Drupal 8.0.x will not receive any further development aside from security fixes. Drupal 8.1.0-rc1 is now available and sites should prepare to update to 8.1.0.

Bug reports should be targeted against the 8.1.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.9 was released on September 7 and is the final bugfix release for the Drupal 8.1.x series. Drupal 8.1.x will not receive any further development aside from security fixes. Drupal 8.2.0-rc1 is now available and sites should prepare to upgrade to 8.2.0.

Bug reports should be targeted against the 8.2.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.6 was released on February 1, 2017 and is the final full bugfix release for the Drupal 8.2.x series. Drupal 8.2.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.3.0 on April 5, 2017. (Drupal 8.3.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.3.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.6 was released on August 2, 2017 and is the final full bugfix release for the Drupal 8.3.x series. Drupal 8.3.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.4.0 on October 4, 2017. (Drupal 8.4.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.4.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.5.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.4.x-dev » 8.5.x-dev

Drupal 8.4.4 was released on January 3, 2018 and is the final full bugfix release for the Drupal 8.4.x series. Drupal 8.4.x will not receive any further development aside from critical and security fixes. Sites should prepare to update to 8.5.0 on March 7, 2018. (Drupal 8.5.0-alpha1 is available for testing.)

Bug reports should be targeted against the 8.5.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.6.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.5.x-dev » 8.6.x-dev

Drupal 8.5.6 was released on August 1, 2018 and is the final bugfix release for the Drupal 8.5.x series. Drupal 8.5.x will not receive any further development aside from security fixes. Sites should prepare to update to 8.6.0 on September 5, 2018. (Drupal 8.6.0-rc1 is available for testing.)

Bug reports should be targeted against the 8.6.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.7.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.6.x-dev » 8.8.x-dev

Drupal 8.6.x will not receive any further development aside from security fixes. Bug reports should be targeted against the 8.8.x-dev branch from now on, and new development or disruptive changes should be targeted against the 8.9.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.8.x-dev » 8.9.x-dev

Drupal 8.8.7 was released on June 3, 2020 and is the final full bugfix release for the Drupal 8.8.x series. Drupal 8.8.x will not receive any further development aside from security fixes. Sites should prepare to update to Drupal 8.9.0 or Drupal 9.0.0 for ongoing support.

Bug reports should be targeted against the 8.9.x-dev branch from now on, and new development or disruptive changes should be targeted against the 9.1.x-dev branch. For more information see the Drupal 8 and 9 minor version schedule and the Allowed changes during the Drupal 8 and 9 release cycles.

Version: 8.9.x-dev » 9.2.x-dev

Drupal 8 is end-of-life as of November 17, 2021. There will not be further changes made to Drupal 8. Bugfixes are now made to the 9.3.x and higher branches only. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.2.x-dev » 9.3.x-dev
catch’s picture

Category: Bug report » Task
Related issues: +#3054535: Discuss whether to decouple from Symfony Validator

I think this got even stricter in Symfony since this issue was opened. We probably need to do it alongside #3054535: Discuss whether to decouple from Symfony Validator. Moving to a task because it's mostly an API annoyance, very annoying though it is.

Version: 9.3.x-dev » 9.4.x-dev

Drupal 9.3.15 was released on June 1st, 2022 and is the final full bugfix release for the Drupal 9.3.x series. Drupal 9.3.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.4.x-dev branch from now on, and new development or disruptive changes should be targeted for the 9.5.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.4.x-dev » 9.5.x-dev

Drupal 9.4.9 was released on December 7, 2022 and is the final full bugfix release for the Drupal 9.4.x series. Drupal 9.4.x will not receive any further development aside from security fixes. Drupal 9 bug reports should be targeted for the 9.5.x-dev branch from now on, and new development or disruptive changes should be targeted for the 10.1.x-dev branch. For more information see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.