0
フォームにはcustomer
の選択フィールドがあり、query_builder
があります。選択した顧客には、invoicingType
ラジオボタンでデータを選択する必要があります。どのようにそれを行うことができますか?symfonyフォームイベント、ラジオボタンの値を変更
今はフォームのフィールドinvoicing_address
'mapped' => false,
ための変更ラベルのためのsymfonyのフォームイベントを使用し、正常に動作し、ラジオボタンの同じ決定は
アドレスは、ラジオボタンを変更して動作しませんまだ何も、なぜ?
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')
);
}
});
});