/** * {@inheritdoc} */ public function validate($entity, Constraint $constraint) { $className = $this->doctrineHelper->getEntityClass($entity); $organizationField = $this->metadataProvider->getMetadata($className)->getGlobalOwnerFieldName(); if ($organizationField) { $constraint->fields = array_merge((array) $constraint->fields, [$organizationField]); } parent::validate($entity, $constraint); }
/** * {@inheritdoc} */ public function validate($entity, Constraint $constraint) { if (!$constraint instanceof UniqueEntity) { throw new UnexpectedTypeException($constraint, __NAMESPACE__ . '\\UniqueEntity'); } $criticalError = false; $class = $this->entityManager->getClassMetadata(get_class($entity)); foreach ($constraint->fields as $fieldName) { $object = $class->reflFields[$fieldName]->getValue($entity); try { $this->entityManager->initializeObject($object); } catch (EntityNotFoundException $exception) { $this->context->addViolationAt($fieldName, $fieldName . ' does not exist'); $criticalError = true; } } if ($criticalError === false) { parent::validate($entity, $constraint); } }
public function testEntityManagerNullObject() { $uniqueFields = array('name'); $registry = $this->getMock('Doctrine\\Common\\Persistence\\ManagerRegistry'); $constraint = new UniqueEntity(array('fields' => $uniqueFields)); $uniqueValidator = new UniqueEntityValidator($registry); $entity = new SingleIntIdEntity(1, null); $this->setExpectedException('Symfony\\Component\\Validator\\Exception\\ConstraintDefinitionException', 'Unable to find the object manager associated with an entity of class "Symfony\\Bridge\\Doctrine\\Tests\\Fixtures\\SingleIntIdEntity"'); $uniqueValidator->validate($entity, $constraint); }