根据Country Select Symfony更新State,City下拉列表

时间:2015-10-17 18:40:48

标签: php symfony cascadingdropdown

我有这种关系。 国与国之间的一对多。 国家与城市之间的一对多。

在就业表上,我必须显示这些下拉列表,我想在选择一个国家时,只显示相关的状态。现在它显示了所有。

这是我的表单代码。

<?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';
    }
}

1 个答案:

答案 0 :(得分:1)

我说最简单的方法是通过JavaScript显示/隐藏选项。将国家,州和城市转储为JSON,并将其用作JS中决策的来源。