Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedding dependent form #29

Open
Kamhal24 opened this issue Jun 17, 2024 · 1 comment
Open

Embedding dependent form #29

Kamhal24 opened this issue Jun 17, 2024 · 1 comment

Comments

@Kamhal24
Copy link

Kamhal24 commented Jun 17, 2024

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.

@toby-griffiths
Copy link

@Kamhal24 have you tried using the exampleType in your OrderDispatchConfigComponent.php ?

From my (slightly limited) experience I would expect that to work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants