|
| 1 | +CardScheme |
| 2 | +========== |
| 3 | + |
| 4 | +.. versionadded:: 2.2 |
| 5 | + The CardScheme validation is new in Symfony 2.2. |
| 6 | + |
| 7 | +This constraint ensures that a credit card number is valid for a given credit card |
| 8 | +company. It can be used to validate the number before trying to initiate a payment |
| 9 | +through a payment gateway. |
| 10 | + |
| 11 | ++----------------+--------------------------------------------------------------------------+ |
| 12 | +| Applies to | :ref:`property or method<validation-property-target>` | |
| 13 | ++----------------+--------------------------------------------------------------------------+ |
| 14 | +| Options | - `schemes`_ | |
| 15 | +| | - `message`_ | |
| 16 | ++----------------+--------------------------------------------------------------------------+ |
| 17 | +| Class | :class:`Symfony\\Component\\Validator\\Constraints\\CardScheme` | |
| 18 | ++----------------+--------------------------------------------------------------------------+ |
| 19 | +| Validator | :class:`Symfony\\Component\\Validator\\Constraints\\CardSchemeValidator` | |
| 20 | ++----------------+--------------------------------------------------------------------------+ |
| 21 | + |
| 22 | +Basic Usage |
| 23 | +----------- |
| 24 | + |
| 25 | +To use the CardScheme validator, simply apply it to a property or method on an |
| 26 | +object that will contain a credit card number. |
| 27 | + |
| 28 | +.. configuration-block:: |
| 29 | + |
| 30 | + .. code-block:: yaml |
| 31 | +
|
| 32 | + # src/Acme/SubscriptionBundle/Resources/config/validation.yml |
| 33 | + Acme\SubscriptionBundle\Entity\Transaction: |
| 34 | + properties: |
| 35 | + cardNumber: |
| 36 | + - CardScheme: |
| 37 | + schemes: [VISA] |
| 38 | + message: You credit card number is invalid. |
| 39 | +
|
| 40 | + .. code-block:: xml |
| 41 | +
|
| 42 | + <!-- src/Acme/SubscriptionBundle/Resources/config/validation.xml --> |
| 43 | + <class name="Acme\SubscriptionBundle\Entity\Transaction"> |
| 44 | + <property name="cardNumber"> |
| 45 | + <constraint name="CardScheme"> |
| 46 | + <option name="schemes"> |
| 47 | + <value>VISA</value> |
| 48 | + </option> |
| 49 | + <option name="message">You credit card number is invalid.</option> |
| 50 | + </constraint> |
| 51 | + </property> |
| 52 | + </class> |
| 53 | +
|
| 54 | + .. code-block:: php-annotations |
| 55 | +
|
| 56 | + // src/Acme/SubscriptionBundle/Entity/Transaction.php |
| 57 | + use Symfony\Component\Validator\Constraints as Assert; |
| 58 | +
|
| 59 | + class Transaction |
| 60 | + { |
| 61 | + /** |
| 62 | + * @Assert\CardScheme(schemes = {"VISA"}, message = "You credit card number is invalid.") |
| 63 | + */ |
| 64 | + protected $cardNumber; |
| 65 | + } |
| 66 | +
|
| 67 | + .. code-block:: php |
| 68 | +
|
| 69 | + // src/Acme/SubscriptionBundle/Entity/Transaction.php |
| 70 | + use Symfony\Component\Validator\Mapping\ClassMetadata; |
| 71 | + use Symfony\Component\Validator\Constraints\CardScheme; |
| 72 | +
|
| 73 | + class Transaction |
| 74 | + { |
| 75 | + protected $cardNumber; |
| 76 | +
|
| 77 | + public static function loadValidatorMetadata(ClassMetadata $metadata) |
| 78 | + { |
| 79 | + $metadata->addPropertyConstraint('cardSchema', new CardScheme(array( |
| 80 | + 'schemes' => array( |
| 81 | + 'VISA' |
| 82 | + ), |
| 83 | + 'message' => 'You credit card number is invalid.', |
| 84 | + ))); |
| 85 | + } |
| 86 | + } |
| 87 | +
|
| 88 | +Available Options |
| 89 | +----------------- |
| 90 | + |
| 91 | +schemes |
| 92 | +------- |
| 93 | + |
| 94 | +**type**: ``array`` |
| 95 | + |
| 96 | +This option is required and represents the name of the number scheme used to |
| 97 | +validate the credit card number. Valid values are: |
| 98 | + |
| 99 | +* ``AMEX`` |
| 100 | +* ``CHINA_UNIONPAY`` |
| 101 | +* ``DINERS`` |
| 102 | +* ``DISCOVER`` |
| 103 | +* ``INSTAPAYMENT`` |
| 104 | +* ``JCB`` |
| 105 | +* ``LASER`` |
| 106 | +* ``MAESTRO`` |
| 107 | +* ``MASTERCARD`` |
| 108 | +* ``VISA`` |
| 109 | + |
| 110 | +For more information about the used schemes, see `Wikipedia`_. |
| 111 | + |
| 112 | +message |
| 113 | +~~~~~~~ |
| 114 | + |
| 115 | +**type**: ``string`` **default**: ``Unsupported card type or invalid card number`` |
| 116 | + |
| 117 | +The default message supplied when the value does not pass the CardScheme check. |
| 118 | + |
| 119 | +.. _`Wikipedia`: http://en.wikipedia.org/wiki/Bank_card_number#Issuer_identification_number_.28IIN.29 |
0 commit comments