|
| 1 | +Isbn |
| 2 | +==== |
| 3 | + |
| 4 | +.. versionadded:: New in version 2.3: |
| 5 | + The Isbn validation were added in Symfony 2.3. |
| 6 | + |
| 7 | +This constraint permits that a ISBN (International Standard Book Numbers) |
| 8 | +number is either a valid ISBN-10, a valid ISBN-13 code or both on a value. |
| 9 | + |
| 10 | ++----------------+----------------------------------------------------------------------+ |
| 11 | +| Applies to | :ref:`property or method<validation-property-target>` | |
| 12 | ++----------------+----------------------------------------------------------------------+ |
| 13 | +| Options | - `isbn10Message`_ | |
| 14 | +| | - `isbn13Message`_ | |
| 15 | +| | - `bothIsbnMessage`_ | |
| 16 | +| | - `isbn10`_ | |
| 17 | +| | - `isbn13`_ | |
| 18 | ++----------------+----------------------------------------------------------------------+ |
| 19 | +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Isbn` | |
| 20 | ++----------------+----------------------------------------------------------------------+ |
| 21 | +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\IsbnValidator` | |
| 22 | ++----------------+----------------------------------------------------------------------+ |
| 23 | + |
| 24 | +Basic Usage |
| 25 | +----------- |
| 26 | + |
| 27 | +To use the ``Isbn`` validator, simply apply it to a property or method |
| 28 | +on an object that will contain a ISBN number. |
| 29 | + |
| 30 | +.. configuration-block:: |
| 31 | + |
| 32 | + .. code-block:: yaml |
| 33 | +
|
| 34 | + # src/Acme/BookcaseBunlde/Resources/config/validation.yml |
| 35 | + Acme\BookcaseBunlde\Entity\Book: |
| 36 | + properties: |
| 37 | + isbn: |
| 38 | + - Isbn: |
| 39 | + isbn10: true |
| 40 | + isbn13: true |
| 41 | + bothIsbnMessage: This value is neither a valid ISBN-10 nor a valid ISBN-13. |
| 42 | +
|
| 43 | + .. code-block:: xml |
| 44 | +
|
| 45 | + <!-- src/Acme/BookcaseBunlde/Resources/config/validation.xml --> |
| 46 | + <class name="Acme\BookcaseBunlde\Entity\Book"> |
| 47 | + <property name="isbn"> |
| 48 | + <constraint name="Isbn"> |
| 49 | + <option name="isbn10">true</option> |
| 50 | + <option name="isbn13">true</option> |
| 51 | + <option name="bothIsbnMessage">This value is neither a valid ISBN-10 nor a valid ISBN-13.</option> |
| 52 | + </constraint> |
| 53 | + </property> |
| 54 | + </class> |
| 55 | +
|
| 56 | + .. code-block:: php-annotations |
| 57 | +
|
| 58 | + // src/Acme/BookcaseBunlde/Entity/Book.php |
| 59 | + use Symfony\Component\Validator\Constraints as Assert; |
| 60 | +
|
| 61 | + class Book |
| 62 | + { |
| 63 | + /** |
| 64 | + * @Assert\Isbn( |
| 65 | + * isbn10 = true, |
| 66 | + * isbn13 = true, |
| 67 | + * bothIsbnMessage = "This value is neither a valid ISBN-10 nor a valid ISBN-13." |
| 68 | + * ) |
| 69 | + */ |
| 70 | + protected $isbn; |
| 71 | + } |
| 72 | +
|
| 73 | + .. code-block:: php |
| 74 | +
|
| 75 | + // src/Acme/BookcaseBunlde/Entity/Book.php |
| 76 | + namespace Acme\BookcaseBunlde\Entity; |
| 77 | +
|
| 78 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 79 | + use Symfony\Component\Validator\Constraints as Assert; |
| 80 | +
|
| 81 | + class Book |
| 82 | + { |
| 83 | + protected $isbn; |
| 84 | +
|
| 85 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 86 | + { |
| 87 | + $metadata->addPropertyConstraint('isbn', new Assert\Isbn(array( |
| 88 | + 'isbn10' => true, |
| 89 | + 'isbn13' => true, |
| 90 | + 'bothIsbnMessage' => 'This value is neither a valid ISBN-10 nor a valid ISBN-13.' |
| 91 | + ))); |
| 92 | + } |
| 93 | + } |
| 94 | +
|
| 95 | +Available Options |
| 96 | +----------------- |
| 97 | + |
| 98 | +isbn10Message |
| 99 | +~~~~~~~~~~~~~ |
| 100 | + |
| 101 | +**type**: ``string`` **default**: ``This value is not a valid ISBN-10.`` |
| 102 | + |
| 103 | +The message that will be shown if the option isbn10 is true |
| 104 | +and the given value does not pass the ISBN-10 check. |
| 105 | + |
| 106 | +isbn13Message |
| 107 | +~~~~~~~~~~~~~ |
| 108 | + |
| 109 | +**type**: ``string`` **default**: ``This value is not a valid ISBN-13.`` |
| 110 | + |
| 111 | +The message that will be shown if the option isbn13 is true |
| 112 | +and the given value does not pass the ISBN-13 check. |
| 113 | + |
| 114 | +bothIsbnMessage |
| 115 | +~~~~~~~~~~~~~~~ |
| 116 | + |
| 117 | +**type**: ``string`` **default**: ``This value is neither a valid ISBN-10 nor a valid ISBN-13.`` |
| 118 | + |
| 119 | +The message that will be shown if the options (isbn10, isbn13) is true |
| 120 | +and the given value does not pass the ISBN-13 nor ISBN-13 check. |
| 121 | + |
| 122 | +isbn10 |
| 123 | +~~~~~~ |
| 124 | + |
| 125 | +**type**: ``boolean`` [:ref:`default option<validation-default-option>`] |
| 126 | + |
| 127 | +If this required option is set to ``true`` the constraint will check |
| 128 | +if the code is a valid ISBN-10 code. |
| 129 | + |
| 130 | +isbn13 |
| 131 | +~~~~~~ |
| 132 | + |
| 133 | +**type**: ``boolean`` [:ref:`default option<validation-default-option>`] |
| 134 | + |
| 135 | +If this required option is set to ``true`` the constraint will check |
| 136 | +if the code is a valid ISBN-13 code. |
0 commit comments