Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[Form] label path in ObjectChoiceList can also be callable#3479

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Closed
mvrhov wants to merge2 commits intosymfony:masterfrommvrhov:entityChoiceLabelBuilder
Closed

[Form] label path in ObjectChoiceList can also be callable#3479

mvrhov wants to merge2 commits intosymfony:masterfrommvrhov:entityChoiceLabelBuilder

Conversation

mvrhov
Copy link

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -

This PR adds a callback support as additional option to label path in ObjectChoiceList class.

Right now you either have to set a property path to a property or a getter or implement __toString method in the objects you pass as choices to get the choice label. Usually this is ok, but sometimes you need to have different labels depending on where the choice filed is displayed, or __toString is used elsewhere for different purposes.

Usage example:

$builder            ->add('account','entity',array('class' =>'xxx\Entity\Account','property' =>function ($entity) {/** @var $entity xxx\Entity\Account */$ret ='';$ret .=$entity->getFirstName() .'';$ret .=$entity->getLastName();$ret .=$entity->getCompany() ?' @' .$entity->getCompany() :'';return$ret;                },            ))

@stof
Copy link
Member

stof commentedMar 1, 2012

I don't see any reason to add this. there is already aproperty option allowing to pass the property used to get the label (what is used is the corresponding getter). Simply create agetDisplayName() method in your Account class containing you logic and pass theproperty option todisplayName

@mvrhov
Copy link
Author

IMHO this doesn't belong to the entity class

@stof
Copy link
Member

stof commentedMar 1, 2012

why not ? it is about getting some display name. Adding a getter in the entity makes this reusable

@Burgov
Copy link
Contributor

i could imagine the case where the label needs to be rendered using a service, e.g.:

$builder->add('product','entity',array(// ... //'label_builder' =>function($entity)use ($priceManager) {returnsprintf('%s (%.2f)',$entity->getName(),$priceManager->calculateDiscountPrice($entity));  })));

This is definitely not something that the entity should handle.

@pvanliefland
Copy link

Just asked myself the same question today...

I would like to display a dropdown list of hierarchical entity data, something like

Europe

  • Belgium
  • France
    Asia
  • Japan
  • China

To do that, I could add a method getChoiceText() in my entity :

publicfunctiongetChoiceText() {returnltrim(str_repeat('-',$this->level) .'' .$this->name);}

It works, but it feels a bit odd to add purely "form/presentation" logic in my entity. This method will only be used in my form.

The callback idea makes sense, although there's no doubt we can live without.

Wouldn't it be possible to use the 'property' option to achieve what@mvrhov is suggesting ? This option could accept either a string, to be used by PropertyPath, or a closure that would be provided with an entity ?

@craue
Copy link
Contributor

Would be nice to have something like this. Enhancing theproperty option to accept a closure as@pvanliefland mentioned would be fine as well.

@stof: I'd need to call the method of a service to build each label, which is not possible currently.

@sstok
Copy link
Contributor

+1

@inoryy
Copy link
Contributor

+1, it's not going to be used a lot, but still a useful feature

@elnur
Copy link
Contributor

Nice. I'd love to have this.

@@ -64,6 +64,9 @@ class EntityChoiceList extends ObjectChoiceList
*/
private $loaded = false;


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

extra empty line here

@stof
Copy link
Member

stof commentedApr 3, 2012

This should probably be implemented in the ObjectChoiceList instead of the EntityChoiceList

@mvrhov
Copy link
Author

Added test, moved the implementation to base class (ObjectChoiceList) as suggested by@stof. and extended $labelPath parameter as suggested by@pvanliefland.

I'd really like to see some comments about changed phpdoc from someone more fluent in English.

$labels[$i] = $this->labelPath->getValue($choice);
} elseif (is_callable($this->labelPath)) {
$labels[$i] = $callable($choice);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

this is wrong. It won't work for callable likearray($object, $method) on PHP 5.3

@fabpot
Copy link
Member

@bschussek is it ok for you?

* is used instead.
* by calling the getter on the object. You can
* also pass any callable. When callable will be
* executed it will get the choice parameter passed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I would say :

Alternatively, $labelPath can be a PHP callable
taking an element from the ObjectChoiceList as
parameter and returning a string.

But then $labelPath should be documented as a mixed parameter ?

@webmozart
Copy link
Contributor

@fabpot I'm not yet satisfied with this PR. I think this problem should be solved on a more general scope than just ObjectChoiceList, but I need to give this some more thought.

@fabpot
Copy link
Member

Closing this PR in favor of#4067

@fabpotfabpot closed thisJul 9, 2012
@inoryy
Copy link
Contributor

@fabpot are they really same, though? This PR offered label customization for every type (which I was really looking forward to :/),#4067 is for choice/entity choice labels..

@fabpot
Copy link
Member

ping @bschussek

@webmozart
Copy link
Contributor

@Inori Yes they are the same.#4067 goes even further and adds this functionality to all choice lists, not just to ObjectChoiceList.

@inoryy
Copy link
Contributor

@bschussek ok, thanks!

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

10 participants
@mvrhov@stof@Burgov@pvanliefland@craue@sstok@inoryy@elnur@fabpot@webmozart

[8]ページ先頭

©2009-2025 Movatter.jp