Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
527 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 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...