cakephp 3 验证信用卡号码不起作用

cakephp 3 validate credit card number not working

如果我用这个,效果很好

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

但是当我将 all 更改为 ['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;
    }
);

它一直给我这个错误信息

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

尝试将 mastercard 替换为 mc:

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

来自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})$/'
];

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']