Seems like a great feature to do validation on an entity level. But strangely enough I can't track down relevant documentation on this. I tried to build a custom constraint.
I used an annotation like this
/**
* @Constraint(
* id = "SubscriptionExists",
* label = @Translation("Is Subscription Unique", context = "Validation"),
* type = { "string", },
* )
*/
and then I used a SubscriptionExists class that extends class and I created a SubscriptionExistsValidator class
Then I adapted by Entity annotation adding the constraint there:
* constraints = {
* "SubscriptionExists" = {},
* },
But that is where it goes wrong. I keep getting the error
Drupal\Component\Plugin\Exception\PluginNotFoundException: The "SubscriptionExists" plugin does not exist. in Drupal\Core\Plugin\DefaultPluginManager->doGetDefinition() (line 57 of core/lib/Drupal/Component/Plugin/Discovery/DiscoveryTrait.php).
So I think the plugin is not detected. But I can't figure out why. I know I'm not giving much info on this one but I can't really tell where to start. I have put the files in my /modules/custom/modulename/src/plugins directory. Maybe it's like the entity api and I need to put the code in a certain directoryname to make them being detected.
Can somebody offer me some tips or tell me where I can find reference?
Thanks a lot
Matt.
Comments
Bingo!
Well...
Finally fixed it. I solved it by looking at the user entity definition and I have a few remarks that might come in handy:
Please note that the path to
Please note that the path to put the constraint in should be /modules/custom/modulename/src/Plugin/Validator/Constraint
How did you make it work?
HI, Im working with a custom field adding a custom unique validation, but I always get an error:
I have in my custom module in the .module file:
In the src/Plugin/Validation/Constraint/ApiDocumentation.php file:
And in my In the src/Plugin/Validation/Constraint/ApiDocumentationValidator.php file:
I expect to see the message when I save the node of type documentation but instead I get the error above, any clue? Also I tried to change the annotation as you suggestion but the same.
Any help will be very appreciated.
You've spelled 'validation'
You've spelled 'validation' wrong in your namespaces.
Crap ¬¬
It works, thanks for your help
Facing same issue
Defining the custom constraint followed by this doc https://www.drupal.org/docs/drupal-apis/entity-api/entity-validation-api... and using it to entity type field. It results the below error.
Drupal\Component\Plugin\Exception\PluginNotFoundException: The "VariableName" plugin does not exist.
Well plugins work, so there
Well plugins work, so there must be something wrong with your code.