1

I have a custom field validation for a field made with paragraphs which isn't working anymore (perhaps after core update but not sure). When I debug the code with a breakpoint I can see the validation being run, the violation added, but the node is still saved and no error is shown.

I have created a new validation to get the basics up and running again, trying to wrap my head around it, but with no luck. Could anyone tell me what the problem is?

Core version: 8.4.4

mymodule.module

/**
 * Implements hook_entity_bundle_field_info_alter().
 */
function mymodule_entity_bundle_field_info_alter(&$fields, EntityTypeInterface $entity_type, $bundle) {
  if ($entity_type->id() == 'node') {
    if (!empty($fields['title'])) {
      $fields['title']->addConstraint('sdvTitleConstraint', []);
    }
  }
}

src/Plugin/Validation/Constraint/sdvTitleConstraint.php

namespace Drupal\mymodule\Plugin\Validation\Constraint;

use Symfony\Component\Validator\Constraint;

/**
 * Checks the title.
 *
 * @Constraint(
 *   id = "sdvTitleConstraint",
 *   label = @Translation("Paragraphs", context = "Validation"),
 * )
 */
class sdvTitleConstraint extends Constraint {
  public $message = 'Title error';
}

src/Plugin/Validation/Constraint/sdvTitleConstraintValidator.php

namespace Drupal\mymodule\Plugin\Validation\Constraint;

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

/**
 * Validates the title constraint.
 */
class sdvTitleConstraintValidator extends ConstraintValidator {

  /**
   * {@inheritdoc}
   */
  public function validate($items, Constraint $constraint) {
    $this->context->addViolation($constraint->message);
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.