1

if I use this, it works well

$validator->creditCard(
    'cc_number',
    'all',
    'Invalid credit card number',
    function ($context) {
        if($context['data']['payment_method_id'] == 1)
            return true;
    }
);

but when i change the all to ['mastercard', 'visa', 'amex']

$validator->creditCard(
    'cc_number',
    ['mastercard', 'visa', 'amex'],
    'Invalid credit card number',
    function ($context) {
        if($context['data']['payment_method_id'] == 1)
            return true;
    }
);

it keeps giving me this error message

Notice (8): Undefined index: mastercard [CORE\src\Validation\Validation.php, line 194]

ndm
  • 59,784
  • 9
  • 71
  • 110
cakephp_dev1
  • 159
  • 1
  • 6

1 Answers1

1

Try replacing mastercard with mc:

$validator->creditCard(
    'cc_number',
    ['mc', 'visa', 'amex'],
    'Invalid credit card number',
    function ($context) {
        if($context['data']['payment_method_id'] == 1)
            return true;
    }
);

From the source code:

$cards = [
    'all' => [
        'amex' => '/^3[47]\\d{13}$/',
        'bankcard' => '/^56(10\\d\\d|022[1-5])\\d{10}$/',
        'diners' => '/^(?:3(0[0-5]|[68]\\d)\\d{11})|(?:5[1-5]\\d{14})$/',
        'disc' => '/^(?:6011|650\\d)\\d{12}$/',
        'electron' => '/^(?:417500|4917\\d{2}|4913\\d{2})\\d{10}$/',
        'enroute' => '/^2(?:014|149)\\d{11}$/',
        'jcb' => '/^(3\\d{4}|2131|1800)\\d{11}$/',
        'maestro' => '/^(?:5020|6\\d{3})\\d{12}$/',
        'mc' => '/^(5[1-5]\\d{14})|(2(?:22[1-9]|2[3-9][0-9]|[3-6][0-9]{2}|7[0-1][0-9]|720)\\d{12})$/',
        'solo' => '/^(6334[5-9][0-9]|6767[0-9]{2})\\d{10}(\\d{2,3})?$/',
        'switch' => '/^(?:49(03(0[2-9]|3[5-9])|11(0[1-2]|7[4-9]|8[1-2])|36[0-9]{2})\\d{10}(\\d{2,3})?)|(?:564182\\d{10}(\\d{2,3})?)|(6(3(33[0-4][0-9])|759[0-9]{2})\\d{10}(\\d{2,3})?)$/',
        'visa' => '/^4\\d{12}(\\d{3})?$/',
        'voyager' => '/^8699[0-9]{11}$/'
    ],
    'fast' => '/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/'
];

There seems to be a typo in the docs:

string $type optional 'all'

The type of cards you want to allow. Defaults to 'all'. You can also supply an array of accepted card types. e.g ['mastercard', 'visa', 'amex']

Inigo Flores
  • 4,461
  • 1
  • 15
  • 36