|
| 1 | +Slug |
| 2 | +==== |
| 3 | + |
| 4 | +.. versionadded:: 7.3 |
| 5 | + |
| 6 | + The ``Slug`` constraint was introduced in Symfony 7.3. |
| 7 | + |
| 8 | +Validates that a value is a slug. |
| 9 | +A slug is a string that, by default, matches the regex ``/^[a-z0-9]+(?:-[a-z0-9]+)*$/``. |
| 10 | + |
| 11 | +.. include:: /reference/constraints/_empty-values-are-valid.rst.inc |
| 12 | + |
| 13 | +========== =================================================================== |
| 14 | +Applies to :ref:`property or method <validation-property-target>` |
| 15 | +Class :class:`Symfony\\Component\\Validator\\Constraints\\Slug` |
| 16 | +Validator :class:`Symfony\\Component\\Validator\\Constraints\\SlugValidator` |
| 17 | +========== =================================================================== |
| 18 | + |
| 19 | +Basic Usage |
| 20 | +----------- |
| 21 | + |
| 22 | +The ``Slug`` constraint can be applied to a property or a "getter" method: |
| 23 | + |
| 24 | +.. configuration-block:: |
| 25 | + |
| 26 | + .. code-block:: php-attributes |
| 27 | +
|
| 28 | + // src/Entity/Author.php |
| 29 | + namespace App\Entity; |
| 30 | +
|
| 31 | + use Symfony\Component\Validator\Constraints as Assert; |
| 32 | +
|
| 33 | + class Author |
| 34 | + { |
| 35 | + #[Assert\Slug] |
| 36 | + protected string $slug; |
| 37 | + } |
| 38 | +
|
| 39 | + .. code-block:: yaml |
| 40 | +
|
| 41 | + # config/validator/validation.yaml |
| 42 | + App\Entity\Author: |
| 43 | + properties: |
| 44 | + slug: |
| 45 | + - Slug: ~ |
| 46 | +
|
| 47 | + .. code-block:: xml |
| 48 | +
|
| 49 | + <!-- config/validator/validation.xml --> |
| 50 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 51 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 52 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 53 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 54 | +
|
| 55 | + <class name="App\Entity\Author"> |
| 56 | + <property name="slug"> |
| 57 | + <constraint name="Slug"/> |
| 58 | + </property> |
| 59 | + </class> |
| 60 | + </constraint-mapping> |
| 61 | +
|
| 62 | + .. code-block:: php |
| 63 | +
|
| 64 | + // src/Entity/Author.php |
| 65 | + namespace App\Entity; |
| 66 | +
|
| 67 | + use Symfony\Component\Validator\Constraints as Assert; |
| 68 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 69 | +
|
| 70 | + class Author |
| 71 | + { |
| 72 | + // ... |
| 73 | +
|
| 74 | + public static function loadValidatorMetadata(ClassMetadata $metadata): void |
| 75 | + { |
| 76 | + $metadata->addPropertyConstraint('slug', new Assert\Slug()); |
| 77 | + } |
| 78 | + } |
| 79 | +
|
| 80 | +Examples of valid values : |
| 81 | + |
| 82 | +* foobar |
| 83 | +* foo-bar |
| 84 | +* foo123 |
| 85 | +* foo-123bar |
| 86 | + |
| 87 | +Upper case characters would result in an violation of this constraint. |
| 88 | + |
| 89 | +Options |
| 90 | +------- |
| 91 | + |
| 92 | +``regex`` |
| 93 | +~~~~~~~~~ |
| 94 | + |
| 95 | +**type**: ``string`` default: ``/^[a-z0-9]+(?:-[a-z0-9]+)*$/`` |
| 96 | + |
| 97 | +This option allows you to modify the regular expression pattern that the input will be matched against |
| 98 | +via the :phpfunction:`preg_match` PHP function. |
| 99 | + |
| 100 | +If you need to use it, you might also want to take a look at the :doc:`Regex constraint <Regex>`. |
| 101 | + |
| 102 | +``message`` |
| 103 | +~~~~~~~~~~~ |
| 104 | + |
| 105 | +**type**: ``string`` **default**: ``This value is not a valid slug`` |
| 106 | + |
| 107 | +This is the message that will be shown if this validator fails. |
| 108 | + |
| 109 | +You can use the following parameters in this message: |
| 110 | + |
| 111 | +================= ============================================================== |
| 112 | +Parameter Description |
| 113 | +================= ============================================================== |
| 114 | +``{{ value }}`` The current (invalid) value |
| 115 | +================= ============================================================== |
| 116 | + |
| 117 | +.. include:: /reference/constraints/_groups-option.rst.inc |
| 118 | + |
| 119 | +.. include:: /reference/constraints/_payload-option.rst.inc |
0 commit comments