user6838184
user6838184

Reputation:

Get values of Entity Type in Symfony 3

How do I get the value of dropdown field in controller.

I have drop down fields where values of data is from one of my table in database. Please see the code below.

class SubAgentType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $builder->add('company_id', EntityType::class, array(
            'label' => 'Company',
            'required' => true,
            'class' => 'SwipeBundle:Company',
            'choice_label' => 'name',
            'expanded' => false,
            'multiple' => false,
            'placeholder' => 'Choose a Company',
            'constraints' => array(
                new NotBlank(array("message" => 'Company name is required.')),
            ),            
        )); ../ 

In my html.twig it works fine, it renders the html correctly whereas there's a value numeric and the name of company.

UPDATE:

<select id="sub_agent_company_id" name="sub_agent[company_id]" required="required">
<option value="" selected="selected">Choose a Company</option>
<option value="20">20 Incorporated</option>
<option value="21">21</option>
<option value="22">22</option>
</select>

The problem is the entity type is returning the class company object instead of the value of a dropdown only.

How do I get only the values of dropdown instead an object?

here's the screenshot of error in my dropdown companyid [![enter image description here][1]][1]

Upvotes: 0

Views: 2494

Answers (2)

Alvin Bunk
Alvin Bunk

Reputation: 7764

An easier way is to use choice_value and then specify the ORM Column name you used in the Entity. I presume below that is company_id but you might need to change it. You can also specify the choice_label in the same way. It makes coding easier.

$builder->add('company_id', EntityType::class, array(
    'label' => 'Company',
    'class' => 'SwipeBundle:Company',
    'choice_value' => 'company_id',
    'choice_label' => 'company_id',
    'placeholder' => 'Choose a Company',
    'constraints' => array(
        new NotBlank(array("message" => 'Company name is required.')),
    ),            
));

Let me know if that works. I think it should.

Upvotes: 0

Dan Costinel
Dan Costinel

Reputation: 1736

I guess you need to use an anonymous function inside choice_label, in order to get the ids. And I also think you should rename the field name, and instead of company_id simply put company.

$builder->add('company', EntityType::class, array(
    //...
    'choice_label' => function($company){
        return $company->getId();
    },
//...

Moreover, you may need to use the query_builder option inside this code, but this thing you'll gonna tell us later, after trying the code I wrote.

LE

For query_builder.

use use Doctrine\ORM\EntityRepository;
//...
$builder->add('company', EntityType::class, array(
//...
`query_builder` => function(EntityRepository $er){
    return $er->createQueryBuilder('c');
},
'choice_label' => function($company){
    return $company->getId();
},
//...

Upvotes: 1

Related Questions