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

Appearance settings

Commit 0dcfe3c

Browse filesBrowse files
committed
Merge pull request symfony#1598 from merk/luhn-validator
[2.2] [Validator] Luhn Validator
2 parents d06484c + 4ffc9ab commit 0dcfe3c
Copy full SHA for 0dcfe3c

File tree

Expand file treeCollapse file tree

3 files changed

+93
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+93
-1
lines changed

‎reference/constraints.rst

Copy file name to clipboardExpand all lines: reference/constraints.rst
+3-1Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,16 @@ Validation Constraints Reference
4040
constraints/File
4141
constraints/Image
4242

43+
constraints/Luhn
44+
4345
constraints/Callback
4446
constraints/All
4547
constraints/UserPassword
4648
constraints/Valid
4749

4850
The Validator is designed to validate objects against *constraints*.
4951
In real life, a constraint could be: "The cake must not be burned". In
50-
Symfony2, constraints are similar: They are assertions that a condition is
52+
Symfony2, constraints are similar: They are assertions that a condition is
5153
true.
5254

5355
Supported Constraints

‎reference/constraints/Luhn.rst

Copy file name to clipboard
+85Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
Luhn
2+
======
3+
4+
This constraint is used to ensure that a creditcard number passes the Luhn algorithm.
5+
It is useful as a first step to validating a creditcard, before communicating with a
6+
payment gateway.
7+
8+
+----------------+-----------------------------------------------------------------------+
9+
| Applies to | :ref:`property or method<validation-property-target>` |
10+
+----------------+-----------------------------------------------------------------------+
11+
| Options | - `message`_ |
12+
+----------------+-----------------------------------------------------------------------+
13+
| Class | :class:`Symfony\\Component\\Validator\\Constraints\\Luhn` |
14+
+----------------+-----------------------------------------------------------------------+
15+
| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\LuhnValidator` |
16+
+----------------+-----------------------------------------------------------------------+
17+
18+
Basic Usage
19+
-----------
20+
21+
To use the Luhn validator, simply apply it to a property on an object that will contain
22+
the creditcard number submission.
23+
24+
.. configuration-block::
25+
26+
.. code-block:: yaml
27+
28+
# src/Acme/SubscriptionBundle/Resources/config/validation.yml
29+
Acme\SubscriptionBundle\Entity\Transaction:
30+
properties:
31+
cardNumber:
32+
- Luhn:
33+
message: Please check your creditcard number.
34+
35+
.. code-block:: xml
36+
37+
<!-- src/Acme/SubscriptionBundle/Resources/config/validation.xml -->
38+
<class name="Acme\SubscriptionBundle\Entity\Transaction">
39+
<property name="cardNumber">
40+
<constraint name="Luhn">
41+
<option name="message">Please check your creditcard number.</option>
42+
</constraint>
43+
</property>
44+
</class>
45+
46+
.. code-block:: php-annotations
47+
48+
// src/Acme/SubscriptionBundle/Entity/Transaction.php
49+
use Symfony\Component\Validator\Constraints as Assert;
50+
51+
class Transaction
52+
{
53+
/**
54+
* @Assert\Luhn(message = "Please check your creditcard number.")
55+
*/
56+
protected $cardNumber;
57+
}
58+
59+
.. code-block:: php
60+
61+
// src/Acme/SubscriptionBundle/Entity/Transaction.php
62+
use Symfony\Component\Validator\Mapping\ClassMetadata;
63+
use Symfony\Component\Validator\Constraints\Luhn;
64+
65+
class Transaction
66+
{
67+
protected $cardNumber;
68+
69+
public static function loadValidatorMetadata(ClassMetadata $metadata)
70+
{
71+
$metadata->addPropertyConstraint('luhn', new Luhn(array(
72+
'message' => 'Please check your creditcard number',
73+
)));
74+
}
75+
}
76+
77+
Available Options
78+
-----------------
79+
80+
message
81+
~~~~~~~
82+
83+
**type**: ``string`` **default**: ``Invalid card number``
84+
85+
The default message supplied when the value does not pass the Luhn check.

‎reference/constraints/map.rst.inc

Copy file name to clipboardExpand all lines: reference/constraints/map.rst.inc
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@ File Constraints
5454
* :doc:`File </reference/constraints/File>`
5555
* :doc:`Image </reference/constraints/Image>`
5656

57+
Financial Constraints
58+
~~~~~~~~~~~~~~~~~~~~~
59+
60+
* :doc:`Luhn </reference/constraints/Luhn>`
61+
5762
Other Constraints
5863
~~~~~~~~~~~~~~~~~
5964

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.