Replies: 1 comment 3 replies
-
My suggestion would be to create a custom constraint, for which the ConstraintValidator would reuse the UniqueEntity with the appropriate configuration: use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class MyUniqueEntityValidator extends ConstraintValidator
{
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof MyUniqueEntity) {
throw new UnexpectedTypeException($constraint, MyUniqueEntity::class);
}
$em = 'foo'; // Put the logic to choose the entity manager here
$this->context->getValidator()
->inContext($this->context)
->validate($value, new UniqueEntity(em: $em));
}
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using Symfony 5.4 LTS. UniqueEntity provides the exact validation I need. Unfortunately,
em
needs to be hard-coded and that doesn't work for me because mine is a multi-tenancy application. Validation crashes with a SQL error because it invokes the right repository method in the wrong EM.I have an injectable service class that provides access to the right EM, but
MyEntityClass::loadValidatorMetadata
is a static method and I don't see an obvious way to use DI from there.Is there a clean way to use this builtin constraint in my case?
Beta Was this translation helpful? Give feedback.
All reactions