2017-02-14 11 views
0

フォームにはcustomerの選択フィールドがあり、query_builderがあります。選択した顧客には、invoicingTypeラジオボタンでデータを選択する必要があります。どのようにそれを行うことができますか?symfonyフォームイベント、ラジオボタンの値を変更

今はフォームのフィールドinvoicing_address'mapped' => false,ための変更ラベルのためのsymfonyのフォームイベントを使用し、正常に動作し、ラジオボタンの同じ決定は

enter image description here

enter image description here

アドレスは、ラジオボタンを変更して動作しませんまだ何も、なぜ?

class OutboundInvoiceForm extends AbstractType 
{ 
/** 
* @param FormBuilderInterface $builder 
* @param array $options 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add('customer', 'entity', array(
      'class' => Customer::class, 
      'property' => 'name', 
      'empty_value' => 'Choice Customer', 
      'query_builder' => function ($repository) { 
       /** @var CustomerRepository $repository */ 
       return $repository->getAllQuery(); 
      }, 
      'required' => true 
     )); 

     $formModifier = function (FormInterface $form, Customer $customer = null) { 
     if (null === $customer) { 
      $positions = '-'; 
      $label = $positions; 
      $invoicingType = null; 
     } else { 
      $positions = $customer->getInvoicingAddress() 
       ? $customer->getInvoicingAddress()->getFormattedAddress() 
       : '-'; 
      $label = $positions; 
      $invoicingType = $customer->getInvoicingType() 
       ? $customer->getInvoicingType() 
       : null; 
     } 

     $form 
      ->add('invoicingType', 'entity', array(
       'class' => InvoicingType::class, 
       'property' => 'name', 
       'data' => $invoicingType, 
       'query_builder' => function ($repository) { 
        /** @var InvoicingTypeRepository $repository */ 
        return $repository->getAllQuery(); 
       }, 
       'required' => false, 
       'expanded' => true, 
      )) 
      ->add('invoicing_address', TextType::class, [ 
       'mapped' => false, 
       'empty_data' => $positions, 
       'label' => $label 
      ]); 

    }; 

    $builder->addEventListener(
     FormEvents::PRE_SET_DATA, 
     function (FormEvent $event) use ($formModifier) { 
      $data = $event->getData(); 
      $formModifier($event->getForm(), $data->getCustomer()); 
     } 
    ); 

    $builder->get('customer')->addEventListener(
     FormEvents::POST_SUBMIT, 
     function (FormEvent $event) use ($formModifier) { 
      $customer = $event->getForm()->getData(); 
      $formModifier($event->getForm()->getParent(), $customer); 
     } 
    ); 
    $builder 
     ->add('message') 
     ->add('notes'); 
} 

/** 
* @param OptionsResolverInterface $resolver 
*/ 
public function setDefaultOptions(OptionsResolverInterface $resolver) 
{ 
    $resolver->setDefaults(array(
     'data_class' => OutboundInvoice::class, 
     'csrf_protection' => false, 
     'edit' => false, 
     'terms_edit_data' => 0 
    )); 
} 

/** 
* @return string 
*/ 
public function getName() 
{ 
    return 'economy_bundle_outbound_invoice'; 
} 

テンプレート、および要素によって

var $customer = $('#economy_bundle_outbound_invoice_customer'); 

$customer.change(function() { 
var $form = $(this).closest('form'); 
var data = {}; 
data[$sport.attr('name')] = $sport.val(); 
$.ajax({ 
    url : $form.attr('action'), 
    type: $form.attr('method'), 
    data : data, 
    success: function(html) { 
     $('#invoicing-address-container').replaceWith(
      $(html).find('#invoicing-address-container') 
     ); 
    } 
}); 
}); 

答えて

0

でHTMLを置き換え、この私のjsは(名前で選択するようにしてください、それが動作することを願っていJS

 <div id="invoicing-address-container" class="form-group"> 
     invoicing_address <br> 
     <label for="customer-address-id"> 
      {{ form_label(form.invoicing_address)}} 
     </label> 
     <div id="customer-address-container" style="display: none;"> 
      {{ form_widget(form.invoicing_address) }} 
     </div> 
     <br> 
     <label for="reversed-vat"> 
      {{ form_label(form.invoicingType, 'invoicing_type*')}} 
     </label> 
     {{ form_widget(form.invoicingType) }} 
    </div> 

に置き換えるブロックしかし、あなたは)長い長い形式の名前を持っている:):

$('input[type=radio][name="economy_bundle_outbound_invoice[invoicing_address]"]').change(function() { 
    // var selectedValue = this.value; 
} 

あなたはswitch、または単にif elseif elseif elseあなたの条件ができます。

Chromeコンソールから選択入力を試して、トリガーを呼び出して何が起こっているかを確認できます。

関連する問題