2016-11-14 1 views
0

누군가가 나를 돕기를 원한다. 나는 이것을 얻을 수 없다.추가 옵션이있는 형식의 엔티티 필드

Symfony 3.1.6을 사용하고 있습니다.

하나의 관계와 많은 관계가있는 두 개의 엔터티, Region 및 Cities가 있습니다. 지역을로드하는 선택 상자가있는 양식이 있고 선택한 지역의 기능에이 지역의 도시가 표시됩니다. 그건 잘된거야.

나는 그의 도시가 목록에 없으면이 옵션을 선택할 수있는 기회를 제공하기 위해 finishView()를 통해 도시 콤보 상자에 옵션을 추가했으며 표시되는 입력은 추가 할 가능성을 제공합니다 새로운 도시. 이 옵션 값은 0입니다.

그러나 양식을 제출하면 "이 값은 유효하지 않습니다"라는 오류가 표시됩니다. id = 0, 옵션의 가치가있는 어떤 지역의 도시도 없기 때문에 이것이라고 생각합니다. 그렇다면 내가 필요로하는 것은이 유효성 검사를 비활성화/제거 /하는 것이지만 어떻게해야 하는지를 알지 못합니다.

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

을하지만 일을하지 않았다 ....

은 분명히 내가 대신 EntityType의 ChoiceType 이동 기관 또는 다른 그리 우아한 방법 사이의 관계를 제거 할 수 있습니다 :

나는 tryed있다 이것을 해결하십시오. 그러나 나는 방법이 있다는 것을 확신합니다, 이것은 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; 
} 

}이 도움이

희망.

답변

0

EntityType 필드 정의를 붙여 넣을 수 있습니까?

문제를 해결하는 데 도움이 될 수 있습니다.

EntityType 필드를 설정하려면 다음 옵션을 지정해야합니다

  • 클래스
  • CHOICE_LABEL을
(당신이 __toString 당신의 기업에 대한() 함수를하지 않는 한)
관련 문제