带附加选项的实体字段

问题描述:

希望有人帮助我,我无法绕过这个。带附加选项的实体字段

我正在使用Symfony 3.1.6。

我有两个实体,地区和城市,关系一对多。我有一个表单,加载区域的选择框,并在选择至极区域的功能显示该地区的城市。这很好。

我已通过finishView()向城市组合框中添加了一个选项,以便为用户提供oportunity,如果他的城市未列出,则选择此选项并输入出现的输入提供添加的可能性一个新的城市。此选项值为0.

但是,当表单被引用时,会显示一个错误,并显示“此值无效”。我认为这是因为没有任何区域的id = 0的任何城市的选项的价值。然后,我需要的是禁用/消除/这是什么验证,但我不知道该怎么做。

我已经tryed:

$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { 
     $event->stopPropagation(); 
    }, 900); 

但没有工作....

显然,我可以去ChoiceType代替的EntityType,消除实体或其他不那么优雅的方式之间的关系解决此问题。但我确信有一种方法,这是Symfony。

编辑1:添加的locationType

<?php 
namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\Form\FormEvent; 
use Symfony\Component\Form\FormEvents; 
use Doctrine\ORM\EntityRepository; 
use Symfony\Component\Form\Extension\Core\Type\CountryType; 
use Symfony\Component\Form\Extension\Core\Type\TextType; 
use Symfony\Bridge\Doctrine\Form\Type\EntityType; 
use Symfony\Component\Form\FormView; 
use Symfony\Component\Form\FormInterface; 
use Symfony\Component\Form\ChoiceList\View\ChoiceView; 

class LocationType extends AbstractType 
{ 

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 

    $region_options = array(
     'class' => 'AppBundle:Region', 
     'choice_label' => 'name', 
     'label' => 'Region', 
     'translation_domain' => 'messages', 
     'required' => true, 
     'mapped' => false, 
    ); 

    $city_options = array(
     'class' => 'AppBundle:City', 
     'choice_label' => 'name', 
     'label' => 'City', 
     'translation_domain' => 'messages', 
     'required' => true, 
     'mapped' => false, 
    ); 

    $alt_city_options = array(
     'required' => false, 
     'mapped' => true, 
     'label' => 'Alternative city', 
     'translation_domain' => 'messages', 
     'attr' => array(
      'placeholder' => 'Enter your city name', 
     ), 
    ); 

    $builder->addEventListener(
     FormEvents::PRE_SET_DATA, 
     function (FormEvent $event) use ($region_options, $city_options, $alt_city_options) { 
      $form = $event->getForm(); 
      $data = $event->getForm()->getParent()->getData(); 

      $country = '332'; 

      $region_options['query_builder'] = function (EntityRepository $er) use ($country) { 
        return $er->createQueryBuilder('r') 
         ->where('r.country = :country') 
         ->setParameter('country', $country) 
         ->orderBy('r.name', 'ASC'); 
       }; 

       if (!empty($data->getRegion())) { 
        $region_options['data'] = $data->getRegion()->getId(); 
       } 
       $form->add('region', EntityType::class, $region_options); 
      } 

      if (!empty($data->getRegion())) { 
       $region = $data->getRegion(); 
       $city_options['query_builder'] = function (EntityRepository $er) use ($country, $region) { 
        return $er->createQueryBuilder('c') 
         ->where('c.country = :country') 
         ->andWhere('c.region = :region') 
         ->setParameter('country', $country) 
         ->setParameter('region', $region) 
         ->orderBy('c.name', 'ASC'); 
       }; 

       if (!empty($data->getCity())) { 
        $region_options['data'] = $data->getCity()->getId(); 
       } 
       $form->add('city', EntityType::class, $city_options); 
      } 
      $form->add('alt_city', TextType::class, $alt_city_options); 
     } 
    ); 

    $builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) { 
     $event->stopPropagation(); 
    }, 900); 
} 

public function finishView(FormView $view, FormInterface $form, array $options) 
{ 
    $new_choice = new ChoiceView(array(), '0', 'not_in_the_list'); 
    $view->children['city']->vars['choices'][] = $new_choice; 
} 

}

希望这有助于。

您可以将我们的EntityType字段定义粘贴到我们吗?

这可能有助于解决问题。

要设置的EntityType场,你必须指定下列选项:

  • CHOICE_LABEL(除非你有一个__toString在实体()函数)