Skip to content

Commit

Permalink
minor #20532 [Validator] Add Slug constraint (tcoch)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
javiereguiluz committed Jan 7, 2025
2 parents 9cd4630 + b4f8f6b commit d8e5122
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
119 changes: 119 additions & 0 deletions reference/constraints/Slug.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
Slug
====

.. versionadded:: 7.3

The ``Slug`` constraint was introduced in Symfony 7.3.

Validates that a value is a slug.
A slug is a string that, by default, matches the regex ``/^[a-z0-9]+(?:-[a-z0-9]+)*$/``.

.. include:: /reference/constraints/_empty-values-are-valid.rst.inc

========== ===================================================================
Applies to :ref:`property or method <validation-property-target>`
Class :class:`Symfony\\Component\\Validator\\Constraints\\Slug`
Validator :class:`Symfony\\Component\\Validator\\Constraints\\SlugValidator`
========== ===================================================================

Basic Usage
-----------

The ``Slug`` constraint can be applied to a property or a "getter" method:

.. configuration-block::

.. code-block:: php-attributes
// src/Entity/Author.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Author
{
#[Assert\Slug]
protected string $slug;
}
.. code-block:: yaml
# config/validator/validation.yaml
App\Entity\Author:
properties:
slug:
- Slug: ~
.. code-block:: xml
<!-- config/validator/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<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 https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="App\Entity\Author">
<property name="slug">
<constraint name="Slug"/>
</property>
</class>
</constraint-mapping>
.. code-block:: php
// src/Entity/Author.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Mapping\ClassMetadata;
class Author
{
// ...
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('slug', new Assert\Slug());
}
}
Examples of valid values :

* foobar
* foo-bar
* foo123
* foo-123bar

Upper case characters would result in an violation of this constraint.

Options
-------

``regex``
~~~~~~~~~

**type**: ``string`` default: ``/^[a-z0-9]+(?:-[a-z0-9]+)*$/``

This option allows you to modify the regular expression pattern that the input will be matched against
via the :phpfunction:`preg_match` PHP function.

If you need to use it, you might also want to take a look at the :doc:`Regex constraint <Regex>`.

``message``
~~~~~~~~~~~

**type**: ``string`` **default**: ``This value is not a valid slug``

This is the message that will be shown if this validator fails.

You can use the following parameters in this message:

================= ==============================================================
Parameter Description
================= ==============================================================
``{{ value }}`` The current (invalid) value
================= ==============================================================

.. include:: /reference/constraints/_groups-option.rst.inc

.. include:: /reference/constraints/_payload-option.rst.inc
1 change: 1 addition & 0 deletions reference/constraints/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ String Constraints
* :doc:`NotCompromisedPassword </reference/constraints/NotCompromisedPassword>`
* :doc:`PasswordStrength </reference/constraints/PasswordStrength>`
* :doc:`Regex </reference/constraints/Regex>`
* :doc:`Slug </reference/constraints/Slug>`
* :doc:`Ulid </reference/constraints/Ulid>`
* :doc:`Url </reference/constraints/Url>`
* :doc:`UserPassword </reference/constraints/UserPassword>`
Expand Down

0 comments on commit d8e5122

Please sign in to comment.