Reputation: 2108
I have collection field type like this:
$defaultData = array('attendances' => array('5', '4'));
$form = $this->createFormBuilder($defaultData)
->add('choice', 'text')
->add('email', 'email')
->add('message', 'textarea')
->add('attendances', 'collection', array(
'type' => 'choice',
'options' => array(
'choices' => array('1' => 'حاضر', '0' => 'غایب'),
),
))
->add('send', 'submit')
->getForm();
what I get is this:
See what labels of choices are(0 and 1) and also their input name attribute, what I want is to change label and input name attr so how can i change $defaultData for this?
Upvotes: 0
Views: 1523
Reputation: 6932
I'd suggest that if you're going to create a select, you use the 'choice'
type directly:
->add('attendances', 'choice', array(
'choices' => array('1' => 'حاضر', '0' => 'غایب'),))
Hope it helps.
UPDATE
You can use a predefined array to populate the choices. You can also set the default using the data attribute.
$defaultData = array('attendances' => array('5' => 'One option', '4' => 'Another option'));
...
->add('attendances', 'choice', array(
'choices' => $defaultData["attendances"],
'data' => '5',))// just in case you want a preselected choice, in this case, key 5 will appear selected by default.
...
Upvotes: 0
Reputation: 363
Ok you can do this with data
attribute as array, you can read more about it here
Example with your code:
$defaultData = array('attendances' =>
array('key' => '5', 'key2' => '4')
);
$form = $this->createFormBuilder($defaultData)
->add('choice', 'text')
->add('email', 'email')
->add('message', 'textarea')
->add('attendances', 'collection', array(
'type' => 'choice',
'options' => array(
'choices' => array('1' => 'حاضر', '0' => 'غایب'),
),
'data' => $defaultData['attendances']
))
->add('send', 'submit')
->getForm();
result:
name="form[attendances][key]"
name="form[attendances][key2]"
and label also key
and key2
Upvotes: 2