Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create UniqueEntityValidator #1

Open
robotccm opened this issue Nov 12, 2016 · 1 comment
Open

Create UniqueEntityValidator #1

robotccm opened this issue Nov 12, 2016 · 1 comment
Labels
enhancement New feature or request minor

Comments

@robotccm
Copy link

Original report by Xavier Leune (Bitbucket: [Xavier Leune](https://bitbucket.org/Xavier Leune), ).


Something like that does the job !

#!yml

UniqueEntity(fields={"username", "email"}, repository="\AppBundle\Association\Model\Repository\UserRepository")
#!php

<?php

namespace AppBundle\Validator\Constraints;

use CCMBenchmark\Ting\Repository\RepositoryFactory;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

class UniqueEntityValidator extends ConstraintValidator
{
    private $repositoryFactory;

    public function __construct(RepositoryFactory $repositoryFactory)
    {
        $this->repositoryFactory = $repositoryFactory;
    }

    /**
     * @inheritDoc
     */
    public function validate($entity, Constraint $constraint)
    {
        if (!$constraint instanceof UniqueEntity) {
            throw new UnexpectedTypeException($constraint, UniqueEntity::class);
        }
        $repository = $this->repositoryFactory->get($constraint->repository);

        $fields = (array)$constraint->fields;
        $criteria = [];
        foreach ($fields as $field) {
            $propertyName    = 'get' . $field;
            $criteria[$field] = $entity->$propertyName();
        }

        $myEntity = $repository->getOneBy($criteria);

        if ($myEntity !== null) {
            $this->context->buildViolation($constraint->message)
                ->setParameter('{{ data }}', implode(', ', $criteria))
                ->addViolation();
        }
    }
}

#!php

<?php


namespace AppBundle\Validator\Constraints;


use Symfony\Component\Validator\Constraint;

/**
 * Class UniqueEntity
 *
 * @Annotation
 * @Target({"CLASS", "ANNOTATION"})
 */
class UniqueEntity extends Constraint
{
    public $message = 'Another entity exists for this data: {{ data }}';
    public $repository;
    public $fields = array();

    public function getTargets()
    {
        return self::CLASS_CONSTRAINT;
    }

    public function getRequiredOptions()
    {
        return ['fields', 'repository'];
    }

    public function getDefaultOption()
    {
        return ['fields', 'repository'];
    }
}

@robotccm robotccm added minor enhancement New feature or request labels Oct 28, 2019
@Deuchnord
Copy link

Deuchnord commented Nov 22, 2021

@xavierleune looks like this has been done in 81aea89, hasn't it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request minor
Projects
None yet
Development

No branches or pull requests

2 participants