Adding a form field with 2 different types to the Symfony 6 formbuilder

32 Views Asked by At
            ->add('useExtendedCustomer', CheckboxType::class, [
                'label' => 'Create New Customer ?',
                'mapped' => false, 
                'required' => false,
            ])

            ->add('customer', CustomerAutocompleteChoiceType::class, [
                'label' => 'Search Customer by Last Name',
                'resource' => 'sylius.customer',
                'choice_name' => 'lastName',
                'choice_value' => 'id',
                ])
                
             ->add('customer', CustomerType::class);

        ;

Here's my 'customer' field, which I'd like to display depending on whether the user clicks on the 'useExtendedCustomer' checkbox.

If the user clicks on the checkbox ('useExtendedCustomer') then I'd like to display this:

->add('customer', CustomerType::class);

and therefore delete this (as it's a default display in the first place) :

        ->add('customer', CustomerAutocompleteChoiceType::class, [

            label' => 'Search Customer by Last Name',
            'resource' => 'sylius.customer',
            'choice_name' => 'lastName',
            choice_value' => 'id',

            ])

twig :


        {{ form_start(form, {'action': path(configuration.vars.route.name|default(configuration.getRouteName('create')), configuration.vars.route.parameters|default({})), 'attr': {'class': 'ui loadable form', 'novalidate': 'novalidate'}}) }}


        <div class="ui two column stackable grid">

            <div class="column">
                <div class="ui segment">

                {{ form_row(form.customer) }}

                <div id="licence_customer" style="display: none;">
                    <div class="two fields">

                        {# {{ form_row(form.customer.firstName) }}
                        {{ form_row(form.customer.lastName) }} #}

                    </div>

                </div>

                {{ form_row(form.useExtendedCustomer)}}

                    {# <h4 class="ui dividing header">Customer details</h4>

                    <div class="two fields">

                        {{ form_row(form.customer.firstName) }}
                        {{ form_row(form.customer.lastName) }}

                    </div>

                    {{ form_row(form.customer.email) }}
                    {{ form_row(form.customer.group) }}

                </div>

                <div class="ui segment">

                    <h4 class="ui dividing header">Extra information</h4>

                    {{ form_row(form.customer.gender) }}
                    {{ form_row(form.customer.birthday) }}
                    {{ form_row(form.customer.phoneNumber) }}
                    {{ form_row(form.customer.subscribedToNewsletter) }}

                </div>

            </div> #}

For the moment, as I can't display the same 'customer' field twice, there's a 2nd commented form starting from :

{{ form_row(form.customer.firstName) }}

Which I'd like to display according to the click on the checkbox.

So here's the logic:

It's to display by default with this:

        ->add('customer', CustomerAutocompleteChoiceType::class, [

            label' => 'Search Customer by Last Name',
            'resource' => 'sylius.customer',
            'choice_name' => 'lastName',
            choice_value' => 'id',

            ])

this in the twig :

{{ form_row(form.customer) }}

And if the checkbox is true, then the lines above, then add this:

    ->add('customer', CustomerAutocompleteChoiceType::class, [

        label' => 'Search Customer by Last Name',
        resource' => 'sylius.customer',
        choice_name' => 'lastName',
        choice_value' => 'id',
        ])

and display the comment section in the twig.

Here's a problem I can't see how to solve dynamically?

0

There are 0 best solutions below