Skip to content

[Validator] Add Slug constraint #20532

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Check failure on line 67 in reference/constraints/Slug.rst

View workflow job for this annotation

GitHub Actions / Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\Validator\Constraints" does not exist
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
Loading