Symfony表单事件,单选按钮上的更改值

时间:2017-02-14 22:09:41

标签: php symfony

我有OutboundInvoice实体的表单,表单我有customer选项字段,query_builder,当选择客户我需要更改时,在字段{{1}中的单选按钮中选择数据}。怎么办呢?

现在我在表单中使用symfony表单事件作为字段invoicingType invoicing_address的更改标签并且工作正常,单选按钮的相同决定不起作用

enter image description here

enter image description here

地址改了,单选按钮还没有,为什么?

'mapped' => false,

模板和在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';
}

这是我的js用元素替换html

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

1 个答案:

答案 0 :(得分:0)

尝试选择名称,并希望它有效(但你有一个很长的表格名称:)):

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

然后您可以switch或只是if elseif elseif else您的条件。

您可以尝试从Chrome控制台和呼叫触发器中选择输入,看看发生了什么。

相关问题