首页 文章

Symfony形式和依赖选择

提问于
浏览
0

我正在使用symfony 2.8和php 7,并且确实遇到了使用地理数据填写表单的问题(国家/州/省/市/邮编) .

我只需要5个选择框,第一个已经填充,其他人在我改变选择之前重新加载 . 例如,如果我更改国家/地区,则州选择将填充新值 .

我今天读了很多例子,但仍然没有得到如何运行它 .

这是我的代码:

如果有5个返回数组的方法,你可以通过它的名字来理解它们返回的内容:

private function getCountryList()
private function getStatesList( $idCountry )
private function getProvinceList( $idState )
private function getCityList( $idProvince )
private function getZipList( $idCity )

这是我的表格:

$locationForm = $builder->create('location', FormType::class, array('data_class' => 'M3\CoreBundle\Entity\Location',
        'by_reference' => true));

$locationForm->add('GeoCountryId', ChoiceType::class, array(
                'label' => 'Country',
                'choices' => $this->getCountryList(),
            ));
$locationForm->add('GeoStateId', ChoiceType::class, array('label' => 'State', 'choices' => array()));
$locationForm->add('GeoProvinceId', ChoiceType::class, array('label' => 'Province', 'choices' => array()));
$locationForm->add('GeoCityId', ChoiceType::class, array('label' => 'City', 'choices' => array()));
$locationForm->add('GeoZipCodeId', ChoiceType::class, array('label' => 'Zip Code', 'choices' => array()));

我试图添加一些像这样的事件

$locationForm->addEventListener(
            FormEvents::PRE_SET_DATA,
            function (FormEvent $event) {
                $data = $event->getData();
                $form = $event->getForm();
                $country = $form->get('GeoCountryId')->getData();
            //here i tried a lot of things               
            $form->get('GeoStateId')->setData($this->getStatesList($country));
            }
        );

我尝试了很多东西,却一无所获 . 我读了这本手册http://symfony.com/doc/2.8/form/dynamic_form_modification.html#form-events-user-data但也没什么用 . 你能帮助我吗?

也许我只需要添加javascript事件“on change”进行选择并手动加载所有数据?

1 回答

  • 0

    您必须使用PHP和JS的组合 . 您可以使用下面几段文档的示例:http://symfony.com/doc/2.8/form/dynamic_form_modification.html#dynamic-generation-for-submitted-forms

    {# app/Resources/views/Meetup/create.html.twig #}
    {{ form_start(form) }}
        {{ form_row(form.sport) }}    {# <select id="meetup_sport" ... #}
        {{ form_row(form.position) }} {# <select id="meetup_position" ... #}
        {# ... #}
    {{ form_end(form) }}
    
    <script>
    var $sport = $('#meetup_sport');
    // When sport gets selected ...
    $sport.change(function() {
      // ... retrieve the corresponding form.
      var $form = $(this).closest('form');
      // Simulate form data, but only include the selected sport value.
      var data = {};
      data[$sport.attr('name')] = $sport.val();
      // Submit data via AJAX to the form's action path.
      $.ajax({
        url : $form.attr('action'),
        type: $form.attr('method'),
        data : data,
        success: function(html) {
          // Replace current position field ...
          $('#meetup_position').replaceWith(
            // ... with the returned one from the AJAX response.
            $(html).find('#meetup_position')
          );
          // Position field now displays the appropriate positions.
        }
      });
    });
    </script>
    

相关问题