diff --git a/reference/constraints.rst b/reference/constraints.rst index b64c8fbdbaa..d81a4eefd37 100644 --- a/reference/constraints.rst +++ b/reference/constraints.rst @@ -19,6 +19,7 @@ Validation Constraints Reference constraints/Regex constraints/Ip constraints/Uuid + constraints/Json constraints/EqualTo constraints/NotEqualTo diff --git a/reference/constraints/Json.rst b/reference/constraints/Json.rst new file mode 100644 index 00000000000..4b722ca1064 --- /dev/null +++ b/reference/constraints/Json.rst @@ -0,0 +1,99 @@ +Json + +Validates that a value is valid ``json``. Specifically, this checks to see if +the value is a valid ``json`` or not. + ++----------------+-----------------------------------------------------------------------+ +| Applies to | :ref:`property or method ` | ++----------------+-----------------------------------------------------------------------+ +| Options | - `message`_ | +| | - `payload`_ | ++----------------+-----------------------------------------------------------------------+ +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Json` | ++----------------+-----------------------------------------------------------------------+ +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\JsonValidator` | ++----------------+-----------------------------------------------------------------------+ + +Basic Usage +----------- + +The ``Json`` constraint can be applied to a property or a "getter" method, +but is most commonly useful in the latter case. For example, suppose that +you want to guarantee that some ``jsonString`` property is valid JSON. + +.. configuration-block:: + + .. code-block:: php-annotations + + // src/Entity/Book.php + namespace App\Entity; + + use Symfony\Component\Validator\Constraints as Assert; + + class Book + { + /** + * @Assert\Json( + * message = "You've entered an invalid Json." + * ) + */ + private $chapters; + } + + .. code-block:: yaml + + # config/validator/validation.yaml + App\Entity\Book: + properties: + chapters: + - Json: + message: You've entered an invalid Json. + + .. code-block:: xml + + + + + + + + + + + + + + + .. code-block:: php + + // src/Entity/Book.php + namespace App\Entity; + + use Symfony\Component\Validator\Mapping\ClassMetadata; + use Symfony\Component\Validator\Constraints as Assert; + + class Book + { + private $chapters; + + public static function loadValidatorMetadata(ClassMetadata $metadata) + { + $metadata->addPropertyConstraint('chapters', new Assert\Json(array( + 'message' => 'You\'ve entered an invalid Json.', + ))); + } + } + +Options +------- + +message +~~~~~~~ + +**type**: ``string`` **default**: ``This value should be valid JSON.`` + +This message is shown if the underlying data is not a valid JSON. + +.. include:: /reference/constraints/_payload-option.rst.inc diff --git a/reference/constraints/map.rst.inc b/reference/constraints/map.rst.inc index 03d573559ee..655ef1723b3 100644 --- a/reference/constraints/map.rst.inc +++ b/reference/constraints/map.rst.inc @@ -21,6 +21,7 @@ String Constraints * :doc:`Regex ` * :doc:`Ip ` * :doc:`Uuid` +* :doc:`Json` Comparison Constraints ~~~~~~~~~~~~~~~~~~~~~~