ux icon indicating copy to clipboard operation
ux copied to clipboard

[Live Component][Dependent Form] Embedding dependent form

Open Kamhal24 opened this issue 1 year ago • 2 comments

Hello, I'm struggling with UX dependent form component. I could not find anything in docs, but is it even possible to use dependent form and use that in other form that is not a component? So far it seems, that I need to have whole form as a component to have dependent form working. But maybe I'm missing something.

For example

OrderDispatchType.php
...
public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder = new DynamicFormBuilder($builder);
    $builder
        ->add('orderDispatchFrequency', EnumType::class, [
            'class' => OrderDispatchFrequency::class,
            'choice_label' => 'label',
        ])
        ->addDependent(
            'orderDispatchDay',
            'orderDispatchFrequency',
            static function (DependentField $field, ?OrderDispatchFrequency $dispatchFrequency): void {
                if (OrderDispatchFrequency::Weekly === $dispatchFrequency) {
                    $field->add(EnumType::class, [
                        'class' => OrderDispatchDay::class,
                        'choice_label' => 'label',
                        'required' => false,
                    ]);
                }
            }
        )
        ->addDependent(
            'oneTimeOrderDispatchDate',
            'orderDispatchFrequency',
            static function (DependentField $field, ?OrderDispatchFrequency $dispatchFrequency): void {
                if (OrderDispatchFrequency::OneTime === $dispatchFrequency) {
                    $field->add(DatePickerType::class);
                }
            }
        )
        ->add('orderDispatchTime', TimeType::class)
    ;
}
{# dispatchConfig.html.twig #}
<fieldset {{ attributes }}>
    {{ form_row(form.orderDispatchFrequency) }}

    <div>
        {% if orderDispatchConfig.oneTimeOrderDispatchDate is defined %}
            {{ form_row(form.oneTimeOrderDispatchDate) }}
        {% endif %}
    </div>

    <div>
        {% if orderDispatchConfig.orderDispatchDay is defined %}
            {{ form_row(form.orderDispatchDay) }}
        {% endif %}
    </div>

    {{ form_row(form.orderDispatchTime) }}
</fieldset>
OrderDispatchConfigComponent.php
...
#[AsLiveComponent(
    name: 'admOrderingFoodDispatchConfigType',
    template: 'components/twig/dispatchConfig.html.twig',
)]
final class OrderDispatchConfigComponent extends AbstractController
{
    use ComponentWithFormTrait;
    use DefaultActionTrait;

    protected function instantiateForm(): FormInterface
    {
        return $this->createForm(OrderDispatchType::class);
    }
}

and then use it in other form as

exampleType.php
...
public function buildForm(FormBuilderInterface $builder, array $options): void
{
    $builder
        ->add('name', TextType::class)
        ->add('orderDispatchConfig', OrderDispatchType::class)
    ;
}
{# example.twig.html #}
...
{{ form(exampleForm) }}

Any help (or simple info, that this is not supported yet) would be highly appreciated. Thank you in advance.

Kamhal24 avatar Jun 13 '24 05:06 Kamhal24

Probably more a question for the DynamicForms repository: https://github.com/SymfonyCasts/dynamic-forms

.. i'm not sure here and prefer not give you a wrong answer 🤷‍♂️

smnandre avatar Jun 14 '24 10:06 smnandre

Thank you, I'll try to ask them. So far it looks like this is not possible (yet). If we found out solution, I'll post it here

Edit: issue on dynamic-forms repo https://github.com/SymfonyCasts/dynamic-forms/issues/29

Kamhal24 avatar Jun 17 '24 06:06 Kamhal24

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

carsonbot avatar Dec 18 '24 12:12 carsonbot

Friendly reminder that this issue exists. If I don't hear anything I'll close this.

carsonbot avatar Jan 01 '25 12:01 carsonbot

Hey,

I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen!

carsonbot avatar Jan 15 '25 12:01 carsonbot

Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?

carsonbot avatar Jul 16 '25 12:07 carsonbot