2017-02-14 48 views
2

我想在Symfony 3.0.9中定義一些ChoiceType,表單中填充了ajax,具體取決於動態創建了多少個選項,但驗證表單表示選擇的選項不是有效。Symfony ChoiceType初始化空白數組

我定義ChoiceType這樣的:

->add('position', ChoiceType::class, [ 
    'placeholder' => 'Select position', 
    'choices' => [], 
    'attr' => [ 
     'class' => 'form-control choice-position' 
    ], 
]) 

而我得到的錯誤是:

Symfony\Component\Validator\ConstraintViolation 
Object(Symfony\Component\Form\Form).children[lessonGroups].children[0].children[position] = 1 

Caused by: 
Symfony\Component\Form\Exception\TransformationFailedException 
Unable to reverse value for property path "position": The choice "1" does not exist or is not unique 

Caused by: 
Symfony\Component\Form\Exception\TransformationFailedException 
The choice "1" does not exist or is not unique 

,如果有需要的任何進一步的信息,我不知道。

謝謝!

+0

你是什麼symfony的版本? – goto

+0

您需要查看錶單事件 - http://symfony.com/doc/current/form/dynamic_form_modification.html – Rooneyl

回答

2

因爲你的定義不包括任何選擇('choices' => [],)Symfony檢測比用戶嘗試提交的結果不在最初的授權結果。

您可以設置包含所有可用值的選擇初始陣列或者您可以通過使用禁用驗證:

$builder->get('yourField')->resetViewTransformers(); 
+0

噢!我不知道。我認爲,如果我填充該數組之後,定義將不會在意......無論如何,我只是添加了該代碼行並且工作完美。謝謝! –