-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[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
Conversation
7a990a8
to
8fc661a
Compare
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 But it can be very helpful for 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 |
@webmozart Could you validate this new feature? It looks like quite straightforward but having your approval would probably help. Thanks. |
Saw that question on stack today : http://stackoverflow.com/questions/35769809/how-to-style-choice-field-labels-in-symfony/35826837#35826837 |
* | ||
* @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); |
There was a problem hiding this comment.
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.
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. |
It's the same BC break as introduced in 2.7. I was too late at this time :( |
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 ? |
8fc661a
to
104f542
Compare
104f542
to
6f71cdc
Compare
Complete refactoring, see #18318. Updated description. |
Failures are unrelated. |
0d69e63
to
7bd6d19
Compare
c18aaf8
to
aa9c3cc
Compare
Rebased and green :) Would you merge this one? |
648c8a7
to
aa4d19a
Compare
That would be for 3.3 :) |
Is this still planned for 3.3? Really looking forward to see this merged. |
@HeahDude What's the status of this PR? Last call for 3.3 :) |
aa4d19a
to
9d9301e
Compare
9d9301e
to
0fe7097
Compare
// 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())); |
There was a problem hiding this comment.
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 ..."
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here
@HeahDude what do you want to do with this PR? Last call for deprecations in 3.4 :) |
@HeahDude Do you think you can work on finishing this PR or shall we close it? |
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. |
Closing this PR as there is no more activity. @HeahDude Feel free to reopen whenever you have time to finish it. Thanks. |
This feature would be quite useful with ChoiceType multiple=true and expanded=true. Sad it wasn't merged. |
choice_attr
as nested arrays in favor of a global array for all choices or a callable,choice_attr
as string, property path,choice_label_attr
option for radio buttons andcheckboxes which can be a global array for all choices or a callable.