|
| 1 | +Luhn |
| 2 | +====== |
| 3 | + |
| 4 | +This constraint is used to ensure that a credit card number passes the `Luhn algorithm`_. |
| 5 | +It is useful as a first step to validating a credit card: 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 |
| 22 | +will contain a credit card number. |
| 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 credit card 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 credit card 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 credit card 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 credit card 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. |
| 86 | + |
| 87 | +.. _`Luhn algorithm`: http://en.wikipedia.org/wiki/Luhn_algorithm |
0 commit comments