Open seb-jean opened 1 year ago
Hi there!
There are 2 steps to this:
1) You need to setup a few events in your form so that, when the brand
field changes, model
updates. This is 100% about the form component. It's well-documented, but tricky. You can see the MealPlannerForm.php
tab on the demo page - https://ux.symfony.com/live-component/demos/dependent-form-fields - or check the docs https://symfony.com/doc/current/form/dynamic_form_modification.html#form-events-submitted-data
2) Once you have this, you'll next want to create a AsLiveComponent
to render your form. There's really nothing special about this component, fortunately. You can look at the MealPlannerComponent.php
file on the demo - https://ux.symfony.com/live-component/demos/dependent-form-fields - it's quite simple. To make the edit form also work, you'll include a LiveProp
for Car
and use that to create the form - something like this:
use App\Form\MealPlannerForm;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormInterface;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
use Symfony\UX\LiveComponent\DefaultActionTrait;
#[AsLiveComponent('car_form_component')]
class MealPlannerComponent extends AbstractController
{
use ComponentWithFormTrait;
use DefaultActionTrait;
#[LiveProp(fieldName: 'formData')]
public ?Car $car = null;
protected function instantiateForm(): FormInterface
{
return $this->createForm(CarType::class, $this->car);
}
}
Then, instead of {{ include('car/_form.html.twig') }}
, you would render the component:
{{ component('car_form_component', {
form: form,
car: car.id ? car : null
}) }}
(that car.id ? car : null
is needed currently to avoid passing a non-persisted entity to the component - but it won't be needed in the future).
Let me know if this helps. I'd also love to know what we should change on the demo site / docs to be more useful for people :)
Cheers!
Basically, the idea is to just put 2 fields which are dependent fields but not written form_start
and form_end
.
I don't know if this can help you more for my problem.
Oh yes, that's totally allowed also, and it doesn't change much... but I think we don't have it documented. Everything would basically be the same (the component would likely look the same) but you would have the component responsible for rendering a bit less:
{# templates/car/_form.html.twig #}
{{ form_start(form) }}
{{ form_row(form.field1) }}
{{ form_row(form.field2) }}
{{ component('car_model_fields_component', {
form: form,
car: car.id ? car : null
}) }}
<button class="btn">{{ button_label|default('Save') }}</button>
{{ form_end(form) }}
Then the component template would basically have form_row(form.brand)
and form_row(form.model)
. So, though it's possible, there's not much "point" to having the component only render those 2 field specifically.
There might be a way we can add something to live components to make this simpler... or more automatic... or more "focused" on just the dependent fields (e.g. so that you could render normally, but a live component wraps itself around the 2 fields), but I'm not sure at the moment :)
Unfortunately, it doesn't work.
There might be a way we can add something to live components to make this simpler... or more automatic... or more "focused" on just the dependent fields (e.g. so that you could render normally, but a live component wraps itself around the 2 fields), but I'm not sure at the moment :)
It would be very interesting :)
I would like to do the exact same thing.
@weaverryan, How could we do this? 😄
There might be a way we can add something to live components to make this simpler... or more automatic... or more "focused" on just the dependent fields (e.g. so that you could render normally, but a live component wraps itself around the 2 fields), but I'm not sure at the moment :)
Thank you for this issue. There has not been a lot of activity here for a while. Has this been resolved?
Could I get a reply or should I close this?
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!
Hi,
I have a
Car
entity withyear
,color
,brand
andmodel
as fields. I haveCarController
generated withmake:crud
and histemplates
. I would like integrated demo Dependent Form Fields with an existing form but I do not know how. When I select 'Audi' for thebrand
, the options for themodel
are 'A1', 'A4', 'A5', 'S4'. When I select 'Citroen' for thebrand
, the options for themodel
are 'AX', 'Berlingo', 'Jumpy', 'Saxo'. etc.