Skip to content

Instantly share code, notes, and snippets.

@markuspoerschke
Created September 7, 2012 17:30
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save markuspoerschke/3667963 to your computer and use it in GitHub Desktop.
Save markuspoerschke/3667963 to your computer and use it in GitHub Desktop.
SonataAdminBundle: Validate Unique
class ModelAdmin extend Admin
{
// ...
public function validate(ErrorElement $errorElement, $object)
{
// find object with the same uniqueField-value
$other = $this->modelManager->findOneBy($this->getClass(), array('uniqueField' => $object->getUniqueSlug()));
if (null !== $other && !$other->equals($object)) {
$errorElement
->with('uniqueField')
->addViolation('The unique field must be unique!')
->end();
}
}
}
@joffreycarle
Copy link

It should works better with an 's'.

class ModelAdmin extends Admin
{
[...]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment