2017-02-28 18 views
0

我正在開發一個symfony 3.2項目,並且正在添加一個帶有一些驗證的簡單信用卡表單。我在2.7項目中構建了一個類似的表單,並使用了約束選項。沒有數據類的表單上的Contraints

  ->add('SecurityCode', TextType::class, array(
       'required' => true, 
       'constraints' => array(
         new Length(array('min' => 3)), 
         new Length(array('max' => 3))), 
       'empty_data' => null 
      )) 

當我在3.2運行此我得到一個「選項‘約束’不存在」的錯誤。我查看了兩個版本之間的文檔,並沒有顯示任何使用方面的差異。我錯過了什麼嗎?

添加錯誤信息...... enter image description here

編輯: 這可能是我如何打電話的形式。我沒有在控制器中創建表單,所以我創建了自己的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

你能不能展示你的'createFormBuilder'部分如何創建表單?我認爲問題在那裏。 –

+0

是的,我認爲這可能也是。 – Cathmor

+0

增加了編輯#2部分 - >你可以試試嗎? –

回答

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

我認爲這是正確的,但仔細檢查我。

+0

是的,我有一個使用聲明,並可能錯過了一個右括號時複製粘貼在這裏。 – Cathmor

+0

你還有錯誤嗎? –

+0

是的,錯誤在於約束不是數組的有效鍵。 – Cathmor

相關問題