Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Documented currency field type and validator #2704

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 30, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions reference/constraints.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ Validation Constraints Reference
constraints/Image

constraints/CardScheme
constraints/Currency
constraints/Luhn
constraints/Iban
constraints/Isbn
Expand Down
85 changes: 85 additions & 0 deletions reference/constraints/Currency.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
Currency
========

.. versionadded:: 2.3
This constraint is new in version 2.3.

Validates that a value is a valid `3-letter ISO 4217`_ currency name.

+----------------+---------------------------------------------------------------------------+
| Applies to | :ref:`property or method<validation-property-target>` |
+----------------+---------------------------------------------------------------------------+
| Options | - `message`_ |
+----------------+---------------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Currency` |
+----------------+---------------------------------------------------------------------------+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\CurrencyValidator` |
+----------------+---------------------------------------------------------------------------+

Basic Usage
-----------

If you want to ensure that the ``currency`` property of an ``Order`` is a valid
currency, you could do the following:

.. configuration-block::

.. code-block:: yaml

# src/EcommerceBundle/Resources/config/validation.yml
Acme\EcommerceBundle\Entity\Order:
properties:
currency:
- Currency: ~

.. code-block:: php-annotations

// src/Acme/EcommerceBundle/Entity/Order.php
namespace Acme\EcommerceBundle\Entity;

use Symfony\Component\Validator\Constraints as Assert;

class Order
{
/**
* @Assert\Currency
*/
protected $currency;
}

.. code-block:: xml

<!-- src/Acme/EcommerceBundle/Resources/config/validation.xml -->
<class name="Acme\EcommerceBundle\Entity\Order">
<property name="currency">
<constraint name="Currency" />
</property>
</class>

.. code-block:: php

// src/Acme/EcommerceBundle/Entity/Order.php
namespace Acme\SocialBundle\Entity;

use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Constraints as Assert;

class Order
{
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('currency', new Assert\Currency());
}
}

Options
-------

message
~~~~~~~

**type**: ``string`` **default**: ``This value is not a valid currency.``

This is the message that will be shown if the value is not a valid currency.

.. _`3-letter ISO 4217`: http://en.wikipedia.org/wiki/ISO_4217
1 change: 1 addition & 0 deletions reference/constraints/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ Financial and other Number Constraints
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

* :doc:`CardScheme </reference/constraints/CardScheme>`
* :doc:`Currency </reference/constraints/Currency>`
* :doc:`Luhn </reference/constraints/Luhn>`
* :doc:`Iban </reference/constraints/Iban>`
* :doc:`Isbn </reference/constraints/Isbn>`
Expand Down
1 change: 1 addition & 0 deletions reference/forms/types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Form Types Reference
types/choice
types/collection
types/country
types/currency
types/date
types/datetime
types/email
Expand Down
75 changes: 75 additions & 0 deletions reference/forms/types/currency.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
.. index::
single: Forms; Fields; currency

currency Field Type
===================

The ``currency`` type is a subset of the
:doc:`choice type </reference/forms/types/choice` that allows the user to
select from a large list of `3-letter ISO 4217`_ currencies.

Unlike the ``choice`` type, you don't need to specify a ``choices`` or
``choice_list`` option as the field type automatically uses a large list of
currencies. You *can* specify either of these options manually, but then you
should just use the ``choice`` type directly.

+-------------+------------------------------------------------------------------------+
| Rendered as | can be various tags (see :ref:`forms-reference-choice-tags`) |
+-------------+------------------------------------------------------------------------+
| Overridden | - `choices`_ |
| Options | |
+-------------+------------------------------------------------------------------------+
| Inherited | - `multiple`_ |
| options | - `expanded`_ |
| | - `preferred_choices`_ |
| | - `empty_value`_ |
| | - `error_bubbling`_ |
| | - `required`_ |
| | - `label`_ |
| | - `read_only`_ |
| | - `disabled`_ |
| | - `mapped`_ |
+-------------+------------------------------------------------------------------------+
| Parent type | :doc:`choice </reference/forms/types/choice>` |
+-------------+------------------------------------------------------------------------+
| Class | :class:`Symfony\\Component\\Form\\Extension\\Core\\Type\\CurrencyType` |
+-------------+------------------------------------------------------------------------+

Overridden Options
------------------

choices
~~~~~~~

**default**: ``Symfony\Component\Intl\Intl::getCurrencyBundle()->getCurrencyNames()``

The choices option defaults to all currencies.

Inherited options
-----------------

These options inherit from the :doc:`choice</reference/forms/types/choice>` type:

.. include:: /reference/forms/types/options/multiple.rst.inc

.. include:: /reference/forms/types/options/expanded.rst.inc

.. include:: /reference/forms/types/options/preferred_choices.rst.inc

.. include:: /reference/forms/types/options/empty_value.rst.inc

.. include:: /reference/forms/types/options/error_bubbling.rst.inc

These options inherit from the :doc:`date</reference/forms/types/form>` type:

.. include:: /reference/forms/types/options/required.rst.inc

.. include:: /reference/forms/types/options/label.rst.inc

.. include:: /reference/forms/types/options/read_only.rst.inc

.. include:: /reference/forms/types/options/disabled.rst.inc

.. include:: /reference/forms/types/options/mapped.rst.inc

.. _`3-letter ISO 4217`: http://en.wikipedia.org/wiki/ISO_4217
1 change: 1 addition & 0 deletions reference/forms/types/map.rst.inc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Choice Fields
* :doc:`language</reference/forms/types/language>`
* :doc:`locale</reference/forms/types/locale>`
* :doc:`timezone</reference/forms/types/timezone>`
* :doc:`currency</reference/forms/types/currency>`

Date and Time Fields
~~~~~~~~~~~~~~~~~~~~
Expand Down