Class kartik\validators\CardValidator

Inheritancekartik\validators\CardValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject
Implementsyii\base\Configurable
Uses Traitskartik\base\TranslationTrait, kartik\validators\ValidatorTrait

CardValidator validates standard debit and credit card number inputs using Luhn's checksum validation. It also additionally validates the card holder name, expiry date and CVV entered.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$allowedCards array List of allowed cards. kartik\validators\CardValidator
$attributeNames array Attribute names. yii\validators\Validator
$attributes array|string Attributes to be validated by this validator. yii\validators\Validator
$autoDetectType boolean Whether to auto detect card type based on pattern detection in $cards configuration and auto update the $typeValue. kartik\validators\CardValidator
$autoUpdateNumber boolean Whether to auto update the card number as pure numeric digits and overwrite the model attribute kartik\validators\CardValidator
$autoUpdateType boolean Whether to auto update and store the auto detected type within the $typeAttribute. kartik\validators\CardValidator
$behaviors yii\base\Behavior[] List of behaviors attached to this component. yii\base\Component
$builtInValidators array List of built-in validators (name => class or configuration) yii\validators\Validator
$cards array Holds the configurations for each of the credit / debit cards including the card lengths and regex patterns to check for valid card number prefixes kartik\validators\CardValidator
$cvvAttribute integer The card CVV attribute kartik\validators\CardValidator
$cvvValue integer Card cvv to check (overrides value set in $cvvAttribute) kartik\validators\CardValidator
$enableClientValidation boolean Whether to enable client-side validation for this validator. yii\validators\Validator
$except array|string Scenarios that the validator should not be applied to. yii\validators\Validator
$expiryMonthAttribute integer The card expiry month attribute kartik\validators\CardValidator
$expiryMonthValue integer Card expiry month to check (overrides value set in $expiryMonthAttribute) kartik\validators\CardValidator
$expiryYearAttribute integer The card expiry year attribute kartik\validators\CardValidator
$expiryYearValue integer Card expiry year to check (overrides value set in $expiryYearAttribute) kartik\validators\CardValidator
$holderAttribute string The card holder attribute kartik\validators\CardValidator
$holderPattern string The regular expression pattern to match for card holder name kartik\validators\CardValidator
$holderValue string Card holder name to check (overrides value set in $holderAttribute) kartik\validators\CardValidator
$i18n array The the internalization configuration for this widget. kartik\base\TranslationTrait
$invalidCVVMessage string The message shown if the detected card CVV is invalid kartik\validators\CardValidator
$invalidExpiryMessage string The message shown if the detected card expiry year or month is invalid kartik\validators\CardValidator
$invalidHolderMessage string The message shown if the detected card holder is invalid kartik\validators\CardValidator
$invalidTypeMessage string The message shown if the detected card type is invalid kartik\validators\CardValidator
$isEmpty callable|null A PHP callable that replaces the default implementation of isEmpty(). yii\validators\Validator
$message string|null The user-defined error message. yii\validators\Validator
$on array|string Scenarios that the validator can be applied to. yii\validators\Validator
$skipOnEmpty boolean Whether this validation rule should be skipped if the attribute value is null or an empty string. yii\validators\Validator
$skipOnError boolean Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. yii\validators\Validator
$typeAttribute string The card type attribute kartik\validators\CardValidator
$typeValue string Card type to check (overrides value set in $typeAttribute) kartik\validators\CardValidator
$validateCVV boolean Whether to validate the card CVV kartik\validators\CardValidator
$validateExpiry boolean Whether to validate the card expiry date (year / month) kartik\validators\CardValidator
$validateHolder boolean Whether to validate the card holder name kartik\validators\CardValidator
$validationAttributes array List of attribute names. yii\validators\Validator
$when callable|null A PHP callable whose return value determines whether this validator should be applied. yii\validators\Validator
$whenClient string|null A JavaScript function name whose return value determines whether this validator should be applied on the client-side. yii\validators\Validator

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$_cards array The final list of cards based on allowedCards kartik\validators\CardValidator
$_msgCat string Translation message file category name for i18n. kartik\base\TranslationTrait

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\Component
__clone() This method is called after the object is created by cloning an existing one. yii\base\Component
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of a component property. yii\base\Component
__isset() Checks if a property is set, i.e. defined and not null. yii\base\Component
__set() Sets the value of a component property. yii\base\Component
__unset() Sets a component property to be null. yii\base\Component
addError() Adds an error about the specified attribute to the model object. yii\validators\Validator
attachBehavior() Attaches a behavior to this component. yii\base\Component
attachBehaviors() Attaches a list of behaviors to the component. yii\base\Component
behaviors() Returns a list of behaviors that this component should behave as. yii\base\Component
canGetProperty() Returns a value indicating whether a property can be read. yii\base\Component
canSetProperty() Returns a value indicating whether a property can be set. yii\base\Component
className() Returns the fully qualified name of this class. yii\base\BaseObject
clientValidateAttribute() Returns the JavaScript needed for performing client-side validation. kartik\validators\CardValidator
createValidator() Creates a validator object. yii\validators\Validator
detachBehavior() Detaches a behavior from the component. yii\base\Component
detachBehaviors() Detaches all behaviors from the component. yii\base\Component
ensureBehaviors() Makes sure that the behaviors declared in behaviors() are attached to this component. yii\base\Component
getAttributeNames() Returns cleaned attribute names without the ! character at the beginning. yii\validators\Validator
getBehavior() Returns the named behavior object. yii\base\Component
getBehaviors() Returns all behaviors attached to this component. yii\base\Component
getClientOptions() Returns the client-side validation options. yii\validators\Validator
getValidationAttributes() Returns a list of attributes this validator applies to. yii\validators\Validator
hasEventHandlers() Returns a value indicating whether there is any handler attached to the named event. yii\base\Component
hasMethod() Returns a value indicating whether a method is defined. yii\base\Component
hasProperty() Returns a value indicating whether a property is defined for this component. yii\base\Component
init() Initializes the object. kartik\validators\CardValidator
initI18N() Yii i18n messages configuration for generating translations kartik\base\TranslationTrait
isActive() Returns a value indicating whether the validator is active for the given scenario and attribute. yii\validators\Validator
isEmpty() Checks if the given value is empty. yii\validators\Validator
isValidCVV() Check if the card CVV is valid kartik\validators\CardValidator
isValidExpiry() Validates if the card expiry is correct kartik\validators\CardValidator
isValidHolder() Check if the card holder name is valid kartik\validators\CardValidator
luhnCheck() Luhn checksum check kartik\validators\CardValidator
off() Detaches an existing event handler from this component. yii\base\Component
on() Attaches an event handler to an event. yii\base\Component
trigger() Triggers an event. yii\base\Component
validate() Validates a given value. yii\validators\Validator
validateAttribute() Validates a single attribute. kartik\validators\CardValidator
validateAttributes() Validates the specified object. yii\validators\Validator

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
formatMessage() Formats a mesage using the I18N, or simple strtr if \Yii::$app is not available. yii\validators\Validator
getCardProp() Gets the property of a card kartik\validators\CardValidator
getCardType() Gets card type kartik\validators\CardValidator
getExpiryDate() Gets expiry date kartik\validators\CardValidator
initSettings() Initialize settings kartik\validators\CardValidator
initValues() Initializes related attribute values kartik\validators\CardValidator
parseValue() Gets value based on attribute property or the value property kartik\validators\CardValidator
setMsg() Sets message property kartik\validators\ValidatorTrait
validCVVLength() Check if the card CVV length is valid kartik\validators\CardValidator
validCard() Check if the card number is valid kartik\validators\CardValidator
validLuhn() Check if the card luhn checksum is valid kartik\validators\CardValidator
validateValue() Validates a value. kartik\validators\CardValidator

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
AMEX 'American Express' kartik\validators\CardValidator
BC_GLOBAL 'BC Global' kartik\validators\CardValidator
CARTE_BLANCHE 'Carte Blanche' kartik\validators\CardValidator
DANKORT 'Dankort' kartik\validators\CardValidator
DINERS 'Diners Club' kartik\validators\CardValidator
DISCOVER 'Discover' kartik\validators\CardValidator
ELECTRON 'Visa Electron' Valid Card list kartik\validators\CardValidator
FBF 'Forbrugsforeningen' kartik\validators\CardValidator
INSTA_PAY 'Insta Payment' kartik\validators\CardValidator
JCB 'JCB' kartik\validators\CardValidator
KOREAN_LOCAL 'Korean Local' kartik\validators\CardValidator
LASER 'Laser' kartik\validators\CardValidator
MAESTRO 'Maestro' kartik\validators\CardValidator
MASTERCARD 'Mastercard' kartik\validators\CardValidator
MIR 'mir' kartik\validators\CardValidator
SOLO 'Solo' kartik\validators\CardValidator
SWITCH_CARD 'Switch Card' kartik\validators\CardValidator
UNIONPAY 'Union Pay' kartik\validators\CardValidator
VISA 'Visa' kartik\validators\CardValidator
VOYAGER 'Voyager' kartik\validators\CardValidator

Property Details

$_cards protected property

The final list of cards based on allowedCards

protected array $_cards = []
$allowedCards public property

List of allowed cards. If not set or empty, all cards set in $cards will be allowed. For example, to allow only VISA and MASTERCARD, set this to [CardValidator::VISA, CardValidator::MASTERCARD].

public array $allowedCards = []
$autoDetectType public property

Whether to auto detect card type based on pattern detection in $cards configuration and auto update the $typeValue.

public boolean $autoDetectType true
$autoUpdateNumber public property

Whether to auto update the card number as pure numeric digits and overwrite the model attribute

public boolean $autoUpdateNumber true
$autoUpdateType public property

Whether to auto update and store the auto detected type within the $typeAttribute. Applicable only when $autoDetectType is set to true.

public boolean $autoUpdateType true
$cards public property

Holds the configurations for each of the credit / debit cards including the card lengths and regex patterns to check for valid card number prefixes

public array $cards = [
    self::ELECTRON => ['pattern' => '/^(?:417500|4026\\d{2}|4917\\d{2}|4913\\d{2}|4508\\d{2}|4844\\d{2})\\d{10}$/''cvvLength' => [3], 'luhn' => true],
    
self::MAESTRO => ['pattern' => ' /^(5[06-9]|6[37])\d*$/''cvvLength' => [3], 'luhn' => true],
    
self::FBF => ['pattern' => '/^600[0-9]{13}$/''cvvLength' => [3], 'luhn' => true],
    
self::DANKORT => ['pattern' => '/^5019[0-9]{12}$/''cvvLength' => [3], 'luhn' => true],
    
self::VISA => ['pattern' => '/^4[0-9]{12}([0-9]{3})?$/''cvvLength' => [3], 'luhn' => true],
    
self::MASTERCARD => ['pattern' => '/^(5[0-5]|2[2-7])[0-9]{14}$/''cvvLength' => [3], 'luhn' => true],
    
self::AMEX => ['pattern' => '/^3[47][0-9]{13}$/''cvvLength' => [3,
    
4], 'luhn' => true],
    
self::CARTE_BLANCHE => ['pattern' => '/^389[0-9]{11}$/''cvvLength' => [3], 'luhn' => true],
    
self::DINERS => ['pattern' => '/^3(?:0[0-5]|[68][0-9])[0-9]{11}$/''cvvLength' => [3], 'luhn' => true],
    
self::BC_GLOBAL => ['pattern' => '/^(6541|6556)[0-9]{12}$/''cvvLength' => [3], 'luhn' => true],
    
self::DISCOVER => ['pattern' => '/^65[4-9][0-9]{13}|64[4-9][0-9]{13}|6011[0-9]{12}|(622(?:12[6-9]|1[3-9][0-9]|[2-8][0-9][0-9]|9[01][0-9]|92[0-5])[0-9]{10})$/''cvvLength' => [3], 'luhn' => true],
    
self::INSTA_PAY => ['pattern' => '/^63[7-9][0-9]{13}$/''cvvLength' => [3], 'luhn' => true],
    
self::JCB => ['pattern' => '/^(3[0-9]{4}|2131|1800)[0-9]{11}$/''cvvLength' => [3], 'luhn' => true],
    
self::VOYAGER => ['pattern' => '/^8699[0-9]{11}$/''cvvLength' => [3], 'luhn' => true],
    
self::KOREAN_LOCAL => ['pattern' => '/^9[0-9]{15}$/''cvvLength' => [3], 'luhn' => true],
    
self::SOLO => ['pattern' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/''cvvLength' => [3], 'luhn' => true],
    
self::SWITCH_CARD => ['pattern' => '/^(4903|4905|4911|4936|6333|6759)[0-9]{12}|(4903|4905|4911|4936|6333|6759)[0-9]{14}|(4903|4905|4911|4936|6333|6759)[0-9]{15}|564182[0-9]{10}|564182[0-9]{12}|564182[0-9]{13}|633110[0-9]{10}|633110[0-9]{12}|633110[0-9]{13}$/''cvvLength' => [3], 'luhn' => true],
    
self::LASER => ['pattern' => '/^(6304|6706|6709|6771)[0-9]{12,15}$/''cvvLength' => [3], 'luhn' => true],
    
self::UNIONPAY => ['pattern' => '/^(62|88)[0-9]{14,17}$/''cvvLength' => [3], 'luhn' => false],
    
self::MIR => ['pattern' => '/^220[0-4][0-9]{12}$/''cvvLength' => [3], 'luhn' => true]
]
$cvvAttribute public property

The card CVV attribute

public integer $cvvAttribute null
$cvvValue public property

Card cvv to check (overrides value set in $cvvAttribute)

public integer $cvvValue null
$expiryMonthAttribute public property

The card expiry month attribute

$expiryMonthValue public property

Card expiry month to check (overrides value set in $expiryMonthAttribute)

public integer $expiryMonthValue null
$expiryYearAttribute public property

The card expiry year attribute

$expiryYearValue public property

Card expiry year to check (overrides value set in $expiryYearAttribute)

public integer $expiryYearValue null
$holderAttribute public property

The card holder attribute

public string $holderAttribute null
$holderPattern public property

The regular expression pattern to match for card holder name

public string $holderPattern '/^[a-z ,.\'-]+$/i'
$holderValue public property

Card holder name to check (overrides value set in $holderAttribute)

public string $holderValue null
$invalidCVVMessage public property

The message shown if the detected card CVV is invalid

public string $invalidCVVMessage null
$invalidExpiryMessage public property

The message shown if the detected card expiry year or month is invalid

$invalidHolderMessage public property

The message shown if the detected card holder is invalid

$invalidTypeMessage public property

The message shown if the detected card type is invalid

$typeAttribute public property

The card type attribute

public string $typeAttribute null
$typeValue public property

Card type to check (overrides value set in $typeAttribute)

public string $typeValue null
$validateCVV public property

Whether to validate the card CVV

public boolean $validateCVV true
$validateExpiry public property

Whether to validate the card expiry date (year / month)

public boolean $validateExpiry true
$validateHolder public property

Whether to validate the card holder name

public boolean $validateHolder true

Method Details

clientValidateAttribute() public method

Returns the JavaScript needed for performing client-side validation.

Calls getClientOptions() to generate options array for client-side validation.

You may override this method to return the JavaScript validation code if the validator can support client-side validation.

The following JavaScript variables are predefined and can be used in the validation code:

  • attribute: an object describing the the attribute being validated.
  • value: the value being validated.
  • messages: an array used to hold the validation error messages for the attribute.
  • deferred: an array used to hold deferred objects for asynchronous validation
  • $form: a jQuery object containing the form element

The attribute object contains the following properties:

  • id: a unique ID identifying the attribute (e.g. "loginform-username") in the form
  • name: attribute name or expression (e.g. "[0]content" for tabular input)
  • container: the jQuery selector of the container of the input field
  • input: the jQuery selector of the input field under the context of the form
  • error: the jQuery selector of the error tag under the context of the container
  • status: status of the input field, 0: empty, not entered before, 1: validated, 2: pending validation, 3: validating
public string|null clientValidateAttribute ( $model, $attribute, $view )
$model yii\base\Model

The data model being validated

$attribute string

The name of the attribute to be validated.

$view yii\web\View

The view object that is going to be used to render views or view files containing a model form with this validator applied.

return string|null

The client-side validation script. Null if the validator does not support client-side validation.

getCardProp() protected method

Gets the property of a card

protected mixed getCardProp ( $prop )
$prop string

The card property

throws yii\base\InvalidValueException
getCardType() protected method

Gets card type

protected string getCardType ( $number )
$number string
getExpiryDate() protected method

Gets expiry date

protected string getExpiryDate ( )
init() public method

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )
throws ReflectionException
initSettings() protected method

Initialize settings

protected void initSettings ( )
throws ReflectionException
initValues() protected method

Initializes related attribute values

protected void initValues ( $model, $attribute )
$model yii\base\Model

The model instance

$attribute string

The attribute

isValidCVV() public method

Check if the card CVV is valid

public boolean isValidCVV ( )
isValidExpiry() public method

Validates if the card expiry is correct

public boolean isValidExpiry ( )
return boolean

The validation result

isValidHolder() public method

Check if the card holder name is valid

public boolean isValidHolder ( )
return boolean

Validation result

luhnCheck() public static method

Luhn checksum check

public static boolean luhnCheck ( $number )
$number string|integer

The card number

parseValue() protected method

Gets value based on attribute property or the value property

protected mixed|null|string parseValue ( $model, $prop, $default null )
$model yii\base\Model

The model instance

$prop string

The attribute property

$default string

The default value

validCVVLength() protected method

Check if the card CVV length is valid

protected boolean validCVVLength ( )
validCard() protected method

Check if the card number is valid

protected boolean validCard ( $number )
$number string|integer

The card number

validLuhn() protected method

Check if the card luhn checksum is valid

protected boolean validLuhn ( $number )
$number string|integer

The card number

validateAttribute() public method

Validates a single attribute.

Child classes must implement this method to provide the actual validation logic.

public void validateAttribute ( $model, $attribute )
$model yii\base\Model

The data model to be validated

$attribute string

The name of the attribute to be validated.

validateValue() protected method

Validates a value.

A validator class can implement this method to support data validation out of the context of a data model.

protected array|null validateValue ( $value )
$value mixed

The data value to be validated.

return array|null

The error message and the array of parameters to be inserted into the error message.