如何在符号中构建嵌套形式

发布于 2025-01-27 07:28:42 字数 3351 浏览 7 评论 0 原文

我正在使用EasyAdmin为实体创建控制器( process ),该控制器与另一个实体有关( e节)(一对多关系)与另一个实体有关系(子段)(也是一对一的)。

在“新”过程的“新”形式中,我有一个字段,该字段是easyAdmin集合以添加部分的字段,但是由于这些部分具有小节,我需要在该小节的各节中添加另一个平等字段...具有与相同的行为小节。 easyAdmin类型的字段(单击“添加项目”时,为其他实体显示了一个迷你表单)。

主要形式是为了创建一个过程,“部分”字段是为了向该过程添加部分,并且在部分中,我需要一个像EasyAdmin中的一个按钮来为该部分创建更多子部分。

这是过程的主要形式:


”在此处输入图像说明“

,这就是收集类型字段的样子,用于创建各节的字段:


“


这是过程控制器:一个称为“ secciontype ”的自定义字段)

    namespace App\Controller\Admin;

    use App\Entity\Proceso;
    use App\Form\Type\SeccionType;
    use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
    use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
    use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
    use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
    use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;

    class ProcesoCrudController extends AbstractCrudController
    {
       public static function getEntityFqcn(): string
       {
           return Proceso::class;
       }

       public function configureFields(string $pageName): iterable
       {
           yield TextField::new('nombre');
           yield TextEditorField::new('descripcion');
           yield CollectionField::new('secciones')        // <--- This is Sections field with the custom Symfony formType
                ->setEntryType(SeccionType::class)
                ->setFormTypeOptions(['block_prefix' => 'secciones_form',])
                ->addCssClass('actividadesFld');
           yield BooleanField::new('estado')->renderAsSwitch(false);
       }
    }

,这是自定义字段的类 scciontype

namespace App\Form\Type;

use App\Entity\Seccion;
use App\Entity\Proceso;
use App\Entity\Subseccion;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormTypeInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use Symfony\Component\Validator\Constraints\NotNull;

class SeccionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name')
            ->add('description')
            ->add('order')
            ->add('tag')
            // ->add('proccess')
            ->add('subSection', CollectionType::class, ['entry_type' => ..?]);
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Seccion::class,
        ]);
    }
}

任何帮助或建议,我都会非常感谢

I'm using EasyAdmin to create a controller for an entity (Processes), which is related to another entity (Sections) (a one-to-many relationship) that has a relationship to another entity (Sub-Sections) (also one-to-many).

In the "new" form of Processes I have a field that is of type EasyAdmin Collection to add sections, but since those sections have subsections, I need to add another equal field within Sections for the subsections... that has the same behavior as the subsections. fields of type EasyAdmin Collection (which when clicking on "Add item", shows a mini form for that other entity).

The main form is for creating a Process, the "sections" field is for adding sections to that Process, and within sections, is where I need another button like the one in EasyAdmin to create more sub-sections for that section.

This is the main form of Processes:


enter image description here

And this is what the Collection type field looks like with the fields to create Sections:


enter image description here


This is the process controller: (I created a custom field for sections called "SeccionType")

    namespace App\Controller\Admin;

    use App\Entity\Proceso;
    use App\Form\Type\SeccionType;
    use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
    use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
    use EasyCorp\Bundle\EasyAdminBundle\Field\TextField;
    use EasyCorp\Bundle\EasyAdminBundle\Field\TextEditorField;
    use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;

    class ProcesoCrudController extends AbstractCrudController
    {
       public static function getEntityFqcn(): string
       {
           return Proceso::class;
       }

       public function configureFields(string $pageName): iterable
       {
           yield TextField::new('nombre');
           yield TextEditorField::new('descripcion');
           yield CollectionField::new('secciones')        // <--- This is Sections field with the custom Symfony formType
                ->setEntryType(SeccionType::class)
                ->setFormTypeOptions(['block_prefix' => 'secciones_form',])
                ->addCssClass('actividadesFld');
           yield BooleanField::new('estado')->renderAsSwitch(false);
       }
    }

And this is the class of the custom field SeccionType

namespace App\Form\Type;

use App\Entity\Seccion;
use App\Entity\Proceso;
use App\Entity\Subseccion;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\FormTypeInterface;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use Symfony\Component\Validator\Constraints\NotNull;

class SeccionType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options): void
    {
        $builder
            ->add('name')
            ->add('description')
            ->add('order')
            ->add('tag')
            // ->add('proccess')
            ->add('subSection', CollectionType::class, ['entry_type' => ..?]);
        ;
    }

    public function configureOptions(OptionsResolver $resolver): void
    {
        $resolver->setDefaults([
            'data_class' => Seccion::class,
        ]);
    }
}

Any help or suggestion, I will be infinitely grateful

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

如果没结果 2025-02-03 07:28:42

it seems you did not add the allow-add => true option to your CollectionType in SeccionType.php, as well as prototype => true. You should also probably create a SubSeccionType to give in the entry-type option. That should be a good start to move along.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文