8000 minor #9278 Documented the new relativeProtocol option of Url constra… · symfony/symfony-docs@45ba13c · GitHub
[go: up one dir, main page]

Skip to content

Commit 45ba13c

Browse files
committed
minor #9278 Documented the new relativeProtocol option of Url constraint (javiereguiluz)
This PR was squashed before being merged into the master branch (closes #9278). Discussion ---------- Documented the new relativeProtocol option of Url constraint This fixes #9266. Ping @MyDigitalLife: as the original contributor of this feature, could you please review if I documented it right? Thanks! (and thanks for contributing the feature too!) Commits ------- cc440d9 Documented the new relativeProtocol option of Url constraint
2 parents bfc455a + cc440d9 commit 45ba13c

File tree

1 file changed

+75
-0
lines changed

1 file changed

+75
-0
lines changed

reference/constraints/Url.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Validates that a value is a valid URL string.
1111
| | - `payload`_ |
1212
| | - `checkDNS`_ |
1313
| | - `dnsMessage`_ |
14+
| | - `relativeProtocol`_ |
1415
+----------------+---------------------------------------------------------------------+
1516
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Url` |
1617
+----------------+---------------------------------------------------------------------+
@@ -382,3 +383,77 @@ DNS check failed.
382383
)));
383384
}
384385
}
386+
387+
relativeProtocol
388+
~~~~~~~~~~~~~~~~
389+
390+
**type**: ``boolean`` **default**: ``false``
391+
392+
.. versionadded:: 4.1
393+
The ``relativeProtocol`` option was introduced in Symfony 4.1.
394+
395+
If ``true``, the protocol is considered optional when validating the syntax of
396+
the given URL. This means that both ``http://`` and ``https://`` are valid but
397+
also relative URLs that contain no protocol (e.g. ``//example.com``).
398+
399+
.. configuration-block::
400+
401+
.. code-block:: php-annotations
402+
403+
// src/Entity/Author.php
404+
namespace App\Entity;
405+
406+
use Symfony\Component\Validator\Constraints as Assert;
407+
408+
class Author
409+
{
410+
/**
411+
* @Assert\Url(
412+
* relativeProtocol = true
413+
* )
414+
*/
415+
protected $bioUrl;
416+
}
417+
418+
.. code-block:: yaml
419+
420+
# config/validator/validation.yaml
421+
App\Entity\Author:
422+
properties:
423+
bioUrl:
424+
- Url: { relativeProtocol: true }
425+
426+
.. code-block:: xml
427+
428+
<!-- config/validator/validation.xml -->
429+
<?xml version="1.0" encoding="UTF-8" ?>
430+
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
431+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
432+
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
433+
434+
<class name="App\Entity\Author">
435+
<property name="bioUrl">
436+
<constraint name="Url">
437+
<option name="relativeProtocol">true</option>
438+
</constraint>
439+
</property>
440+
</class>
441+
</constraint-mapping>
442+
443+
.. code-block:: php
444+
445+
// src/Entity/Author.php
446+
namespace App\Entity;
447+
448+
use Symfony\Component\Validator\Mapping\ClassMetadata;
449+
use Symfony\Component\Validator\Constraints as Assert;
450+
451+
class Author
452+
{
453+
public static function loadValidatorMetadata(ClassMetadata $metadata)
454+
{
455+
$metadata->addPropertyConstraint('bioUrl', new Assert\Url(array(
456+
'relativeProtocol' => true,
457+
)));
458+
}
459+
}

0 commit comments

Comments
 (0)
0