Score:2

addViolation() is deprecated: how to replace it?

sa flag

How to adapt the following code to avoid the deprecation of addViolation() according to https://www.drupal.org/node/3238432 ?

class MyValidator extends ConstraintValidator {

  public function validate($items, Constraint $constraint) {
    if (!$this->isValid($items)) {
      $this->context->addViolation($constraint->message); // How to adapt this line?
    }
  }
}
4uk4 avatar
cn flag
I think the deprecation message is a false alarm. In contrast to the methods mentioned in the change record addViolation() is not marked as internal in Symfony 6 and if Drupal removes this method they will replace it with Symfony code.
Baud avatar
sa flag
thank you. Is this means that the deprecation code has been falsely added to the addViolation() method... Some kind of Core bug I would say?
4uk4 avatar
cn flag
It's not falsely added, it's clear they want to remove this implementation of the method. This doesn't change the usage of the method, though, it is used the same way in Symfony.
Score:2
us flag

The code shown in the question doesn't need to be changed: Once Start using the class Symfony\Component\Validator\ValidatorBuilder instead of copied classes is fixed, $this->context won't contain anymore an instance of \Drupal\Core\TypedData\Validation\ExecutionContext but \Symfony\Component\Validator\Context\ExecutionContext.
As the documentation says, classes that extend ConstraintValidator should just assume that $this->context is an object implementing ExecutionContextInterface, not which class is actually used. With that assumption, the changes introduced by that issue won't influence contrain validator classes (except for the fact the deprecation message won't be anymore triggered).

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.