0

Is there any "easy" way to handle collection type on entity creation form?

What I've got:

namespace AppBundle\Entity;
// ...
class Book
{
    // ...
    /**
     * @ORM\Column(type="array", nullable=true)
     */
    private $otherTitles;
    // ...
}

In the form builder:

// ...
->add('bookOtherTitles', 'collection', array(
      'label' => "Other Title(s)",
      'required'=>false,
      'allow_add'=>true
     ))
// ...

What I want - in the Perfect World - is that the field would display one text text input (ok, a vars.prototype can do that one) and a little "+" sign next to it, allowing me to add more fields, but probably I will have to do it myself in JS. My question is, is my approach the best one here (with array entity field and collection field in form)?

Bonus question: how to handle multiple inputs - I assume i need to trick the form into giving them names with []?

Maybe I started in a wrong way? I didn't create a separate entity for that field, because I know every "other title" will be just string and will not be assigned to any other Book. Maybe that was a mistake? Should I create an entity with only one field and use OneToMany in Book entity?

1 Answer 1

1

Yes, you can add by twig extension in prototype data-prototype="{{...|e}}{{your_twig_return}}". Please read this: http://symfony.com/doc/current/cookbook/templating/twig_extension.html Dont forget {{your_twig_return}} should return htmlentities. After that style it by css and then viola.

Anyway, I think the best way is use JS instead of using twig extension. Its very simple btw.

how to handle multiple inputs? dont worry about that, symfony already handle that. Please read this : http://symfony.com/doc/current/cookbook/form/form_collections.html careful.

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.