Closed yellowmamba closed 10 years ago
@yellowmamba see how select2 js widget is initialized - the form type passes on any options in configs
array. So, if you add
(assuming here you're useing Admingenerator)
fields:
mySelect2Field:
addFormOptions:
configs:
tags:
- red
- green
- blue
or in PHP formType:
<?php
// formType class
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('mySelect2Field', 'afe_select2_choice', array(
'multiple' => true,
'label' => 'Tags',
'configs' => array(
'tags' => array('red', 'green', 'blue')
)
);
}
Ah cool, so it's still using afe_select2_choice
type. Will try this out. Thanks.
My tags are actually entities, I tried the following but no luck:
$builder->add('tags', 'afe_select2_entity', array(
'class' => 'MyBundle:Tag',
'multiple' => true,
'configs' => array(
'tags' => array()
)
));
My Tag
table only have a name
field.
You have to somehow force select2 to only display the name but submit the id, as entity Type expects ids in POST
yes, the problem is when you add a new entry (tag), there's no <input>
tag attached with the value of id. I am currently just using collection type to add tags to an entity, but there's no way to validate against duplicate entires. So wondering if I could do it using select2.
First of all, if you want to add a new tag, then it has no ID (yet). It will have an id once its submitted to form and persisted to Database.
You just have to configure the widget to include the ID when you select an existing tag, plus you have to handle the case when a new tag (nonexisting) is submitted -> create an entity for it and persist it.
You'll have to read select2 documentation and maybe use some event hooks to make the submitted POST valid for Symfony2 Entity form (afe_select2_entity extends symfony2's entity type).
See select2 documentation http://ivaynberg.github.io/select2/#documentation and look for "Events". Probably you can use the "change" event.
Using data transformer might do the trick. Anyway, I will close this ticket as I wanted to know if this is supported by default. Appreciate your help.
Hi there,
Do you have a field type for select2 tagging support? http://ivaynberg.github.io/select2/#tags
Cheers!