Luhn
此约束用于确保信用卡号码通过 Luhn 算法。它作为验证信用卡的第一步非常有用:在与支付网关通信之前。
适用于 | 属性或方法 |
类 | Luhn |
验证器 | LuhnValidator |
基本用法
要使用 Luhn 验证器,请将其应用于对象上将包含信用卡号码的属性。
1 2 3 4 5 6 7 8 9 10
// src/Entity/Transaction.php
namespace App\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class Transaction
{
#[Assert\Luhn(message: 'Please check your credit card number.')]
protected string $cardNumber;
}
注意
与大多数其他约束一样,null
和空字符串被视为有效值。 这是为了允许它们成为可选值。 如果该值是强制性的,一个常见的解决方案是将此约束与 NotBlank 结合使用。
这项工作,包括代码示例,根据 Creative Commons BY-SA 3.0 许可获得许可。