Php Symfony2执行表单验证,而不是数据库验证
我有一个信用卡实体,现在我想在实体中对此进行基本验证,例如:Php Symfony2执行表单验证,而不是数据库验证,php,validation,symfony,encryption,Php,Validation,Symfony,Encryption,我有一个信用卡实体,现在我想在实体中对此进行基本验证,例如: /** * @ORM\Column(name="number", type="string", length=255) * @Assert\CardScheme( * schemes={"VISA", "MASTERCARD", "DISCOVER", "DINERS"}, * message="The card number is not valid, we only support Visa, Master
/**
* @ORM\Column(name="number", type="string", length=255)
* @Assert\CardScheme(
* schemes={"VISA", "MASTERCARD", "DISCOVER", "DINERS"},
* message="The card number is not valid, we only support Visa, Mastercard, Discover og Diners club."
* )
*/
private $number;
但问题是我存储的卡号是加密的
它们最终看起来完全不同,在我的表单类型中,我这样做是为了加密值并删除所有不成功的内容
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('number')
->add('expiration_year', 'choice', array(
'required' => true,
'choices' => $this->buildYearChoices()
))
->add('expiration_month', 'choice', array(
'required' => true,
'choices' => array(
'01' => '01',
'02' => '02',
'03' => '03',
'04' => '04',
'05' => '05',
'06' => '06',
'07' => '07',
'08' => '08',
'09' => '09',
'10' => '10',
'11' => '11',
'12' => '12',
)
))
->add('cvc', 'text');
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event)
{
$data = $event->getData();
$number = preg_replace("/[^0-9]/", "", $data['number']);
$data['number'] = $number;
$event->setData($data);
});
$builder->addEventListener(FormEvents::SUBMIT, function (FormEvent $event)
{
$credit_card = $event->getData();
$number = $credit_card->getNumber();
$customer_helper = $this->container->get('customer_helper');
$crypt = $customer_helper->encryptCreditCardNumber($number);
$credit_card->setNumber($crypt);
$event->setData($credit_card);
});
}
即使我知道表单验证是在提交前执行的,但当我在提交时加密表单时,仍然会出现验证错误
编辑:加密和解密方法:
public function encryptCreditCardNumber($plaintext) {
$ivSize = mcrypt_get_iv_size(self::CIPHER, self::MODE); //Gets the size of the IV belonging to a specific cipher/mode combination.
$iv = mcrypt_create_iv($ivSize, MCRYPT_DEV_RANDOM); //Creates an initialization vector (IV) from a random source.
$ciphertext = mcrypt_encrypt(self::CIPHER, $this->container->getParameter('cc_encryption_key'), $plaintext, self::MODE, $iv); //Encrypts the data and returns it.
return base64_encode($iv.$ciphertext); //Encode Base 64
}
public function decryptCreditCardNumber($ciphertext) {
$ciphertext = base64_decode($ciphertext); //Decode Base 64
$ivSize = mcrypt_get_iv_size(self::CIPHER, self::MODE); //Gets the size of the IV belonging to a specific cipher/mode combination.
if (strlen($ciphertext) < $ivSize) {
throw new Exception('Missing initialization vector');
}
$iv = substr($ciphertext, 0, $ivSize);
$ciphertext = substr($ciphertext, $ivSize);
$plaintext = mcrypt_decrypt(self::CIPHER, $this->container->getParameter('cc_encryption_key'), $ciphertext, self::MODE, $iv); //Decrypts the data and returns it.
return rtrim($plaintext, "\0");
}
公共函数encryptCreditCardNumber($明文){
$ivSize=mcrypt_get_iv_size(self::CIPHER,self::MODE);//获取属于特定密码/模式组合的iv的大小。
$iv=mcrypt_create_iv($ivSize,mcrypt_DEV_RANDOM);//从随机源创建初始化向量(iv)。
$ciphertext=mcrypt_encrypt(self::CIPHER,$this->container->getParameter('cc_encryption_key'),$plaintext,self::MODE,$iv);//加密数据并返回它。
返回base64_encode($iv.$ciphertext);//编码base64
}
公共函数解密CreditCardNumber($ciphertext){
$ciphertext=base64_decode($ciphertext);//解码base64
$ivSize=mcrypt_get_iv_size(self::CIPHER,self::MODE);//获取属于特定密码/模式组合的iv的大小。
if(strlen($ciphertext)<$ivSize){
抛出新异常(“缺少初始化向量”);
}
$iv=substr($ciphertext,0,$ivSize);
$ciphertext=substr($ciphertext,$ivSize);
$plaintext=mcrypt_decrypt(self::CIPHER,$this->container->getParameter('cc_encryption_key'),$ciphertext,self::MODE,$iv);//解密数据并返回它。
返回rtrim($plaintext,“\0”);
}
您可以捕获doctrine事件prePersist
和preUpdate
以动态地将普通代码替换为加密代码(就在保存数据之前,以及表单验证之后)
要解密它,可以使用postLoad
更多关于
请注意,条令事件与表单事件不同,不会干扰表单或验证(甚至可能根本没有表单)
编辑:插入键
每当您需要将静态信息注入实体(如参数或函数)时,请使用依赖项注入
如果您有一个bundle,请转到DependencyInjection
文件夹并编辑Configuration.php
文件(或者是另一个?我现在不记得了)
您想要找到的是使用load
方法加载参数的方法
紧接着,从配置中获取所需的参数,并在类的静态成员中设置它,如下所示:
MyEntity::$secretKey = $configuration->getParameter('secretkey');
在实体内部,只需阅读以下信息:
self::$secretKey
我是在脑子里写的,可能有很多不准确的地方。必要时进行纠正。关键在于如上所示静态注入参数。您可以为验证目的创建一个非映射实体属性:
/**
* @Assert\CardScheme(
* schemes={"VISA", "MASTERCARD", "DISCOVER", "DINERS"},
* message="The card number is not valid, we only support Visa, Mastercard, Discover og Diners club."
* )
*/
private $rawNumber;
/**
* @ORM\Column(name="number", type="string", length=255)
*/
private $number;
然后使用表单验证后的rawNumber的加密值设置数字:
$creditCard->setNumber(
your_encryption_function(
$creditCard->getRawNumber()
)
);
问题是,我需要将事件放在我的实体中,对吗?加密密钥存储在parameters.yml中,因此我不会将其保存在代码中,但我无法将容器放入实体中,据我所知,这样做是不好的做法我发布了我的加密和解密方法,为了澄清,我将编辑我的答案,向您展示如何注入密钥。答案现在应该概述如何解决问题,并且应该很容易实现。如果有一些深夜的错误,请原谅。