发布于 2015-08-27 16:51:21 | 106 次阅读 | 评论: 0 | 来源: 网络整理
This constraint ensures that a credit card number is valid for a given credit card company. It can be used to validate the number before trying to initiate a payment through a payment gateway.
Applies to | property or method |
Options | |
Class | CardScheme |
Validator | CardSchemeValidator |
To use the CardScheme
validator, simply apply it to a property or method
on an object that will contain a credit card number.
# src/Acme/SubscriptionBundle/Resources/config/validation.yml
AcmeSubscriptionBundleEntityTransaction:
properties:
cardNumber:
- CardScheme:
schemes: [VISA]
message: Your credit card number is invalid.
<!-- src/Acme/SubscriptionBundle/Resources/config/validation.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<constraint-mapping xmlns="http://symfony.com/schema/dic/constraint-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/constraint-mapping/constraint-mapping-1.0.xsd">
<class name="AcmeSubscriptionBundleEntityTransaction">
<property name="cardNumber">
<constraint name="CardScheme">
<option name="schemes">
<value>VISA</value>
</option>
<option name="message">Your credit card number is invalid.</option>
</constraint>
</property>
</class>
</constraint-mapping>
// src/Acme/SubscriptionBundle/Entity/Transaction.php
namespace AcmeSubscriptionBundleEntityTransaction;
use SymfonyComponentValidatorConstraints as Assert;
class Transaction
{
/**
* @AssertCardScheme(schemes = {"VISA"}, message = "Your credit card number is invalid.")
*/
protected $cardNumber;
}
// src/Acme/SubscriptionBundle/Entity/Transaction.php
namespace AcmeSubscriptionBundleEntityTransaction;
use SymfonyComponentValidatorMappingClassMetadata;
use SymfonyComponentValidatorConstraints as Assert;
class Transaction
{
protected $cardNumber;
public static function loadValidatorMetadata(ClassMetadata $metadata)
{
$metadata->addPropertyConstraint('cardNumber', new AssertCardScheme(array(
'schemes' => array(
'VISA'
),
'message' => 'Your credit card number is invalid.',
)));
}
}
type: mixed
[default option]
This option is required and represents the name of the number scheme used to validate the credit card number, it can either be a string or an array. Valid values are:
AMEX
CHINA_UNIONPAY
DINERS
DISCOVER
INSTAPAYMENT
JCB
LASER
MAESTRO
MASTERCARD
VISA
For more information about the used schemes, see Wikipedia: Issuer identification number (IIN).
type: string
default: Unsupported card type or invalid card number.
The message shown when the value does not pass the CardScheme
check.
type: mixed
default: null
2.6 新版功能: The payload
option was introduced in Symfony 2.6.
This option can be used to attach arbitrary domain-specific data to a constraint. The configured payload is not used by the Validator component, but its processing is completely up to.
For example, you may want to used several error levels to present failed constraint differently in the front-end depending on the severity of the error.