symfony forms: get a second attribute in label for a select element










0















I have an entity Institution. In a form a user can select one or more. I use Select2 for the font-end. An institution has the attribute internationalName, which is the default attribute because of:



Institution.php



public function __toString()

return $this->internationalName;



An institution can also have an abbreviated name, as attribute abbreviation. What I would like, is to use this second attribute to show (if it exists) in the select form. Even better would be that it's not shown but that you can search for it, but I really don't know if this is possible at all.



I could change __toString() so that it includes the abbreviation, but this is unwanted because of other forms, so I am trying to make it show only in this form through



LocationType.php



->add('Institutions', EntityType::class, [
'class' => Institution::class,
'label' => 'Connected Institution(s)',
'multiple' => true,
'attr' => ['data-select' => 'true', 'data-placeholder' => 'start typing to find your institution...'],
'constraints' => array(
new Count(array(
'min' => 1,
'minMessage' => "Select at least one institution."))),
'query_builder' => function (EntityRepository $er)
return $er->createQueryBuilder('i')
->orderBy('i.internationalName', 'ASC');
,


I tried to use 'choice_label' => 'abbreviation' (just as a test), but that makes all labels blank, which I really don't understand why. I also tried 'choice_label' => 'internationalName'.'abbreviation', but that won't work because there is no property internationalNameabbreviation. I thought about creating a new attribute where I combine the two, but given that 'choice_label' => 'abbreviation' already results in a blank list I don't think this would work. Any other options or solutions?



edit: as requested the relevant entity class part,
Institution.php



/**
* @AssertNotBlank(message="Please enter the international name.")
* @ORMColumn(type="string")
*/
private $internationalName;

/**
* @ORMColumn(type="string", nullable=true)
*/
private $abbreviation;









share|improve this question
























  • Your entity has an "abbrevation" property ?

    – Thomas Lefetz
    Nov 15 '18 at 13:51











  • It has an 'abbreviation' attribute, yes, which is a string.

    – Dirk J. Faber
    Nov 15 '18 at 13:58











  • Can you post you entity class ?

    – Thomas Lefetz
    Nov 16 '18 at 7:19











  • @ThomasLefetz, I added the relevant part of the entity class.

    – Dirk J. Faber
    Nov 16 '18 at 13:10















0















I have an entity Institution. In a form a user can select one or more. I use Select2 for the font-end. An institution has the attribute internationalName, which is the default attribute because of:



Institution.php



public function __toString()

return $this->internationalName;



An institution can also have an abbreviated name, as attribute abbreviation. What I would like, is to use this second attribute to show (if it exists) in the select form. Even better would be that it's not shown but that you can search for it, but I really don't know if this is possible at all.



I could change __toString() so that it includes the abbreviation, but this is unwanted because of other forms, so I am trying to make it show only in this form through



LocationType.php



->add('Institutions', EntityType::class, [
'class' => Institution::class,
'label' => 'Connected Institution(s)',
'multiple' => true,
'attr' => ['data-select' => 'true', 'data-placeholder' => 'start typing to find your institution...'],
'constraints' => array(
new Count(array(
'min' => 1,
'minMessage' => "Select at least one institution."))),
'query_builder' => function (EntityRepository $er)
return $er->createQueryBuilder('i')
->orderBy('i.internationalName', 'ASC');
,


I tried to use 'choice_label' => 'abbreviation' (just as a test), but that makes all labels blank, which I really don't understand why. I also tried 'choice_label' => 'internationalName'.'abbreviation', but that won't work because there is no property internationalNameabbreviation. I thought about creating a new attribute where I combine the two, but given that 'choice_label' => 'abbreviation' already results in a blank list I don't think this would work. Any other options or solutions?



edit: as requested the relevant entity class part,
Institution.php



/**
* @AssertNotBlank(message="Please enter the international name.")
* @ORMColumn(type="string")
*/
private $internationalName;

/**
* @ORMColumn(type="string", nullable=true)
*/
private $abbreviation;









share|improve this question
























  • Your entity has an "abbrevation" property ?

    – Thomas Lefetz
    Nov 15 '18 at 13:51











  • It has an 'abbreviation' attribute, yes, which is a string.

    – Dirk J. Faber
    Nov 15 '18 at 13:58











  • Can you post you entity class ?

    – Thomas Lefetz
    Nov 16 '18 at 7:19











  • @ThomasLefetz, I added the relevant part of the entity class.

    – Dirk J. Faber
    Nov 16 '18 at 13:10













0












0








0








I have an entity Institution. In a form a user can select one or more. I use Select2 for the font-end. An institution has the attribute internationalName, which is the default attribute because of:



Institution.php



public function __toString()

return $this->internationalName;



An institution can also have an abbreviated name, as attribute abbreviation. What I would like, is to use this second attribute to show (if it exists) in the select form. Even better would be that it's not shown but that you can search for it, but I really don't know if this is possible at all.



I could change __toString() so that it includes the abbreviation, but this is unwanted because of other forms, so I am trying to make it show only in this form through



LocationType.php



->add('Institutions', EntityType::class, [
'class' => Institution::class,
'label' => 'Connected Institution(s)',
'multiple' => true,
'attr' => ['data-select' => 'true', 'data-placeholder' => 'start typing to find your institution...'],
'constraints' => array(
new Count(array(
'min' => 1,
'minMessage' => "Select at least one institution."))),
'query_builder' => function (EntityRepository $er)
return $er->createQueryBuilder('i')
->orderBy('i.internationalName', 'ASC');
,


I tried to use 'choice_label' => 'abbreviation' (just as a test), but that makes all labels blank, which I really don't understand why. I also tried 'choice_label' => 'internationalName'.'abbreviation', but that won't work because there is no property internationalNameabbreviation. I thought about creating a new attribute where I combine the two, but given that 'choice_label' => 'abbreviation' already results in a blank list I don't think this would work. Any other options or solutions?



edit: as requested the relevant entity class part,
Institution.php



/**
* @AssertNotBlank(message="Please enter the international name.")
* @ORMColumn(type="string")
*/
private $internationalName;

/**
* @ORMColumn(type="string", nullable=true)
*/
private $abbreviation;









share|improve this question
















I have an entity Institution. In a form a user can select one or more. I use Select2 for the font-end. An institution has the attribute internationalName, which is the default attribute because of:



Institution.php



public function __toString()

return $this->internationalName;



An institution can also have an abbreviated name, as attribute abbreviation. What I would like, is to use this second attribute to show (if it exists) in the select form. Even better would be that it's not shown but that you can search for it, but I really don't know if this is possible at all.



I could change __toString() so that it includes the abbreviation, but this is unwanted because of other forms, so I am trying to make it show only in this form through



LocationType.php



->add('Institutions', EntityType::class, [
'class' => Institution::class,
'label' => 'Connected Institution(s)',
'multiple' => true,
'attr' => ['data-select' => 'true', 'data-placeholder' => 'start typing to find your institution...'],
'constraints' => array(
new Count(array(
'min' => 1,
'minMessage' => "Select at least one institution."))),
'query_builder' => function (EntityRepository $er)
return $er->createQueryBuilder('i')
->orderBy('i.internationalName', 'ASC');
,


I tried to use 'choice_label' => 'abbreviation' (just as a test), but that makes all labels blank, which I really don't understand why. I also tried 'choice_label' => 'internationalName'.'abbreviation', but that won't work because there is no property internationalNameabbreviation. I thought about creating a new attribute where I combine the two, but given that 'choice_label' => 'abbreviation' already results in a blank list I don't think this would work. Any other options or solutions?



edit: as requested the relevant entity class part,
Institution.php



/**
* @AssertNotBlank(message="Please enter the international name.")
* @ORMColumn(type="string")
*/
private $internationalName;

/**
* @ORMColumn(type="string", nullable=true)
*/
private $abbreviation;






php symfony jquery-select2 symfony4






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 13:10







Dirk J. Faber

















asked Nov 15 '18 at 13:24









Dirk J. FaberDirk J. Faber

1,3461317




1,3461317












  • Your entity has an "abbrevation" property ?

    – Thomas Lefetz
    Nov 15 '18 at 13:51











  • It has an 'abbreviation' attribute, yes, which is a string.

    – Dirk J. Faber
    Nov 15 '18 at 13:58











  • Can you post you entity class ?

    – Thomas Lefetz
    Nov 16 '18 at 7:19











  • @ThomasLefetz, I added the relevant part of the entity class.

    – Dirk J. Faber
    Nov 16 '18 at 13:10

















  • Your entity has an "abbrevation" property ?

    – Thomas Lefetz
    Nov 15 '18 at 13:51











  • It has an 'abbreviation' attribute, yes, which is a string.

    – Dirk J. Faber
    Nov 15 '18 at 13:58











  • Can you post you entity class ?

    – Thomas Lefetz
    Nov 16 '18 at 7:19











  • @ThomasLefetz, I added the relevant part of the entity class.

    – Dirk J. Faber
    Nov 16 '18 at 13:10
















Your entity has an "abbrevation" property ?

– Thomas Lefetz
Nov 15 '18 at 13:51





Your entity has an "abbrevation" property ?

– Thomas Lefetz
Nov 15 '18 at 13:51













It has an 'abbreviation' attribute, yes, which is a string.

– Dirk J. Faber
Nov 15 '18 at 13:58





It has an 'abbreviation' attribute, yes, which is a string.

– Dirk J. Faber
Nov 15 '18 at 13:58













Can you post you entity class ?

– Thomas Lefetz
Nov 16 '18 at 7:19





Can you post you entity class ?

– Thomas Lefetz
Nov 16 '18 at 7:19













@ThomasLefetz, I added the relevant part of the entity class.

– Dirk J. Faber
Nov 16 '18 at 13:10





@ThomasLefetz, I added the relevant part of the entity class.

– Dirk J. Faber
Nov 16 '18 at 13:10












1 Answer
1






active

oldest

votes


















1














Maybe something like:



https://symfony.com/doc/current/reference/forms/types/choice.html#choice-value



 ->add('Institutions', EntityType::class, [
'class' => Institution::class,
'label' => 'Connected Institution(s)',
'query_builder' => function (EntityRepository $er)
return $er
->createQueryBuilder('i')
->orderBy('i.internationalName', 'ASC')
;
,
'choice_value' => function (Institution $institution = null)
return $institution ? $institution->getInternationalName() . '(' . $institution->getAbbreviation() . ')' : '';
,
])





share|improve this answer























  • With 'choice_label' instead of 'choice_value' this is a good option and I can show the abbreviation and search for it. Next step, if you have a suggestion for this as well, is to hide the abbreviated part to the user but still make it searchable.

    – Dirk J. Faber
    Nov 16 '18 at 13:08










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320486%2fsymfony-forms-get-a-second-attribute-in-label-for-a-select-element%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Maybe something like:



https://symfony.com/doc/current/reference/forms/types/choice.html#choice-value



 ->add('Institutions', EntityType::class, [
'class' => Institution::class,
'label' => 'Connected Institution(s)',
'query_builder' => function (EntityRepository $er)
return $er
->createQueryBuilder('i')
->orderBy('i.internationalName', 'ASC')
;
,
'choice_value' => function (Institution $institution = null)
return $institution ? $institution->getInternationalName() . '(' . $institution->getAbbreviation() . ')' : '';
,
])





share|improve this answer























  • With 'choice_label' instead of 'choice_value' this is a good option and I can show the abbreviation and search for it. Next step, if you have a suggestion for this as well, is to hide the abbreviated part to the user but still make it searchable.

    – Dirk J. Faber
    Nov 16 '18 at 13:08















1














Maybe something like:



https://symfony.com/doc/current/reference/forms/types/choice.html#choice-value



 ->add('Institutions', EntityType::class, [
'class' => Institution::class,
'label' => 'Connected Institution(s)',
'query_builder' => function (EntityRepository $er)
return $er
->createQueryBuilder('i')
->orderBy('i.internationalName', 'ASC')
;
,
'choice_value' => function (Institution $institution = null)
return $institution ? $institution->getInternationalName() . '(' . $institution->getAbbreviation() . ')' : '';
,
])





share|improve this answer























  • With 'choice_label' instead of 'choice_value' this is a good option and I can show the abbreviation and search for it. Next step, if you have a suggestion for this as well, is to hide the abbreviated part to the user but still make it searchable.

    – Dirk J. Faber
    Nov 16 '18 at 13:08













1












1








1







Maybe something like:



https://symfony.com/doc/current/reference/forms/types/choice.html#choice-value



 ->add('Institutions', EntityType::class, [
'class' => Institution::class,
'label' => 'Connected Institution(s)',
'query_builder' => function (EntityRepository $er)
return $er
->createQueryBuilder('i')
->orderBy('i.internationalName', 'ASC')
;
,
'choice_value' => function (Institution $institution = null)
return $institution ? $institution->getInternationalName() . '(' . $institution->getAbbreviation() . ')' : '';
,
])





share|improve this answer













Maybe something like:



https://symfony.com/doc/current/reference/forms/types/choice.html#choice-value



 ->add('Institutions', EntityType::class, [
'class' => Institution::class,
'label' => 'Connected Institution(s)',
'query_builder' => function (EntityRepository $er)
return $er
->createQueryBuilder('i')
->orderBy('i.internationalName', 'ASC')
;
,
'choice_value' => function (Institution $institution = null)
return $institution ? $institution->getInternationalName() . '(' . $institution->getAbbreviation() . ')' : '';
,
])






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 16:39









Alex.BarylskiAlex.Barylski

81922145




81922145












  • With 'choice_label' instead of 'choice_value' this is a good option and I can show the abbreviation and search for it. Next step, if you have a suggestion for this as well, is to hide the abbreviated part to the user but still make it searchable.

    – Dirk J. Faber
    Nov 16 '18 at 13:08

















  • With 'choice_label' instead of 'choice_value' this is a good option and I can show the abbreviation and search for it. Next step, if you have a suggestion for this as well, is to hide the abbreviated part to the user but still make it searchable.

    – Dirk J. Faber
    Nov 16 '18 at 13:08
















With 'choice_label' instead of 'choice_value' this is a good option and I can show the abbreviation and search for it. Next step, if you have a suggestion for this as well, is to hide the abbreviated part to the user but still make it searchable.

– Dirk J. Faber
Nov 16 '18 at 13:08





With 'choice_label' instead of 'choice_value' this is a good option and I can show the abbreviation and search for it. Next step, if you have a suggestion for this as well, is to hide the abbreviated part to the user but still make it searchable.

– Dirk J. Faber
Nov 16 '18 at 13:08



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320486%2fsymfony-forms-get-a-second-attribute-in-label-for-a-select-element%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto