|
| 1 | +Currency |
| 2 | +======== |
| 3 | + |
| 4 | +..versionadded::2.3 |
| 5 | + This constraint is new in version 2.3. |
| 6 | + |
| 7 | +Validates that a value is a valid `3-letter ISO 4217`_ currency name. |
| 8 | + |
| 9 | ++----------------+---------------------------------------------------------------------------+ |
| 10 | +| Applies to|:ref:`property or method<validation-property-target>`| |
| 11 | ++----------------+---------------------------------------------------------------------------+ |
| 12 | +| Options| - `message`_| |
| 13 | ++----------------+---------------------------------------------------------------------------+ |
| 14 | +| Class|:class:`Symfony\\Component\\Validator\\Constraints\\Currency`| |
| 15 | ++----------------+---------------------------------------------------------------------------+ |
| 16 | +| Validator|:class:`Symfony\\Component\\Validator\\Constraints\\CurrencyValidator`| |
| 17 | ++----------------+---------------------------------------------------------------------------+ |
| 18 | + |
| 19 | +Basic Usage |
| 20 | +----------- |
| 21 | + |
| 22 | +If you want to ensure that the ``currency`` property of an ``Order`` is a valid |
| 23 | +currency, you could do the following: |
| 24 | + |
| 25 | +..configuration-block:: |
| 26 | + |
| 27 | + ..code-block::yaml |
| 28 | +
|
| 29 | +# src/EcommerceBundle/Resources/config/validation.yml |
| 30 | +Acme\EcommerceBundle\Entity\Order: |
| 31 | +properties: |
| 32 | +currency: |
| 33 | + -Currency:~ |
| 34 | +
|
| 35 | + ..code-block::php-annotations |
| 36 | +
|
| 37 | + // src/Acme/EcommerceBundle/Entity/Order.php |
| 38 | + namespace Acme\EcommerceBundle\Entity; |
| 39 | +
|
| 40 | + use Symfony\Component\Validator\Constraints as Assert; |
| 41 | +
|
| 42 | + class Order |
| 43 | + { |
| 44 | + /** |
| 45 | + * @Assert\Currency |
| 46 | + */ |
| 47 | + protected $currency; |
| 48 | + } |
| 49 | +
|
| 50 | + ..code-block::xml |
| 51 | +
|
| 52 | +<!-- src/Acme/EcommerceBundle/Resources/config/validation.xml--> |
| 53 | + <classname="Acme\EcommerceBundle\Entity\Order"> |
| 54 | + <propertyname="currency"> |
| 55 | + <constraintname="Currency" /> |
| 56 | + </property> |
| 57 | + </class> |
| 58 | +
|
| 59 | + ..code-block::php |
| 60 | +
|
| 61 | + // src/Acme/EcommerceBundle/Entity/Order.php |
| 62 | + namespace Acme\SocialBundle\Entity; |
| 63 | +
|
| 64 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 65 | + use Symfony\Component\Validator\Constraints as Assert; |
| 66 | +
|
| 67 | + class Order |
| 68 | + { |
| 69 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 70 | + { |
| 71 | + $metadata->addPropertyConstraint('currency', new Assert\Currency()); |
| 72 | + } |
| 73 | + } |
| 74 | +
|
| 75 | +Options |
| 76 | +------- |
| 77 | + |
| 78 | +message |
| 79 | +~~~~~~~ |
| 80 | + |
| 81 | +**type**: ``string`` **default**: ``This value is not a valid currency.`` |
| 82 | + |
| 83 | +This is the message that will be shown if the value is not a valid currency. |
| 84 | + |
| 85 | +.. _`3-letter ISO 4217`:http://en.wikipedia.org/wiki/ISO_4217 |