|
| 1 | +Json |
| 2 | + |
| 3 | +Validates that a value is valid ``json``. Specifically, this checks to see if |
| 4 | +the value is a valid ``json`` or not. |
| 5 | + |
| 6 | ++----------------+-----------------------------------------------------------------------+ |
| 7 | +| Applies to | :ref:`property or method <validation-property-target>` | |
| 8 | ++----------------+-----------------------------------------------------------------------+ |
| 9 | +| Options | - `message`_ | |
| 10 | +| | - `payload`_ | |
| 11 | ++----------------+-----------------------------------------------------------------------+ |
| 12 | +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Json` | |
| 13 | ++----------------+-----------------------------------------------------------------------+ |
| 14 | +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\JsonValidator` | |
| 15 | ++----------------+-----------------------------------------------------------------------+ |
| 16 | + |
| 17 | +Basic Usage |
| 18 | +----------- |
| 19 | + |
| 20 | +The ``Json`` constraint can be applied to a property or a "getter" method, |
| 21 | +but is most commonly useful in the latter case. For example, suppose that |
| 22 | +you want to guarantee that some ``jsonString`` property is valid JSON. |
| 23 | + |
| 24 | +.. configuration-block:: |
| 25 | + |
| 26 | + .. code-block:: php-annotations |
| 27 | +
|
| 28 | + // src/Entity/Book.php |
| 29 | + namespace App\Entity; |
| 30 | +
|
| 31 | + use Symfony\Component\Validator\Constraints as Assert; |
| 32 | +
|
| 33 | + class Book |
| 34 | + { |
| 35 | + /** |
| 36 | + * @Assert\Json( |
| 37 | + * message = "You've entered an invalid Json." |
| 38 | + * ) |
| 39 | + */ |
| 40 | + private $chapters; |
| 41 | + } |
| 42 | +
|
| 43 | + .. code-block:: yaml |
| 44 | +
|
| 45 | + # config/validator/validation.yaml |
| 46 | + App\Entity\Book: |
| 47 | + properties: |
| 48 | + chapters: |
| 49 | + - Json: |
| 50 | + message: You've entered an invalid Json. |
| 51 | +
|
| 52 | + .. code-block:: xml |
| 53 | +
|
| 54 | + <!-- config/validator/validation.xml --> |
| 55 | + <?xml version="1.0" encoding="UTF-8" ?> |
| 56 | + <constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" |
| 57 | + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
| 58 | + xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> |
| 59 | +
|
| 60 | + <class name="App\Entity\Book"> |
| 61 | + <property name="chapters"> |
| 62 | + <constraint name="Json"> |
| 63 | + <option name"message">You've entered an invalid Json.</option> |
| 64 | + </constraint> |
| 65 | + </property> |
| 66 | + </class> |
| 67 | + </constraint-mapping> |
| 68 | +
|
| 69 | + .. code-block:: php |
| 70 | +
|
| 71 | + // src/Entity/Book.php |
| 72 | + namespace App\Entity; |
| 73 | +
|
| 74 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 75 | + use Symfony\Component\Validator\Constraints as Assert; |
| 76 | +
|
| 77 | + class Book |
| 78 | + { |
| 79 | + private $chapters; |
| 80 | +
|
| 81 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 82 | + { |
| 83 | + $metadata->addPropertyConstraint('chapters', new Assert\Json(array( |
| 84 | + 'message' => 'You\'ve entered an invalid Json.', |
| 85 | + ))); |
| 86 | + } |
| 87 | + } |
| 88 | +
|
| 89 | +Options |
| 90 | +------- |
| 91 | + |
| 92 | +message |
| 93 | +~~~~~~~ |
| 94 | + |
| 95 | +**type**: ``string`` **default**: ``This value should be valid JSON.`` |
| 96 | + |
| 97 | +This message is shown if the underlying data is not a valid JSON. |
| 98 | + |
| 99 | +.. include:: /reference/constraints/_payload-option.rst.inc |
0 commit comments