Bellow I'm leaving the old answer, that worked for me until I had to implement more complex json_array. Here's how it should be done:
ItemType:
->add('extra_data', new ItemExtraDataType(), array(
'label' => false,
'required' => false,
))
ItemExtraDataType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field1', 'text', array(
'required' => false,
'label' => false,
))
->add('field2', 'text', array(
'required' => false,
'label' => false,
))
->add('field3', 'choice', array(
'choices' => MyChoices::getReadables(),
'empty_value' => 'empty',
'required' => false,
'label' => false,
))
;
}
public function getParent()
{
return 'form';
}
Twig template:
{{ form_row(item.extra_data) }}
So now everything works as expected.
Old answer
I believe a better solution is out there, but that's what I did and it worked at last.
Entity:
/**
* @param array $extra_data
* @return Entity
*/
public function setExtraData(array $extra_data)
{
$this->extraData = $extra_data;
return $this;
}
/**
* @return array
*/
public function getExtraData()
{
$rs = $this->extraData;
return $rs ?: array('field1' => '', 'field2' => '');
}
ItemType (notice the type
param):
->add('extra_data', 'collection', array(
'type' => 'text',
'label' => false,
'required' => false,
'allow_add' => false,
'by_reference' => false,
'allow_delete' => false,
))
Twig template:
{% for field, data in item.extra_data %}
{{ form_row(data, {'attr':{'placeholder':'extra_data.' ~ field}}) }}
{% endfor %}
Both fields are printed out and saved without any problem.