Created
May 26, 2011 22:45
-
-
Save stof/994273 to your computer and use it in GitHub Desktop.
Using custom constraints
This file contains hidden or 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
<?php | |
namespace Acme\DemoBundle\Entity; | |
use Symfony\Component\Security\Core\User\UserInterface; | |
use Doctrine\ORM\Mapping as ORM; | |
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; | |
/** | |
* @ORM\Entity | |
* @UniqueEntity(fields="username", message="The username is already used") | |
*/ | |
abstract class User implements UserInterface | |
{ | |
/** | |
* @var string | |
* @ORM\Column(unique=true) | |
*/ | |
protected $username; |
This file contains hidden or 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
<!-- First solution: FQCN --> | |
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping | |
http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd"> | |
<class name="Acme\DemoBundle\Entity\User"> | |
<constraint name="Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity"> | |
<option name="fields">username</option> | |
<option name="message">The username is already used.</option> | |
</constraint> | |
</class> | |
</constraint-mapping> | |
<!-- Second solution: using a prefix --> | |
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | |
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping | |
http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd"> | |
<namespace prefix="orm">Symfony\Bridge\Doctrine\Validator\Constraints\</namespace> | |
<class name="Acme\DemoBundle\Entity\User"> | |
<constraint name="orm:UniqueEntity"> | |
<option name="fields">username</option> | |
<option name="message">The username is already used.</option> | |
</constraint> | |
</class> | |
</constraint-mapping> |
This file contains hidden or 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
# First solution: FQCN | |
Acme\DemoBundle\Entity\User: | |
constraints: | |
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity: { fields: username, message: The username is already used. } | |
# Second solution: using a prefix | |
namespaces: | |
orm: Symfony\Bridge\Doctrine\Validator\Constraints\ | |
Acme\DemoBundle\Entity\User: | |
constraints: | |
- orm:UniqueEntity: { fields: username, message: The username is already used. } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment