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