Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Adding attributes to ChoiceView #9694

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 5 commits into from
Closed

Conversation

ruian
Copy link
Contributor

@ruian ruian commented Dec 4, 2013

Description:
Because some times is usefull to add some attributes to select's options. Exemple when using, select2, chosen, or juste by adding some prototypes.

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #5286
License MIT

Example :

    $builder->add('test2', 'choice', array(
            'choices' => array(
                'male' => 'MALE',
                'female' => 'FEMALE'
            ),
            'choices_attributes' => array(
                 array('titi' => 'toto'),
                 array('foo' => 'bar')
            ),
            // OR
            'choices_attributes' => function(ChoiceListInterface $choices) {
                $att = array();
                foreach ($choices->getChoices() as $choice) {
                    $attr[] = array('foo' => 'data from another place' . $choice);
                }

                return $attr;
            },
            'choices_prototypes' => array(
               array('name' => 'toto', 'form' => new ContactType(), 'options' => array())
            )
            // OR
            'choices_prototypes' => function(ChoiceListInterface $choices) {
                $protos = array();
                foreach ($choices->getChoices() as $choice) {
                    $protos[] = array('name' => 'advanced', 'type' => new ContactType($choice));
                }

                return $protos;
            }
        ));

@@ -19,6 +19,13 @@
class ChoiceView
{
/**
* Attributes who can be used in view.
Copy link
Contributor

Choose a reason for hiding this comment

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

who -> which

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed ;-)

@stof
Copy link
Member

stof commented Dec 7, 2013

This alone does not fix the ticket at all. It needs to be used and configurable in all relevant places

@ruian
Copy link
Contributor Author

ruian commented Dec 9, 2013

@stof can you add some details ? Because this is first step to open the ChoiceView.

@stof
Copy link
Member

stof commented Dec 9, 2013

@ruian this is indeed the first step. bBut in the current state, it does not fix the issue. More steps are needed, which would use the work done in your first commit
So your description saying it fixes the ticket is wrong.

@ruian
Copy link
Contributor Author

ruian commented Dec 9, 2013

@stof Which are the others steps, maybe i could update this PR.
Because with this little code change, we can easily give data-attributes to the choiceView with the buildView method from FormType. Without this, you have to re implement the ChoiceList and the ChoiceView on your own project.

@stof
Copy link
Member

stof commented Dec 9, 2013

Well, attributes are ignored currently, amking them useless. So I see at least 2 steps:

  • updating the rendering of choices to take these attributes into account
  • updating the building of choice views to allow configuring these attributes easily (having to customize buildView to loop over the choice views is not an easy configuration, as it requires using a custom choice type for this)

@ruian
Copy link
Contributor Author

ruian commented Dec 9, 2013

You're right i will work on it.
Thx

@@ -12,21 +12,23 @@
namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\View\ChoiceView;
Copy link
Member

Choose a reason for hiding this comment

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

Can you revert the reordering of the use statements? That makes merging between branches a nightmare.

@fabpot
Copy link
Member

fabpot commented Dec 29, 2013

You forgot to also update the PHP templates.

@fabpot
Copy link
Member

fabpot commented Dec 29, 2013

@bschussek What do you think of this feature?

@ruian
Copy link
Contributor Author

ruian commented Jan 6, 2014

@fabpot Yeah sure i will update this PR asap

ruian and others added 3 commits January 7, 2014 09:45
Description:
Because some times is usefull to add some attributes to select's options. Exemple when using, select2, chosen, or juste by adding some prototypes.

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | symfony#5286
| License       | MIT
@ruian
Copy link
Contributor Author

ruian commented Jan 7, 2014

ping @stof can you give your feedback ?


$choicesAttributes = $options['choices_attributes'];
$choicesPrototypes = $options['choices_prototypes'];
if (count($choicesAttributes) || count($choicesPrototypes)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not:

if (!empty($options['choices_attributes']) || !empty($options['choices_prototypes'])) {}

As you don't need to know how many choices exists, but just there are somem, also those new variable are useless (even you use only one of those).

@ruian
Copy link
Contributor Author

ruian commented Jan 7, 2014

thx @stloyd it's now fixed

@@ -132,6 +156,19 @@ public function buildView(FormView $view, FormInterface $form, array $options)
// POST request.
$view->vars['full_name'] = $view->vars['full_name'].'[]';
}

if (!empty($options['choices_attributes']) || !empty($options['choices_prototypes'])) {
$choicesAttributes = $options['choices_attributes'];
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the point of this variable? You are not manipulating that array, so IMO the is no real point of adding it and not working directly on $options['choices_attributes'].

Copy link
Contributor Author

Choose a reason for hiding this comment

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

humm you must be right.

@fabpot
Copy link
Member

fabpot commented Mar 26, 2014

Closing in favor of #10309

@fabpot fabpot closed this Mar 26, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants