我正在研究symfony 3.2项目,并添加了一个简单的信用卡表单并进行了一些验证。我在2.7项目中构建了一个类似的表单,并使用了约束选项。
->add('SecurityCode', TextType::class, array(
'required' => true,
'constraints' => array(
new Length(array('min' => 3)),
new Length(array('max' => 3))),
'empty_data' => null
))
当我在3.2中运行时,我得到一个“选项'约束'不存在”错误。我查看了文档和两个版本之间,并没有显示任何使用上的差异。我错过了什么吗?
编辑: 这可能就是我打电话给表格的方式。我没有在控制器中创建表单,所以我正在创建自己的FormFactory。
use Symfony\Component\Form\Forms;
//...
$x = Forms::createFormFactory();
$form = $x->create('My\Bundle\Form\CreditCardFormType');
这是我的FormType
namespace My\Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints as Assert;
class CreditCardFormType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('cardType', ChoiceType::class, array(
'label' => 'Payment Type',
'choices' => array(
'visa' => 'visa',
'mastercard' => 'mastercard',
'discover' => 'discover'
),
'data' => 'visa',
'required' => true,
'multiple' => false,
'expanded' => true,
))
->add('CardNumber', TextType::class, array(
'required' => true,
'constraints' => array(
new Assert\CardScheme(array(
'schemes' => array('VISA', 'MASTERCARD', 'DISCOVER'),
'message' => 'The credit card number you entered is invalid.')
)),
'empty_data' => null
))
->add('SecurityCode', TextType::class, array(
'required' => true,
'constraints' => array(
new Length(array('min' => 3)),
new Length(array('max' => 3))),
'empty_data' => null
))
->add('ExpMonth', ChoiceType::class,array('required' => true, 'choices' => (range(0,12))))
->add('ExpYear', ChoiceType::class, array('required' => true, 'choices' => array(
Date('Y') => Date('Y'),
Date('Y') + 1 => Date('Y') + 1,
Date('Y') + 2 => Date('Y') + 2,
Date('Y') + 3 => Date('Y') + 3,
//...add more years
->add('save', SubmitType::class, array('label' => 'Place Order'))
;
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => null,
));
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'credit_card_form';
}
}
答案 0 :(得分:0)
确保文件顶部有以下用法声明:
use Symfony\Component\Validator\Constraints\Length;
另外,我认为你需要这样写:
->add('SecurityCode', TextType::class, array(
'required' => true,
'constraints' => array(
new Length( array('min' => 3, 'max' => 3) ),
'empty_data' => null
))
顺便说一下,你的代码有一个额外的结束括号 - >我不确定这是否会引起问题。
根据反馈编辑#2。
在configureOptions中尝试此操作:
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$collectionConstraint = new Collection(array(
'CardNumber' => array(
new Assert\CardScheme(array(
'schemes' => array('VISA', 'MASTERCARD', 'DISCOVER'),
'message' => 'The credit card number you entered is invalid.'))
),
'SecurityCode' => array(
new Length(array('min' => 3, 'max' => 3))
)
));
$resolver->setDefaults(array(
'data_class' => null,
'constraints' => $collectionConstraint
));
}
我认为这是对的,但要仔细检查一下。