Skip to content

Unique entity custom message #16345

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 1 commit 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 @@ -470,4 +470,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 @@ -12,11 +12,11 @@
namespace Symfony\Bridge\Doctrine\Validator\Constraints;

use Doctrine\Common\Persistence\ManagerRegistry;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Constraint;
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 @@ -128,16 +128,24 @@ public function validate($entity, Constraint $constraint)
$errorPath = null !== $constraint->errorPath ? $constraint->errorPath : $fields[0];
$invalidValue = isset($criteria[$errorPath]) ? $criteria[$errorPath] : $criteria[$fields[0]];

if ($this->context instanceof ExecutionContextInterface) {
$this->context->buildViolation($constraint->message)
->atPath($errorPath)
->setInvalidValue($invalidValue)
->addViolation();
} else {
$this->buildViolation($constraint->message)
->atPath($errorPath)
->setInvalidValue($invalidValue)
->addViolation();
$parameters = array();

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

Choose a reason for hiding this comment

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

this check is a bad idea: when using translation keys, the params are needed, but they are not part of the key

Copy link
Author

Choose a reason for hiding this comment

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

@stof I don't understand ! Do you have an example please ?

Copy link
Author

Choose a reason for hiding this comment

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

ping @stof


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

Choose a reason for hiding this comment

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

you should probably explain why it is necessary

Copy link
Contributor

Choose a reason for hiding this comment

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

PropertyAccess component should be ship with standard edition right ? Why this check ?

Copy link
Member

Choose a reason for hiding this comment

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

@dupuchba You can use the Doctrine bridge without using the full framework.

Copy link
Contributor

Choose a reason for hiding this comment

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

ok, I am not sure if it's widespread but fair enough ! Thx @xabbuh

}

$accessor = PropertyAccess::createPropertyAccessor();

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

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