0
希望有人幫助我,我無法繞過這個。帶附加選項的實體字段
我正在使用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;
}
}
希望這有助於。