4

Introduction

For my personal project i use

  • Symfony v4.2
  • PHP 7.2.12

Problem

I can not figure out how to make ChoiceType element display IDs and EMAILs as HTML SELECT option values and indexes.

Example

The picture displays current state of html select with data (id and email) from database.

The picture displays current state of `thml select  with data from database

Code

Array of data from database ($this->userChoices)

array:4 [
    0 => array:2 [
        "id" => 7
        "email" => "[email protected]"
    ]
    1 => array:2 [
        "id" => 8
        "email" => "[email protected]"
    ]
    2 => array:2 [
        "id" => 5
        "email" => "[email protected]"
    ]
    3 => array:2 [
        "id" => 6
        "email" => "[email protected]"
    ]
]

What i am getting

<select id="my-select" name="my-select" class="form-control">
    <optgroup label="0">
        <option value="7">7</option>
        <option value="[email protected]">[email protected]</option>
    </optgroup>
    <optgroup label="1">
        <option value="8">8</option>
        <option value="[email protected]">[email protected]</option>
    </optgroup>
    <optgroup label="2">
        <option value="5">5</option>
        <option value="[email protected]">[email protected]</option>
    </optgroup>
    <optgroup label="3">
        <option value="6">6</option>
        <option value="[email protected]">[email protected]</option>
    </optgroup>
</select>

What i want to get

<select id="my-select">
    <option value=""> Please choose an option </option>
    <option value="7">[email protected]</option>
    <option value="8">[email protected]</option>
    <option value="5">[email protected]</option>
    <option value="6">[email protected]</option>
</select>

Relevant part of myChoiceType

->add('selectUser', ChoiceType::class,
    [
        'choices' => $this->userChoices,
        'choice_value' => function ($choice)
        {
            return $choice;
        },
        'choice_label' => function ($value)
        {
            return $value;
        },
        'mapped' => false,
        'expanded' => false,
        'multiple' => false,
    ]
)

Finally

What am i doing wrong?

How to get ChoiceType display data in html select in the manner i want it to?

Thank you for ideas!

1
  • Hello, @Jeto! it is already displayed in first code block. I clarified the heading of the code block. Commented Dec 26, 2018 at 16:13

2 Answers 2

3

According to the docs, the choices value must be a simple key => value array.

The choices option is an array, where the array key is the item's label and the array value is the item's value

You can easily get the choices array you want using this code:

$choices = [];
foreach ($this->userChoices as $choice) {
  $choices[$choice['email']] = $choice['id'];
}

Then use the following initialization code:

->add('selectUser', ChoiceType::class,
    [
        'choices' => $choices,
        'mapped' => false,
        'expanded' => false,
        'multiple' => false
    ]
)

Not that I removed choice_value and choice_label which don't serve any purpose in this case.

Sign up to request clarification or add additional context in comments.

4 Comments

Hey @Jeto! I used your code in the controller before passing data array to the form, but i am getting a notice Notice: Undefined index: [email protected]
@Rikijs Sorry I messed up the code. Should be better now! (edited the answer)
Thank you @Jeto, now it works! Just exchanged id ad email to suite my needs - so email is displayed and value is id.
@Rikijs Oh you're right, I just edited again, I mixed them both up (but it was enough to give you the idea). Should be good now.
1

Simply pass it as a one dimensional array, with emails as keys, and ids as values.

$choices = [
    '[email protected]' => 7,
    '[email protected]' => 8,
];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.