Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

Symfony 4 | How to define new fields in a collectionType subform from the parent form

To start I use symfony 4 and no entities.

Here is my problem, in my api I want to define new fields in a collectionType subform from the parent form.

I have several specific forms that have many similar fields, so I created a common form called by the specific forms firstSpec and secondSpec.

The problem occurs when I try to add specific fields to the collectionType of each specific form.

I'm trying to set them using ->getAttribut('prototype')->add('field') but if the fields are added correctly it doesn't work, the fields just ignored in the submission, I guess it's because the prototype is used for rendering.

Hope a solution exists using the constructor.

Thank you in advance for your time and answers.

Here is my code.

class CommonType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        return $builder
            ->add('form_1', CollectionType::class, [
                'entry_type' => ChildForm::class,
                'allow_add' => true
            ])
            ->add('form_2', AutherForm::class);

    }
}
class ChildForm extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        return $builder
            ->add('field_1', TextType::class)
            ->add('field_2', TextType::class)
            ->add('field_3', TextType::class)
            ->add('field_4', TextType::class);
    }

}
class FirstSpecType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        return $builder
            ->add('first_spec', CommonType::class)
            ->get('first_spec')
            ->getAttribute('prototype')
            ->add('field_7', TextType::class)
            ->add('field_8', TextType::class)
        ;
    }

}
class SecondSpecType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        return $builder
            ->add('second_spec', CommonType::class)
            ->get('second_spec')
            ->getAttribute('prototype')
            ->add('field_5', TextType::class)
            ->add('field_6', TextType::class)
        ;
    }

}
question from:https://stackoverflow.com/questions/65859256/symfony-4-how-to-define-new-fields-in-a-collectiontype-subform-from-the-parent

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...