1

let's say I have Post entity that has $title field (array type) and I want to allow the user to write the title of the post in multiple-language

/**
 * Post
 *
 * @ORM\Table(name="posts")
 * @ORM\Entity
 */
class Post
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @var array
     *
     * @ORM\Column(name="title", type="array", nullable=true)
     */
    protected $title;
}

How can I create form type that generate those fields when user want to submit a new post?

<input type="text" name="title[en]" />
<input type="text" name="title[fr]" />
1
  • I really don't think this is the good way to implement a translation behavior with doctrine. Why don't use DoctrineExtension translatable ? Commented Nov 21, 2013 at 12:49

1 Answer 1

1

You have to create a sub type for your form :

class TitleType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
            $builder
                    ->add('en')
                    ->add('fr');
    }

    public function getName()
    {
            return 'form_type';
    }
}

Then you can add this sub type in your main type :

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
            $builder
                    ->add('title', new TitleType());
    }
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.