2015-02-09 56 views
0

我有兩個選擇框一個用於國家,第二個用於城市,第二個用於選擇第一個。Ajax查詢在添加新帖子時起作用,但在更新實體時不起作用

我的代碼是作爲例子動態生成的文件中提交的表單添加一個新的職位時http://symfony.com/doc/current/cookbook/form/dynamic_form_modification.html#cookbook-form-events-submitted-data

所有的事情做工精細,而是試圖做更新發布時,該阿賈克斯查詢要顯示城市不起作用。

這是控制器

// newAction 
/** 
* @ParamConverter("agence", options={"mapping": {"agence_slug":"slug"}}) 
*/ 
public function newAction(Agence $agence, Request $request) 
{ 
    $em = $this->getDoctrine()->getManager(); 
    $travel = new Travel(); 
    $form = $this->createForm(new TravelType($agence), $travel); 

    if ($request->getMethod() == 'POST') 
    { 
     //.... 
    } 

    return $this->render('AppBundle:Dashboard/Travel:new.html.twig', 
    array(
     'form' => $form->createView() , 
     'agence' => $agence, 
     )); 
} 

//editAction 
/** 
* @ParamConverter("agence", options={"mapping": {"agence_slug":"slug"}}) 
* @ParamConverter("travel", options={"mapping": {"travel_id":"id"}}) 
*/ 
public function editAction(Travel $travel, Agence $agence, Request $request) 
{ 
    $em = $this->getDoctrine()->getManager(); 

    $form = $this->createForm(new TravelEditType($agence), $travel); 

    if ($request->getMethod() == 'POST'){ 
     //.... 
    } 

    return $this->render('AppBundle:Dashboard/Travel:edit.html.twig', 
    array(
     'form' => $form->createView() , 
     'travel' => $travel, 
     'agence' => $agence, 
     )); 
} 

travelType和Ti工作良好

namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 
use Symfony\Component\Form\FormEvents; 
use Symfony\Component\Form\FormEvent; 

//........ 
use Symfony\Component\Form\FormInterface; 
use AppBundle\Entity\Country; 

class TravelType extends AbstractType 
{ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    //.... 
    $formModifier = function (FormInterface $form, Country $country = null) { 
     $cities = null === $country ? array() : $country->getCities(); 

     $form->add('destination', 'entity', array(
      'class'  => 'AppBundle:CityWorld', 
      'choices'  => $cities, 
      'multiple' => false, 
      'expanded' => false, 
      'property' => 'name', 
      'label' => 'Destination', 
      'attr' => array('class' => 'col-xs-10 col-sm-10', 'placeholder' => 'Destination'), 
     )); 
    }; 

    $builder->addEventListener(
     FormEvents::PRE_SET_DATA, 
     function (FormEvent $event) use ($formModifier) { 
      // this would be your entity, i.e. SportMeetup 
      $data = $event->getData(); 

      $formModifier($event->getForm(), $data->getCountry()); 
     } 
    ); 

    $builder->get('country')->addEventListener(
     FormEvents::POST_SUBMIT, 
     function (FormEvent $event) use ($formModifier) { 
      // It's important here to fetch $event->getForm()->getData(), as 
      // $event->getData() will get you the client data (that is, the ID) 
      $country = $event->getForm()->getData(); 

      // since we've added the listener to the child, we'll have to pass on 
      // the parent to the callback functions! 
      $formModifier($event->getForm()->getParent(), $country); 
     } 
    ); 

    $builder->addEventListener(FormEvents::POST_SUBMIT, function ($event) { 
     $event->stopPropagation(); 
    }, 90000000000000); // Always set a higher priority than ValidationListener 

} 

public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => 'AppBundle\Entity\Travel' 
    )); 
} 

public function getName() 
{ 
    return null; 
} 
} 

這是TravelEditType

namespace AppBundle\Form; 

use Symfony\Component\Form\AbstractType; 
use Symfony\Component\Form\FormBuilderInterface; 
use Symfony\Component\OptionsResolver\OptionsResolverInterface; 
use Symfony\Component\Form\FormEvents; 
use Symfony\Component\Form\FormEvent; 
//........ 
use Symfony\Component\Form\FormInterface; 
use AppBundle\Entity\Country; 

class TravelEditType extends TravelType 
{ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    parent::buildForm($builder, $options) ; 

} 

public function getName() 
{ 
    return null; 
} 
} 

這是形式和JavaScript代碼

<form method="post" action="" class="form-horizontal" role="form" {{ form_enctype(form) }} > 
//............. 
</form> 

<script type="text/javascript"> 
var $county = $('#country'); 

$county.change(function() { 
    // ... retrieve the corresponding form. 
    var $form = $(this).closest('form'); 

    var data = {}; 
    data[$county.attr('name')] = $county.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 ... 
      $('#city').replaceWith(
        // ... with the returned one from the AJAX response. 
        $(html).find('#city') 
      ); 
      // Position field now displays the appropriate positions. 
     } 
    }); 
}); 

回答

0

嘗試沒有參數的URI:

dashboard_city_ajax: 
    path: /citiies/ajax 
    defaults: { _controller: AppBundle:TravelDashboard:ajaxCities } 

並與POST發送數據:

$.ajax({ 
     url: '{{ path('dashboard_city_ajax') }}', 
     type: 'POST', 
     data: { agence_slug: '{{ agenceSlug }}' }, 

可以在控制器接收它:

$request->request->get('bar', 'default value if bar does not exist'); 
+0

不,它不工作,不存在錯誤時顯示 – hous 2015-02-09 10:05:25

+0

對不起,我已經刪除了JSON響應,它沒有它的作品。我有更新我的問題 – hous 2015-02-09 16:37:17

0

問題在於您的路線匹配。有很多方法可以解決問題。嘗試只包括在你的阿賈克斯路線requirements匹配的路線或在$ .ajax功能使用另一條路線,例如

最簡單的方法(如果你不想重建你的控制器)只是重建你的路線,並把首先你的ajax路線如下:

dashboard_city_ajax: 
    path: /{ajax}/{agence_slug}/citiies 
    defaults: { _controller: AppBundle:TravelDashboard:ajaxCities } 
    requirements: 
     ajax: ajax 

$.ajax({ 
     url: '{{ path('dashboard_city_ajax', {'agence_slug': agence.slug, 'ajax': 'ajax' }) }}', 
     type: 'POST', 
     data: data, 

但是正確的方法恕我直言是從請求中獲取數據。例如

vplanning_ajax: 
    path: /ajax 
    defaults: { _controller: VplanningPageBundle:Page:Ajax } 


function getData(init) { 
    $.post(
     {{ path('vplanning_ajax') }}, 
     { 
      agence_slug: agence.slug, 
      yourdata2: 'yourdata2 
     } , 
     function (data) { 
      handleData(data); 
     } 
    ); 
} 

而在你的控制你只是做

$agence_slug = $this->request->request->get('agence_slug'); 
$yourdata2 = $this->request->request->get('yourdata2); 

...

+0

對不起,我已經刪除了Json響應,沒有它的工作。我有更新我的問題 – hous 2015-02-09 16:36:51