我有這種關聯。國與國之間一對多。國家之間對一對多的城市。更新狀態,城市下拉根據國家選擇Symfony
就業形式,我必須顯示這些下拉列表,我想當我選擇一個國家,只有它相關的狀態顯示。現在它顯示所有。
這是我的表單代碼。
<?php
namespace PNC\UsersBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\FormBuilder;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\Event\DataEvent;
class EmploymentsType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder ->add('designation', 'text', array(
'required' => true,
'label' => 'Designation ',
'attr' => array(
'required' => true,
'placeholder' => 'Designation Name',
'invalid_message' => 'You entered an invalid designation',
'class' => 'form-control'
)))
->add('organization', 'text', array(
'label' => 'Organization',
'attr' => array(
'required' => true,
'placeholder' => 'Organization Name',
'invalid_message' => 'You entered an invalid organization',
'class' => 'form-control'
)))
->add('country', 'entity', array(
'label' => ucfirst('country'),
'class' => 'PNCGeneralBundle:Country',
'property'=>'name',
//'property_path'=>false, //Country is not directly related to City
'attr'=> array(
'required' => true,
'invalid_message' => 'You selected an invalid Industry',
'class' => 'form-control'
)
))
->add('state', 'entity', array(
'label' => ucfirst('state'),
'class' => 'PNC\GeneralBundle\Entity\State',
'attr'=> array(
'required' => true,
'invalid_message' => 'You selected an invalid Industry',
'class' => 'form-control'
)
))
->add('city', 'entity', array(
'label' => ucfirst('city'),
'class' => 'PNC\GeneralBundle\Entity\City',
'attr'=> array(
'required' => true,
'invalid_message' => 'You selected an invalid Industry',
'class' => 'form-control'
)
))
->add('save', 'submit', array(
'attr' => array(
'label' => ucfirst('save'),
'class' => 'btn btn-success'
)
));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'PNC\UsersBundle\Entity\Employments',
));
}
public function getName()
{
return 'pncusers_bundle_employments_type';
}
}
請減小尺寸。你可以向兩個國家和兩個國家展示同樣的問題,而不包括城市(這將是相同的想法)。 – Teepeemm