【问题标题】:Update State, City dropdown base on Country Select Symfony根据 Country Select Symfony 更新州、城市下拉列表
【发布时间】:2015-10-17 18:40:48
【问题描述】:

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

在就业表格上,我必须显示这些下拉列表,我希望当我选择一个国家时,只显示它的相关州。现在显示全部了。

这是我的表单代码。

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

【问题讨论】:

  • 请把这个缩小。您可以在两个国家/地区展示相同的问题,每个国家/地区有两个州,不包括城市(这将是相同的想法)。

标签: php symfony cascadingdropdown


【解决方案1】:

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

【讨论】:

  • 这是实现它的好方法
猜你喜欢
  • 2020-02-18
  • 2013-06-28
  • 2020-08-27
  • 2011-08-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-20
相关资源
最近更新 更多