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

matthias_bauw’s picture

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:

  • Use the @Constraint annotation like I used ablove
  • There is no need to use the Constraint parameter in the entity annotation, seems like a wrong thing in docs
  • Put your constraint in /modules/custom/modulename/Plugin/Validator/Constraint. I couldn't get it to work in any other way
markwittens’s picture

Please note that the path to put the constraint in should be /modules/custom/modulename/src/Plugin/Validator/Constraint

acidaniel’s picture

HI, Im working with a custom field adding a custom unique validation, but I always get an error:

Drupal\Component\Plugin\Exception\PluginException: Plugin (api_documentation) instance class "Drupal\cl_test_rest_sphinx\Plugin\Validation\Constraint\ApiDocumentation" does not exist. in Drupal\Component\Plugin\Factory\DefaultFactory::getPluginClass() (line 97 of core/lib/Drupal/Component/Plugin/Factory/DefaultFactory.php).

I have in my custom module in the .module file:

<?php

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function cl_test_rest_sphinx_entity_bundle_field_info_alter(&$fields, \Drupal\Core\Entity\EntityTypeInterface $entity_type, $bundle) {
  if ($bundle === 'documentation') {
    if (isset($fields['field_machine_name'])) {
      $fields['field_machine_name']->addConstraint('api_documentation', []);
    }
  }
}

In the src/Plugin/Validation/Constraint/ApiDocumentation.php file:

<?php

namespace Drupal\cl_test_rest_sphinx\Plugin\Validaton\Constraint;

use Drupal\Core\Validation\Plugin\Validation\Constraint\UniqueFieldConstraint;
use Symfony\Component\Validator\Constraint;

/**
 * Prevent duplicated values on machine_name field.
 *
 * @Constraint(
 *   id = "api_documentation",
 *   label = @Translation("Unique Machine Name", context = "Validation"),
 * )
 */
//Here I tried to extend to Constraint and to UniqueFieldConstraint with same error.
class ApiDocumentation extends UniqueFieldConstraint  {
  public $message = 'The machine name for documentation %value already exist';
}

And in my In the src/Plugin/Validation/Constraint/ApiDocumentationValidator.php file:

<?php

namespace Drupal\cl_test_rest_sphinx\Plugin\Validaton\Constraint;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

class ApiDocumentationValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) {
    return $this->context->addViolation($constraint->message, array('%label' => 'test', '%value' => 'test'));

  }
}

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.

Jaypan’s picture

You've spelled 'validation' wrong in your namespaces.

acidaniel’s picture

It works, thanks for your help

Kuldeep K’s picture

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.

Jaypan’s picture

Well plugins work, so there must be something wrong with your code.