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

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed

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

wants to merge 2 commits into from

Conversation

mvrhov
Copy link

@mvrhov mvrhov commented Mar 1, 2012

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 commented Mar 1, 2012

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

@mvrhov
Copy link
Author

mvrhov commented Mar 1, 2012

IMHO this doesn't belong to the entity class

@stof
Copy link
Member

stof commented Mar 1, 2012

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

@Burgov
Copy link
Contributor

Burgov commented Mar 2, 2012

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) {
    return sprintf('%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 :

public function getChoiceText() {
   return ltrim(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

craue commented Mar 29, 2012

Would be nice to have something like this. Enhancing the property 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

sstok commented Mar 30, 2012

+1

@inoryy
Copy link
Contributor

inoryy commented Mar 30, 2012

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

@elnur
Copy link
Contributor

elnur commented Apr 2, 2012

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 commented Apr 3, 2012

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

@mvrhov
Copy link
Author

mvrhov commented Apr 6, 2012

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 like array($object, $method) on PHP 5.3

@fabpot
Copy link
Member

fabpot commented Apr 11, 2012

@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

fabpot commented Jul 9, 2012

Closing this PR in favor of #4067

@fabpot fabpot closed this Jul 9, 2012
@inoryy
Copy link
Contributor

inoryy commented Jul 13, 2012

@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

fabpot commented Jul 13, 2012

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

inoryy commented Jul 13, 2012

@bschussek ok, thanks!

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

Successfully merging this pull request may close these issues.

10 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.