2017-02-14 2 views
0

선택 필드가 query_builderOutboundInvoice 엔티티 용 양식이 있고 선택 고객은 필드 invoicingType의 라디오 버튼으로 데이터를 변경해야합니다. 그것을 어떻게 할 수 있습니까? 심포니 양식 이벤트, 라디오 버튼의 값을 변경

가 지금은 양식 필드 invoicing_address 'mapped' => false,에 대한 변경 레이블 심포니 형태의 이벤트를 사용하고 잘 작동, 라디오 버튼 같은 결정은하지

enter image description here 주소가 변경

enter image description here

, 라디오 버튼을 작동 여전히 아무것도, 왜? 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> 

요소에 의해

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 교체

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

템플릿, 및 블록 이름을 선택하고 (그것이 작동하는 희망합니다 긴 형식의 이름을 가지고 있습니다 :)) :

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

다음 switch 또는 간단하게 if elseif elseif else 수 조건.

Chrome 콘솔에서 선택 입력을 시도하고 트리거를 호출하여 어떤 일이 일어나는지 볼 수 있습니다.

관련 문제