I want to add a dynamic field in a form. However, when clicking on the concerned field, nothing happens. Indeed, when clicking on the "Can publish a podcast" field, another field should normally appear.
The project configuration uses Webpack Encore with Symfony 7.
Here are the main codes: The live component :
<?php
namespace App\Twig\Components;
use App\Entity\User;
use App\Form\UserType;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\UX\LiveComponent\DefaultActionTrait;
use Symfony\UX\LiveComponent\Attribute\LiveAction;
use Symfony\UX\LiveComponent\ComponentWithFormTrait;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
#[AsLiveComponent]
final class Authorisation extends AbstractController
{
use ComponentWithFormTrait;
use DefaultActionTrait;
protected function instantiateForm(): FormInterface
{
$user = new User();
// Création du formulaire dynamique pour ajout des champs dynamique dans la page de traitement de la demande
return $this->createForm(UserType::class, $user);
}
}
The form type
<?php
namespace App\Form;
namespace App\Form;
use App\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfonycasts\DynamicForms\DependentField;
use Symfony\Component\Form\FormBuilderInterface;
use Symfonycasts\DynamicForms\DynamicFormBuilder;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\PasswordType;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder = new DynamicFormBuilder($builder); // Rendre le builder dynamique et interactif
$builder
//.....
->add('authorisations', ChoiceType::class, [
'label' => 'Les autorisations accordées:',
'mapped' => false,
'choices' => [
'Can write an article' => 1,
'Can publish a podcast' => 2
],
'label_attr' => [
'id' => 'labelAuthorisations',
],
// Rendre le champs "authorisations" de type checkbox
'expanded' => true,
'multiple' => true
])
// Rendre le champs "authorisations" dynamique et interactif
->addDependent('podcasts', 'authorisations', function (DependentField $field, ?array $authorisations) {
if ($authorisations !== null && in_array(2, $authorisations)) {
$field->add(TextType::class, [
'mapped' => false,
"label" => "Sous-categorie"
]);
}
});
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => User::class,
]);
}
}
The component twig (the view)
<div{{attributes}}>
<!-- component html -->
{{ form_start(form)}}
{{ form_row(form.authorisations)}}
{% if form.podcasts is defined %}
{{ form_row(form.podcasts) }}
<p>hello</p>
{% endif %}
<div id="inputsSubmit">
<input type="submit" value="Refuser" class="secondaryButton" data-live-action-param="save" data-action="live#action:prevent">
<input type="submit" value="Accepter" class="primaryButton">
{{ form_end(form, {render_rest: false})}}</div>
The component import
<twig:Authorisation/>
Nothing appears when I tried With Webpack Encore, but without Webpack it works well.