symfony form Collection type from array

I would like to generate a form where I can add descriptionAttributes which are stored with key/value as json in database. I’d like to have the possibility for a user to add/or delete the descriptions.
This is my FormType:

class HardwareKindType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('type', TextType::class, array(
                'label' => 'Art:',
                'required' => true,))
            ->add('descriptionTemplate', CollectionType::class,array(
                'entry_type' => FormType::class,
                'allow_add' => true,
                'allow_delete' => true,
                'constraints' => [
                    new Type('array')
                ]
            ));


    }/**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AppBundleHardwareHardwareKind'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'appbundle_hardwarekind';
    }
}

This is my entity

class HardwareKind
{
    /**
     * @var int
     *
     * @ORMColumn(name="id", type="integer")
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORMColumn(name="type", type="string", length=32)
     */
    private $type;

    /**
     * @var array
     *
     * @ORMColumn(name="description_template", type="json")
     */
    private $descriptionTemplate;


    /**
     * Get id.
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set type.
     *
     * @param string $type
     *
     * @return HardwareKind
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type.
     *
     * @return string
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set descriptionTemplate.
     *
     * @param array $descriptionTemplate
     *
     * @return HardwareKind
     */
    public function setDescriptionTemplate($descriptionTemplate)
    {
        $this->descriptionTemplate = $descriptionTemplate;

        return $this;
    }

    /**
     * Get descriptionTemplate.
     *
     * @return array
     */
    public function getDescriptionTemplate()
    {
        return $this->descriptionTemplate;
    }
}

But this displays only a field Art:, no description Template. When I change entry_type to TextType::class it show only one text field, but nothing else, nothing for adding another field.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *