0
votes

I have form for OutboundInvoice entity, in form I have customer choice field, with query_builder and when select customer I need change select data in radio button in field invoicingType. How can how to do it ?

Now I using symfony form event for change label for field invoicing_address 'mapped' => false, in form and work fine, the same decision for radio button not work

enter image description here

enter image description here

address changed, radio button still none, why ?

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

template, and block which replace in 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>

this my js where replaced html with by element

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

1 Answers

0
votes

Try to select with name, and hope that it works (but you've got a long-long form name :)):

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

Then you can switch or simply if elseif elseif else your conditions.

You can try selection inputs from Chrome console and call triggers and see what happening.