Created
September 7, 2012 17:30
-
-
Save markuspoerschke/3667963 to your computer and use it in GitHub Desktop.
SonataAdminBundle: Validate Unique
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It should works better with an 's'.