Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Source Link
Baud
  • 542
  • 1
  • 11
  • 37

Constraint on paragraph field not working

Here is a very simple code:

1-My constraint plugin (MyConstraint.php)

/**
 * @Constraint(
 *   id = "MyConstraint",
 *   label = @Translation("Never Validated", context = "Validation"),
 *   type = "string"
 * )
 */
class MyConstraint extends Constraint {
  public $message = "error %value";
}

2-My constraint validator (MyConstraintValidator.php) [=>addViolation in all cases]

class MyConstraintValidator extends ConstraintValidator {

  public function validate($items, Constraint $constraint) {
    dpm("I am displayed");
    $this->context->addViolation($constraint->message, [
              '%value' => 10
            ]);
    return;
}

3-My constraint added to my bundle

function mymodule_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle)
{
    if($bundle=='my_bundle'){
      $fields['my_paragraph_field']->addConstraint('MyConstraint', []);
    }
}

Result:

1-The dpm message 'I am displayed' is displayed

2-The entity is saved without any validation error

Notes:

-I have other constraints which are working well, If I test one of them (on my_bundle), the violation of this working constraint will be displayed but not the one with the code detailled here.

-The problematic constraint is added to a paragraph field... I suspect the problem is coming from there

-Nothing in the error logs.

I am looking for a hint to where to look.