Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Validator] Add new json Validator Documentation
- Loading branch information
1 parent
e08cac0
commit 01ea746
Showing
3 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <validation-property-target>` | | ||
+----------------+-----------------------------------------------------------------------+ | ||
| 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 | ||
<!-- config/validator/validation.xml --> | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd"> | ||
<class name="App\Entity\Book"> | ||
<property name="chapters"> | ||
<constraint name="Json"> | ||
<option name"message">You've entered an invalid Json.</option> | ||
</constraint> | ||
</property> | ||
</class> | ||
</constraint-mapping> | ||
.. 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters