Skip to content

[Form] added "choice_label_attr" option and deprecated "choice_attr" as multi-arrays or property path #16834

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

Conversation

HeahDude
Copy link
Contributor

@HeahDude HeahDude commented Dec 4, 2015

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? yes
Tests pass? no
Fixed tickets #14404, #18318
License MIT
Doc PR todo
  • 68c1557 deprecates choice_attr as nested arrays in favor of a global array for all choices or a callable,
  • f012ebe deprecates choice_attr as string, property path,
  • f7649d1 adds choice_label_attr option for radio buttons and
    checkboxes which can be a global array for all choices or a callable.
  • Address a doc PR
  • Update CHANGELOG

@HeahDude
Copy link
Contributor Author

This one has been approved a long time ago by @webmozart in #14413 (comment).

My original use case was a complex form with many nested ChoiceType expanded and the need to add custom classes to option labels. It actually requires complex loops in twig and maybe override form themes.

But it can be very helpful for EntityType which inherits this option and add some consistency with choice_value, choice_name, choice_label and choice_attr introduced in #14050.

Here's a basic usage :

use AppBundle\Entity\Task;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;

$form = $this->createFormBuilder()
    // ...
    ->add('tasks', EntityType::class, array(
        'class' => Task::class,
        'expanded' => true,
        'choice_label' => 'todo',
        'choice_label_attr' => function($task) {
            if (null === $task || '' === $task) {
                return;
            }

            $class = 'label-task';

            if ($task->needsReview) {
                $class .= ' badged-label badge-review';
            } elseif ($task->isImportant || $task->dueDate() < new \DateTime('+ 2 days')) {
                $class .= ' font-red badged-label badge-alert';
            }

            return array('class' => $class);
        },
        // ...
    );

Is there any hope to get this one merged in 3.1 ?

ping @symfony/deciders

@fabpot
Copy link
Member

fabpot commented Mar 4, 2016

@webmozart Could you validate this new feature? It looks like quite straightforward but having your approval would probably help. Thanks.

@HeahDude
Copy link
Contributor Author

HeahDude commented Mar 6, 2016

*
* @return ChoiceListView The choice list view
*/
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null);
public function createView(ChoiceListInterface $list, $preferredChoices = null, $label = null, $index = null, $groupBy = null, $attr = null, $labelAttr = null);
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 a BC break.

@HeahDude HeahDude changed the title [3.1][Form] Feature : add choice_label_attr option to ChoiceType [Form] add choice_label_attr option to ChoiceType Mar 7, 2016
@webmozart
Copy link
Contributor

Technically, this feature is fine, but after the changes to our BC policy in symfony/symfony-docs#5735 I don't see how this can be added without breaking BC.

@HeahDude
Copy link
Contributor Author

HeahDude commented Mar 7, 2016

It's the same BC break as introduced in 2.7. I was too late at this time :(

@HeahDude
Copy link
Contributor Author

HeahDude commented Mar 7, 2016

If I understand this right, it is a matter of choice between two edge cases: either the one extending choice factories or implementing the factory interface, or the one needing this feature.

Actually is a dev able to implement this feature on its own without messing with the core interface ?

@HeahDude
Copy link
Contributor Author

Complete refactoring, see #18318. Updated description.

@HeahDude
Copy link
Contributor Author

Failures are unrelated.

@HeahDude HeahDude force-pushed the feature-add_choice_label_attr branch 2 times, most recently from 0d69e63 to 7bd6d19 Compare March 26, 2016 18:08
@HeahDude HeahDude force-pushed the feature-add_choice_label_attr branch from c18aaf8 to aa9c3cc Compare November 9, 2016 23:48
@HeahDude
Copy link
Contributor Author

Rebased and green :) Would you merge this one?

@HeahDude HeahDude force-pushed the feature-add_choice_label_attr branch 2 times, most recently from 648c8a7 to aa4d19a Compare November 10, 2016 00:14
@fabpot
Copy link
Member

fabpot commented Nov 10, 2016

That would be for 3.3 :)

@nicolas-grekas nicolas-grekas added this to the 3.x milestone Dec 6, 2016
@Sander-Toonen
Copy link
Contributor

Is this still planned for 3.3? Really looking forward to see this merged.

@fabpot
Copy link
Member

fabpot commented Mar 22, 2017

@HeahDude What's the status of this PR? Last call for 3.3 :)

@HeahDude HeahDude force-pushed the feature-add_choice_label_attr branch from aa4d19a to 9d9301e Compare March 26, 2017 18:35
@HeahDude
Copy link
Contributor Author

HeahDude commented Mar 26, 2017

@fabpot I've updated the changelogs, upgrade file and deprecations notices to target 3.3, and also pushed a fourth commit 0fe7097, to remove the usage of an extra interface in favor of a proper deprecation, if you agree with this change I'll add some tests and the corresponding changelog entry.

@HeahDude HeahDude force-pushed the feature-add_choice_label_attr branch from 9d9301e to 0fe7097 Compare March 26, 2017 18:46
// BC
$refMethod = new \ReflectionMethod($this->choiceListFactory, 'createView');
if (6 > $refMethod->getNumberOfParameters()) {
@trigger_error(sprintf('Not passing a "$labelAttr" as sixth argument of "%s" is deprecated since version 3.3 and will trigger an error in 4.0.', $refMethod->getNamespaceName()));
Copy link
Contributor Author

Choose a reason for hiding this comment

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

In fact the message should say "Not declaring the seventh argument ..."

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@symfony/deciders Should I go with this bc layer to update the interface in 4.0 rather than adding the extra interface (cf last commit)?

@@ -218,12 +219,13 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
@trigger_error('Passing callable strings is deprecated since version 3.1 and PropertyAccessDecorator will treat them as property paths in 4.0. You should use a "\Closure" instead.', E_USER_DEPRECATED);
}

// Deprecated since 3.3 and to be removed in 4.0 with the condition above
Copy link
Member

Choose a reason for hiding this comment

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

if it is deprecated, it must trigger a deprecation notice.

And why deprecating this feature ? It is not consistent with other properties.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because of what =I explained in the related issue, it's more consistent IMO to deprecate this behavior rather than supporting it for choice_label_attr too, this is unneeded overhead, the callable or unique array should be the way to go for these.

},
```

* Using `choice_attr` option as a string or a `ProprertyPath` instance has been
Copy link
Contributor

Choose a reason for hiding this comment

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

Typo

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nice catch, thanks!

},
```

* Using `choice_attr` option as a string or a `ProprertyPath` instance will
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here

@nicolas-grekas
Copy link
Member

@HeahDude what do you want to do with this PR? Last call for deprecations in 3.4 :)

@nicolas-grekas nicolas-grekas modified the milestones: 3.4, 4.1 Oct 8, 2017
@fabpot
Copy link
Member

fabpot commented Jan 24, 2018

@HeahDude Do you think you can work on finishing this PR or shall we close it?

@HeahDude
Copy link
Contributor Author

Don't worry I'll finish all my opened PRs, they're just not the priority for now. I've made a clean up by closing some for the last feature freeze, not having time to finish anything.
2.7 is still my first target with missing fixes and tests, continuing #21877 and closing old issues, another bunch before I finish here.
Happy rebasing and conflict merging stuff are ahead this year <3

@fabpot
Copy link
Member

fabpot commented Mar 31, 2018

Closing this PR as there is no more activity. @HeahDude Feel free to reopen whenever you have time to finish it. Thanks.

@FloatingMaster
Copy link

This feature would be quite useful with ChoiceType multiple=true and expanded=true. Sad it wasn't merged.

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.