Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Form][2.8] Type "entity" not setting selected data when choice_value is a string attribute #17271

Closed
Rasanga opened this issue Jan 5, 2016 · 12 comments
Labels

Comments

@Rasanga
Copy link

Rasanga commented Jan 5, 2016

I have the following code:

$builder->add('category', 'entity', [
                'required'      => false,
                'class'         => 'Ras\Bundle\DomainBundle\Entity\Category',
                'choice_value'  => 'name',
                'choice_label'  => 'label'
            ]);

When I select a category it does not give me the selected category entity, but when I changed choice_value to be id it worked fine. There seem to be an issue when the choice_value is a string attribute

NOTE:
Ras\Bundle\DomainBundle\Entity\Category entity has following attributes: id, name, label

@xabbuh xabbuh added the Form label Jan 5, 2016
@xabbuh
Copy link
Member

xabbuh commented Jan 5, 2016

Why do you want to change the choice_value property to name? Since the name property is apparently not the identifier of your Category entity, the entity type will not be able to find an entry for the submitted values.

@Rasanga
Copy link
Author

Rasanga commented Jan 5, 2016

thank you for the quick response 👍

I thought it should be able to find entries by given choice_value field. can we add this as a feature I can make a PR?

@xabbuh
Copy link
Member

xabbuh commented Jan 5, 2016

What is the advantage of that instead of just using the id?

@Rasanga
Copy link
Author

Rasanga commented Jan 5, 2016

When you use GET method to send form data it would great have to have URL query parameters in human readable manner

For example example.com/search?category=php is better than example.com/search?category=2

See the difference?

@xabbuh
Copy link
Member

xabbuh commented Jan 5, 2016

I understand your use case, but I do not think that a choice or entity type is what you should use for that imo.

@Tobion
Copy link
Member

Tobion commented Jan 5, 2016

The entity tyoe is using the primary key to look up the doctrine entity which of course won't find it with name. You can use a custom query builder for that.

@Tobion Tobion closed this as completed Jan 5, 2016
@Rasanga
Copy link
Author

Rasanga commented Jan 6, 2016

I used a query builder as follows but I had no luck:

$builder->add('category', 'entity', [
                'required'      => false,
                'class'         => 'Ras\Bundle\DomainBundle\Entity\Category',
                'choice_value'  => 'name',
                'choice_label'  => 'label',
                'query_builder' => function (CategoryRepository $repository) {
                    return $repository->createQueryBuilder('c')
                        ->addSelect('c.name')
                        ->addSelect('c.label')
                        ->groupBy('c.name')
                    ;
            ]);

@HeahDude
Copy link
Contributor

This issue should be reopened until #17694 is merged. This is a bug of EntityType not handling choice_value.

@xabbuh
Copy link
Member

xabbuh commented Feb 15, 2016

Well, we can keep it closed as we already keep track of that bug in #13964 and #17693.

fabpot added a commit that referenced this issue Feb 15, 2016
…pe (HeahDude)

This PR was merged into the 2.7 branch.

Discussion
----------

[2.7] [DoctrineBridge] [Form] fix choice_value in EntityType

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #17693, #17271, #13964
| License       | MIT
| Doc PR        | symfony/symfony-docs#6260

Commits
-------

2336d5c fix choice_value option in EntityType and add some tests
@igorpaladino
Copy link

Having this feature working would be a nice thing. I was dealing with the same issue. Meanwhile, a workaround solution would be using Data View Transformers http://symfony.com/doc/current/cookbook/form/data_transformers.html

@HeahDude
Copy link
Contributor

@igorpaladino it fixed by #17694 and #17990 as of 2.7.11, 2.8.4 and 3.0.4. Note that using a custom choice_value will prevent an optimization of the loader, loading all the entities on submission instead of the submitted values only.

@empiricalx
Copy link

I have been going nuts trying to solved this problem, to find out is a bug, Using a the work around is just overkill, I solved the issue with jquery.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

6 participants