Skip to content

Unique entity custom message #15201

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

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,67 @@ public function testEntityManagerNullObject()

$this->validator->validate($entity, $constraint);
}

public function testCustomMessageWithOneUniqueField()
{
$constraint = new UniqueEntity(array(
'message' => 'An entity with name "{{ name }}" already exists.',
'fields' => array('name'),
'em' => self::EM_NAME,
));

$entity1 = new SingleIntIdEntity(1, 'Foo');
$entity2 = new SingleIntIdEntity(2, 'Foo');

$this->validator->validate($entity1, $constraint);

$this->assertNoViolation();

$this->em->persist($entity1);
$this->em->flush();

$this->validator->validate($entity1, $constraint);

$this->assertNoViolation();

$this->validator->validate($entity2, $constraint);

$this->buildViolation('An entity with name "{{ name }}" already exists.')
->setParameters(array('{{ name }}' => 'Foo'))
->atPath('property.path.name')
->setInvalidValue('Foo')
->assertRaised();
}

public function testCustomMessageWithTwoUniqueFields()
{
$constraint = new UniqueEntity(array(
'message' => 'An entity with name1 "{{ name }}" and name2 "{{ name2 }}" already exists.',
'fields' => array('name', 'name2'),
'em' => self::EM_NAME,
'errorPath' => 'name2',
));

$entity1 = new DoubleNameEntity(1, 'Foo', 'Bar');
$entity2 = new DoubleNameEntity(2, 'Foo', 'Bar');

$this->validator->validate($entity1, $constraint);

$this->assertNoViolation();

$this->em->persist($entity1);
$this->em->flush();

$this->validator->validate($entity1, $constraint);

$this->assertNoViolation();

$this->validator->validate($entity2, $constraint);

$this->buildViolation('An entity with name1 "{{ name }}" and name2 "{{ name2 }}" already exists.')
->setParameters(array('{{ name }}' => 'Foo', '{{ name2 }}' => 'Bar'))
->atPath('property.path.name2')
->setInvalidValue('Bar')
->assertRaised();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\PropertyAccess\PropertyAccess;

/**
* Unique Entity Validator checks if one or a set of fields contain unique values.
Expand Down Expand Up @@ -134,7 +135,23 @@ public function validate($entity, Constraint $constraint)
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]];

$this->buildViolation($constraint->message)
$vars = array();
$paramaters = array();

if (preg_match_all('/{{ ([a-zA-Z0-9_]+) }}/', $constraint->message, $vars) > 0) {

if (!class_exists('Symfony\\Component\\PropertyAccess\\PropertyAccess')) {
throw new \RuntimeException('Unable to access entity property as the Symfony PropertyAccess is not installed.');
}

$accessor = PropertyAccess::createPropertyAccessor();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to make sure that the PropertyAccess component is actually available and you should add it to the suggest section in composer.json.


foreach ($vars[1] as $var) {
$paramaters[sprintf('{{ %s }}', $var)] = $accessor->getValue($entity, $var);
}
}

$this->buildViolation($constraint->message, $paramaters)
->atPath($errorPath)
->setInvalidValue($invalidValue)
->addViolation();
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@
"symfony/validator": "",
"doctrine/data-fixtures": "",
"doctrine/dbal": "",
"doctrine/orm": ""
"doctrine/orm": "",
"symfony/property-access": ""
},
"autoload": {
"psr-0": { "Symfony\\Bridge\\Doctrine\\": "" }
Expand Down