0

Is it possible to use an Embedded form without using a separated class for it? The reason is I've got a lot of form classes already, which most of the time contain a single field, so I wonder if it is possible to define embedded forms inline.

So normally we have something like this:

public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('name')
        ->add('email')
        ->add('phone')
        ->add('key', new KeyType())            
    ;
}

The documentation says that I have to create a class for the key field, KeyType for example, where I would set up the form builder for the embedded form. But I would like to, instead of creating the class KeyType, define the fields inline, in the same class. How can I do that?

1 Answer 1

1

Yes this is possible.

public function buildForm(FormBuilder $builder, array $options)
{
    $builder
        ->add('name')
        ->add('email')
        ->add('phone')
        ->add(
            $builder->create('key')
                ->add('someField', 'text')
                ->add('otherField', 'checkbox')
        )       
    ;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.