|
| 1 | +Uuid |
| 2 | +==== |
| 3 | + |
| 4 | +..versionadded::2.5 |
| 5 | + The Uuid constraint was introduced in Symfony 2.5. |
| 6 | + |
| 7 | +Validates that a value is a valid `Universally unique identifier (UUID)`_ per `RFC 4122`_. |
| 8 | +By default, this will validate the format according to the RFC's guidelines, but this can |
| 9 | +be relaxed to accept non-standard UUIDs that other systems (like PostgreSQL) accept. |
| 10 | +UUID versions can also be restricted using a whitelist. |
| 11 | + |
| 12 | ++----------------+---------------------------------------------------------------------+ |
| 13 | +| Applies to|:ref:`property or method<validation-property-target>`| |
| 14 | ++----------------+---------------------------------------------------------------------+ |
| 15 | +| Options| - `message`_| |
| 16 | +|| - `strict`_| |
| 17 | +|| - `versions`_| |
| 18 | ++----------------+---------------------------------------------------------------------+ |
| 19 | +| Class|:class:`Symfony\\Component\\Validator\\Constraints\\Uuid`| |
| 20 | ++----------------+---------------------------------------------------------------------+ |
| 21 | +| Validator|:class:`Symfony\\Component\\Validator\\Constraints\\UuidValidator`| |
| 22 | ++----------------+---------------------------------------------------------------------+ |
| 23 | + |
| 24 | +Basic Usage |
| 25 | +----------- |
| 26 | + |
| 27 | +..configuration-block:: |
| 28 | + |
| 29 | + ..code-block::yaml |
| 30 | +
|
| 31 | +# src/UploadsBundle/Resources/config/validation.yml |
| 32 | +Acme\UploadsBundle\Entity\File: |
| 33 | +properties: |
| 34 | +identifier: |
| 35 | + -Uuid:~ |
| 36 | +
|
| 37 | + ..code-block::php-annotations |
| 38 | +
|
| 39 | + // src/Acme/UploadsBundle/Entity/File.php |
| 40 | + namespace Acme\UploadsBundle\Entity; |
| 41 | +
|
| 42 | + use Symfony\Component\Validator\Constraints as Assert; |
| 43 | +
|
| 44 | + class File |
| 45 | + { |
| 46 | + /** |
| 47 | + * @Assert\Uuid |
| 48 | + */ |
| 49 | + protected $identifier; |
| 50 | + } |
| 51 | +
|
| 52 | + ..code-block::xml |
| 53 | +
|
| 54 | +<!-- src/Acme/UploadsBundle/Resources/config/validation.xml--> |
| 55 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 56 | + <constraint-mappingxmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 57 | +xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 58 | +xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 59 | +
|
| 60 | + <classname="Acme\UploadsBundle\Entity\File"> |
| 61 | + <propertyname="identifier"> |
| 62 | + <constraintname="Uuid" /> |
| 63 | + </property> |
| 64 | + </class> |
| 65 | + </constraint-mapping> |
| 66 | +
|
| 67 | + ..code-block::php |
| 68 | +
|
| 69 | + // src/Acme/UploadsBundle/Entity/File.php |
| 70 | + namespace Acme\UploadsBundle\Entity; |
| 71 | +
|
| 72 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 73 | + use Symfony\Component\Validator\Constraints as Assert; |
| 74 | +
|
| 75 | + class File |
| 76 | + { |
| 77 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 78 | + { |
| 79 | + $metadata->addPropertyConstraint('identifier', new Assert\Uuid()); |
| 80 | + } |
| 81 | + } |
| 82 | +
|
| 83 | +
|
| 84 | +Options |
| 85 | +------- |
| 86 | + |
| 87 | +message |
| 88 | +~~~~~~~ |
| 89 | + |
| 90 | +**type**: ``string`` **default**: ``This is not a valid UUID.`` |
| 91 | + |
| 92 | +This message is shown if the string is not a valid UUID. |
| 93 | + |
| 94 | +strict |
| 95 | +~~~~~~ |
| 96 | + |
| 97 | +**type**: ``boolean`` **default**: ``true`` |
| 98 | + |
| 99 | +If this option is set to ``true`` the constraint will check if the UUID is formatted per the |
| 100 | +RFC's input format rules: ``216fff40-98d9-11e3-a5e2-0800200c9a66``. Setting this to ``false`` |
| 101 | +will allow alternate input formats like: |
| 102 | + |
| 103 | +* ``216f-ff40-98d9-11e3-a5e2-0800-200c-9a66`` |
| 104 | +* ``{216fff40-98d9-11e3-a5e2-0800200c9a66}`` |
| 105 | +* ``216fff4098d911e3a5e20800200c9a66`` |
| 106 | + |
| 107 | +versions |
| 108 | +~~~~~~~~ |
| 109 | + |
| 110 | +**type**: ``int[]`` **default**: ``[1,2,3,4,5]`` |
| 111 | + |
| 112 | +This option can be used to only allow specific `UUID versions`_. Valid versions are 1 - 5. |
| 113 | +The following PHP constants can also be used: |
| 114 | + |
| 115 | +* ``Uuid::V1_MAC`` |
| 116 | +* ``Uuid::V2_DCE`` |
| 117 | +* ``Uuid::V3_MD5`` |
| 118 | +* ``Uuid::V4_RANDOM`` |
| 119 | +* ``Uuid::V5_SHA1`` |
| 120 | + |
| 121 | +All five versions are allowed by default. |
| 122 | + |
| 123 | +.. _`Universally unique identifier (UUID)`:http://en.wikipedia.org/wiki/Universally_unique_identifier |
| 124 | +.. _`RFC 4122`:http://tools.ietf.org/html/rfc4122 |
| 125 | +.. _`UUID versions`:http://en.wikipedia.org/wiki/Universally_unique_identifier#Variants_and_versions |