2013-07-23 15 views
6

2つの選択フィールドがあります。動的に入力された選択フィールドを検証する

フォームをビルドするとき、依存フィールドに空の選択肢の配列があります。

JavaScriptのこのフィールドに、アクションのデータを要求します。

問題は検証から発生します。もちろん、1つまたは複数の値を空の値に対して有効にすることはできないため、合格しません。 私はPRE_BINDリスナーを作成しましたが、これは基本的に適切な値で選択フィールドを削除しても、それでも検証をパスしません。

$form->getErrors()$form->getErrorsAsString()は私の選択フィールドにエラーを返します。

マイ形式:

<?php 

namespace Foo\BarBundle\Form\Type; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\Form\FormEvent; 
use Symfony\Component\Form\FormEvents; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 

class BarFormType extends AbstractType 
{ 
    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     // other fields 

     // This field is filled in ajax 
     $builder->add('stores', 'choice', array(
      'label' => 'form.label.stores', 
      'translation_domain' => 'FooBarBundle', 
      'choices' => $options['storesList'], 
      'required' => false, 
      'multiple' => true, 
      'auto_initialize' => false, 
      'attr' => array(
       'class' => 'chzn-select', 
       'placeholder' => 'form.placeholder.stores' 
     ))); 

     $func = function (FormEvent $e) use ($options) { 
      $data = $e->getData(); 
      $form = $e->getForm(); 
      if ($form->has('stores')) { 
       $form->remove('stores'); 
      } 

      $brand = isset($data['brand']) ? $data['brand'] : null; 

      if ($brand !== null) { 
       $choices = $options['miscRepo']->getStoresNameIndexedById($brand); 
       $choices = array_keys($choices); 
       $choices = array_map('strval', $choices); 
      } else { 
       $choices = array(); 
      } 

      $form->add('stores', 'choice', array('choices' => $choices, 'multiple' => true, 'attr' => array('class' => 'chzn-select'))); 
     }; 

     $builder->addEventListener(FormEvents::PRE_SUBMIT, $func); 
    } 

    public function getName() 
    { 
     return 'bar_form_campaign'; 
    } 

    public function setDefaultOptions(OptionsResolverInterface $resolver) 
    { 
     $resolver->setRequired(array(
      'storesList', 
      'miscRepo', 
     )); 
    } 
} 
+2

どのようなエラーが返されますか?選択肢は、値ではなく、「選択肢」配列の*キー*に含まれなければならないことに注意してください。 –

+2

'Entity'フォームタイプの使用はどうですか? http://symfony.com/doc/current/reference/forms/types/entity.html – gondo

+0

"店舗"フォーム要素を埋めるために使用するajaxを記述できますか?どのようなエラーが返されますか? – jdharandas

答えて

0

私はあなたのように同じ問題が持っていた:JavaScriptでフィールドを更新しますが、検証に合格しませんでした。 PRE_SUBMITイベントで、javascriptで追加した値を読み込み、そのIDでオブジェクトを取得し、フィールドの選択肢を更新します。

$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) { 
    $data = $event->getData(); 
    $form = $event->getForm(); 
    $pageId = $data['page_id']; 

    $page = $this->myManager->getPage($pageId); 
    $options = array($page->getId() => $page->getTitle()); 

    $form->add('page_id', 'choice', array(
     'label'   => 'Select page', 
     'choices'  => $options, 
     'required'  => true, 
     'placeholder' => 'Select page' 
    )); 

    $form->getData()->setPageId($pageId); 
});