@@ -11,6 +11,7 @@ Validates that a value is a valid URL string.
11
11
| | - `payload `_ |
12
12
| | - `checkDNS `_ |
13
13
| | - `dnsMessage `_ |
14
+ | | - `relativeProtocol `_ |
14
15
+----------------+---------------------------------------------------------------------+
15
16
| Class | :class: `Symfony\\ Component\\ Validator\\ Constraints\\ Url ` |
16
17
+----------------+---------------------------------------------------------------------+
@@ -382,3 +383,77 @@ DNS check failed.
382
383
)));
383
384
}
384
385
}
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