Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit48b3a7a

Browse files
committed
Merge pull requestsymfony#2704 from WouterJ/issue_2105
Documented currency field type and validator
2 parentsa48c6ab +3a8bd43 commit48b3a7a

File tree

6 files changed

+164
-0
lines changed

6 files changed

+164
-0
lines changed

‎reference/constraints.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ Validation Constraints Reference
4646
constraints/Image
4747

4848
constraints/CardScheme
49+
constraints/Currency
4950
constraints/Luhn
5051
constraints/Iban
5152
constraints/Isbn

‎reference/constraints/Currency.rst

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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

‎reference/constraints/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ Financial and other Number Constraints
6666
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6767

6868
* :doc:`CardScheme</reference/constraints/CardScheme>`
69+
* :doc:`Currency</reference/constraints/Currency>`
6970
* :doc:`Luhn</reference/constraints/Luhn>`
7071
* :doc:`Iban</reference/constraints/Iban>`
7172
* :doc:`Isbn</reference/constraints/Isbn>`

‎reference/forms/types.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ Form Types Reference
1414
types/choice
1515
types/collection
1616
types/country
17+
types/currency
1718
types/date
1819
types/datetime
1920
types/email

‎reference/forms/types/currency.rst

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
..index::
2+
single: Forms; Fields; currency
3+
4+
currency Field Type
5+
===================
6+
7+
The ``currency`` type is a subset of the
8+
:doc:`choice type </reference/forms/types/choice` that allows the user to
9+
select from a large list of `3-letter ISO 4217`_ currencies.
10+
11+
Unlike the ``choice`` type, you don't need to specify a ``choices`` or
12+
``choice_list`` option as the field type automatically uses a large list of
13+
currencies. You *can* specify either of these options manually, but then you
14+
should just use the ``choice`` type directly.
15+
16+
+-------------+------------------------------------------------------------------------+
17+
| Rendered as| can be various tags (see:ref:`forms-reference-choice-tags`)|
18+
+-------------+------------------------------------------------------------------------+
19+
| Overridden| - `choices`_|
20+
| Options||
21+
+-------------+------------------------------------------------------------------------+
22+
| Inherited| - `multiple`_|
23+
| options| - `expanded`_|
24+
|| - `preferred_choices`_|
25+
|| - `empty_value`_|
26+
|| - `error_bubbling`_|
27+
|| - `required`_|
28+
|| - `label`_|
29+
|| - `read_only`_|
30+
|| - `disabled`_|
31+
|| - `mapped`_|
32+
+-------------+------------------------------------------------------------------------+
33+
| Parent type|:doc:`choice</reference/forms/types/choice>`|
34+
+-------------+------------------------------------------------------------------------+
35+
| Class|:class:`Symfony\\Component\\Form\\Extension\\Core\\Type\\CurrencyType`|
36+
+-------------+------------------------------------------------------------------------+
37+
38+
Overridden Options
39+
------------------
40+
41+
choices
42+
~~~~~~~
43+
44+
**default**: ``Symfony\Component\Intl\Intl::getCurrencyBundle()->getCurrencyNames()``
45+
46+
The choices option defaults to all currencies.
47+
48+
Inherited options
49+
-----------------
50+
51+
These options inherit from the:doc:`choice</reference/forms/types/choice>` type:
52+
53+
..include::/reference/forms/types/options/multiple.rst.inc
54+
55+
..include::/reference/forms/types/options/expanded.rst.inc
56+
57+
..include::/reference/forms/types/options/preferred_choices.rst.inc
58+
59+
..include::/reference/forms/types/options/empty_value.rst.inc
60+
61+
..include::/reference/forms/types/options/error_bubbling.rst.inc
62+
63+
These options inherit from the:doc:`date</reference/forms/types/form>` type:
64+
65+
..include::/reference/forms/types/options/required.rst.inc
66+
67+
..include::/reference/forms/types/options/label.rst.inc
68+
69+
..include::/reference/forms/types/options/read_only.rst.inc
70+
71+
..include::/reference/forms/types/options/disabled.rst.inc
72+
73+
..include::/reference/forms/types/options/mapped.rst.inc
74+
75+
.. _`3-letter ISO 4217`:http://en.wikipedia.org/wiki/ISO_4217

‎reference/forms/types/map.rst.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Choice Fields
2121
* :doc:`language</reference/forms/types/language>`
2222
* :doc:`locale</reference/forms/types/locale>`
2323
* :doc:`timezone</reference/forms/types/timezone>`
24+
* :doc:`currency</reference/forms/types/currency>`
2425

2526
Date and Time Fields
2627
~~~~~~~~~~~~~~~~~~~~

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp