Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d8e5122

Browse filesBrowse files
committed
minor #20532 [Validator] Add Slug constraint (tcoch)
This PR was squashed before being merged into the 7.3 branch. Discussion ---------- [Validator] Add Slug constraint Fix #20527 Commits ------- b4f8f6b [Validator] Add Slug constraint
2 parents 9cd4630 + b4f8f6b commit d8e5122
Copy full SHA for d8e5122

File tree

2 files changed

+120
-0
lines changed
Filter options

2 files changed

+120
-0
lines changed

‎reference/constraints/Slug.rst

Copy file name to clipboard
+119Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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

‎reference/constraints/map.rst.inc

Copy file name to clipboardExpand all lines: reference/constraints/map.rst.inc
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ String Constraints
3333
* :doc:`NotCompromisedPassword </reference/constraints/NotCompromisedPassword>`
3434
* :doc:`PasswordStrength </reference/constraints/PasswordStrength>`
3535
* :doc:`Regex </reference/constraints/Regex>`
36+
* :doc:`Slug </reference/constraints/Slug>`
3637
* :doc:`Ulid </reference/constraints/Ulid>`
3738
* :doc:`Url </reference/constraints/Url>`
3839
* :doc:`UserPassword </reference/constraints/UserPassword>`

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.