cakephp 3验证信用卡号码无效

时间:2017-11-10 08:00:08

标签: validation cakephp cakephp-3.0 credit-card

如果我使用它,它运作良好

$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;
    }
);

它不断给我这个错误信息

  

注意(8):未定义的索引:mastercard [CORE \ src \ Validation \ Validation.php,第194行]

1 个答案:

答案 0 :(得分:1)

尝试将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中似乎有一个拼写错误:

  

字符串$ type optional'全部'

     

您想要允许的卡片类型。默认为“全部”。你也可以   提供一系列可接受的卡类型。例如['mastercard', 'visa', 'amex']