symfony2 FromBuilder multiple checkboxes from entity instead of using "choice" - symfony

How can I get multiple Cehckboxes instead a choice field in Symfony? Actually, I'm using:
->add('usergroups', 'entity', array('class' => 'PrUserBundle:Group','property' => 'name','required' => true))
This will output a select-Field..
In this case, it would be better to output checkboxes as it is easier to handle for the user.
http://symfony.com/doc/current/reference/forms/types/checkbox.html at, there is nothing helpful about multiple select....
Do I have to build an array and place it by myself?

Use the expanded option for choice and entity fields.
->add('usergroups', 'entity', array('class' => 'PrUserBundle:Group','property' => 'name','required' => true, 'expanded' => true,))

Related

Form of CollectionType implementing EntityType in ManyToMany relation: Duplicate record is added to database table when 'multiple' => false

I have 2 classes with many to many relationship: Foo (owning side) and Bar (inverse side). My form is CollectionType (of Foo) implementing EntityType (of Bar) (add Bar into Foo). This is some code for my form
FooType:
->add('Bars', CollectionType::class, array('entry_type' => BarType::class, 'allow_add' => true, 'allow_delete' => true,
'by_reference' => false))
BarType
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('text', EntityType::class,
array(
'class' => Bar::class,
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('bar')
->orderBy('bar.text', 'ASC');
},
'choice_label' => 'text',
'by_reference' => false,
)
)
;
}
if i set 'multiple' => true, there is no problem
if i set 'multiple' => false (dont ask me why, this is a requirement from work: add a plus and minus button for the form. When plus button is click, the options from Bar.text are shown, and user can select select only one from those options. When minus button is clicked, undo the plus action), relationships are established but a duplicate record of selected option is added to Bar table.
I would like to ask:
Why does it happen? Is that the way symfony supposed to works?
Is there any fix/workaround for it?
According to this github disccussion of Symfony, you are supposed to use 'multiple' => true to make it work
I am using Symfony 4.4, doctrine bundle 2.6.3
Edit:
I would like to clarify this: my goal is to select multible options loaded from Bar->$text. But instead of selecting multiple options in one run (by setting multiple true), i would like to archieve it by adding many times (with a plus button), and each time only one option can be selected.
The reason I am doing this is that i am migrating from many to one to many to many, and I do not want to change the current UI (select one at a time) to multiple select.

Symfony Form: translation of attr

I'm trying to translate help messages that I created on my form using attr array but I can't translate it. Does anybody know how to do it?
This is an example:
->add('facebook_sharing_title', null,
array(
'label' => "establishment.edit.facebooksharingtitle",
'required' => false,
'attr' => array('help' => '70 caractères max. conseillé')
)
)
As you can see, the translation o the label is ok. However, if I do the same for help it doesn't work. I can't find anything about it in the symfony site. I know how to do it on twig but I would like to do it on the form type.

ManyToMany nullable not allowed

I have a scenario like this:
tracks ManyToMany instruments
I create the form in this way:
$builder
->add('instruments', 'entity_hidden', array(
'attr' => array('class' => 'one_req'),
'label' => 'form.instrument',
'translation_domain' => 'label',
'required' => false,
'class' => 'Acme\DemoBundle\Entity\Instrument'
))
"hidden_entity" is given by a custom transformer that you can find here: gist
although I believe that is not the problem.
The problem is that the user may not even choose any instrument (such asamong other specified with "required" => false).
When I go to save my returns this error:
Neither the property 'instruments' nor one of the methods 'setInstruments()',
'__set()' or '__call()' exist and have public access in
class 'Acme\DemoBundle\Entity\SoundtrackVersion'.
I do not understand why ..
Where am I doing wrong?
It sounds very obvious, but error can not lie. Check that Acme\DemoBundle\Entity\SoundtrackVersion entity has property instruments and methods setInstruments() and getInstruments().

How to validate two instances of the same entity?

Use case
I am learning Symfony2 and am creating a Table Tennis tracking app to learn the framework. I have configured my entities as follows.
Player 1..n Result n..1 Match
On my form I'd like to validate that the scores for a match are correct.
Implementation
Match has an ArrayCollection() of results.
My MatchType and ResultType forms contain the following.
// Form\MatchType
$builder->add('matchType', 'entity', array(
'class' => 'PingPongMatchesBundle:MatchType',
'property' => 'name',
)
)
->add('results', 'collection', array(
'type' => new ResultType(),
'allow_add' => true,
'by_reference' => false,
)
)
->add('notes');
// Form\ResultType
$builder->add('player', 'entity', array(
'class' => 'PingPongPlayerBundle:Player',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('p')
->orderBy('p.firstName', 'ASC');
},
))
->add('score');
Issue
I need to be able to validate the scores. However I'm not sure how to approach this type of validation as I need to compare two instances of my Result#score in order to know if they are valid or not.
Is anyone able to suggest a method or approach that I could use in order to be able to compare Result#score between the two different instances? Can I validate the ArrayCollection in the Match entity for example?
You could creat a custom validator constraint on Match entity.
http://symfony.com/doc/2.0/cookbook/validation/custom_constraint.html
Take a look in Callback constraint:
http://symfony.com/doc/2.1/reference/constraints/Callback.html

How to order related propel model in symfony2 form

I am using symfony2 form along with propel. I am having trouble on how to order the related entity from propel's model.
The abstracted code below, is taken from http://www.propelorm.org/cookbook/symfony2/mastering-symfony2-forms-with-propel.html, which show how to relate them together. I am wondering how do I order them.
$builder->add('author', 'model', array(
'class' => 'Acme\LibraryBundle\Model\Author',
));
For Example, If in my author table there are columns name, age,.... How should make it to order by name.
$builder->add('author', 'model', array(
'class' => 'Acme\LibraryBundle\Model\Author',
'query' => \Acme\LibraryBundle\Mode\AuthorQuery::create()->orderByName()
));

Resources