Skip to content

Commit 748f989

Browse files
committed
[Validator] Add a requireTld option to Url constraint
1 parent 9b73ea3 commit 748f989

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

reference/constraints/Url.rst

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,109 @@ also relative URLs that contain no protocol (e.g. ``//example.com``).
307307
]));
308308
}
309309
}
310+
311+
``requireTld``
312+
~~~~~~~~~~~~~~
313+
314+
**type**: ``boolean`` **default**: ``false``
315+
316+
.. versionadded:: 7.1
317+
318+
The ``requiredTld`` option was introduced in Symfony 7.1.
319+
320+
By default, URLs like ``https://aaa`` or ``https://foobar`` are considered valid
321+
because they are tecnically valid according to the `URL spec`_. If you set this option
322+
to ``true``, the host part of the URL will have to include a TLD (top-level domain
323+
names): e.g. ``https://example.com`` will be valid but ``https://example`` won't.
324+
325+
.. note::
326+
327+
This constraint does not validate that the given TLD value is included in
328+
the `list of official top-level domains`_ (because that list is growing
329+
continuously and it's hard to keep track of it).
330+
331+
``tldMessage``
332+
~~~~~~~~~~~~~~
333+
334+
**type**: ``string`` **default**: ``TThis URL does not contain a TLD.``
335+
336+
.. versionadded:: 7.1
337+
338+
The ``tldMessage`` option was introduced in Symfony 7.1.
339+
340+
This message is shown if the URL does not contain at least one TLD.
341+
342+
You can use the following parameters in this message:
343+
344+
=============== ==============================================================
345+
Parameter Description
346+
=============== ==============================================================
347+
``{{ value }}`` The current (invalid) value
348+
``{{ label }}`` Corresponding form field label
349+
=============== ==============================================================
350+
351+
.. configuration-block::
352+
353+
.. code-block:: php-attributes
354+
355+
// src/Entity/Website.php
356+
namespace App\Entity;
357+
358+
use Symfony\Component\Validator\Constraints as Assert;
359+
360+
class Website
361+
{
362+
#[Assert\Url(
363+
tldMessage: 'Add at least one TLD to the {{ value }} URL.',
364+
)]
365+
protected string $homepageUrl;
366+
}
367+
368+
.. code-block:: yaml
369+
370+
# config/validator/validation.yaml
371+
App\Entity\Website:
372+
properties:
373+
homepageUrl:
374+
- Url:
375+
tldMessage: Add at least one TLD to the {{ value }} URL.
376+
377+
.. code-block:: xml
378+
379+
<!-- config/validator/validation.xml -->
380+
<?xml version="1.0" encoding="UTF-8" ?>
381+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
382+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
383+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping https://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
384+
385+
<class name="App\Entity\Website">
386+
<property name="homepageUrl">
387+
<constraint name="Url">
388+
<option name="tldMessage">Add at least one TLD to the {{ value }} URL.</option>
389+
</constraint>
390+
</property>
391+
</class>
392+
</constraint-mapping>
393+
394+
.. code-block:: php
395+
396+
// src/Entity/Website.php
397+
namespace App\Entity;
398+
399+
use Symfony\Component\Validator\Constraints as Assert;
400+
use Symfony\Component\Validator\Mapping\ClassMetadata;
401+
402+
class Website
403+
{
404+
// ...
405+
406+
public static function loadValidatorMetadata(ClassMetadata $metadata): void
407+
{
408+
$metadata->addPropertyConstraint('homepageUrl', new Assert\Url([
409+
'tldMessage' => 'Add at least one TLD to the {{ value }} URL.',
410+
]));
411+
}
412+
}
413+
414+
.. _`URL spec`: https://datatracker.ietf.org/doc/html/rfc1738
415+
.. _`list of official top-level domains`: https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains

0 commit comments

Comments
 (0)