10000 [Form] added "choice_label_attr" option and deprecated "choice_attr" as multi-arrays or property path by HeahDude · Pull Request #16834 · symfony/symfony · GitHub
[go: up one dir, main page]

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
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[Form] deprecated "choice_attr" option as PropertyPath
  • Loading branch information
HeahDude committed Mar 26, 2017
commit eb658f971ad7b9d2047530b896166d1f522ecf53
16 changes: 16 additions & 0 deletions UPGRADE-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ Form
},
```

* 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!

deprecated and will throw an exception in 4.0. Use a `callable` instead.

Before:

```php
'choice_attr' => 'htmlAttributes',
```

After:

```php
'choice_attr' => function ($choice, $value, $index) {
return $choice->getHtmlAttributes();
```

FrameworkBundle
---------------

Expand Down
17 changes: 17 additions & 0 deletions UPGRADE-4.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,23 @@ Form
},
```

* 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

throw an exception. Use a `callable` instead.

Before:

```php
'choice_attr' => 'htmlAttributes',
```

After:

```php
'choice_attr' => function ($choice, $value, $index) {
return $choice->getHtmlAttributes();
},
```

FrameworkBundle
---------------

Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Form/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* added `getTypedExtensions`, `getTypes`, and `getTypeGuessers` to `Symfony\Component\Form\Test\FormIntegrationTestCase`
* added `FormPass`
* deprecated `choice_attr` option as array of nested arrays mapped by indexes
* deprecated `choice_attr` option as string or `PropertyPath` instance

3.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ 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.

if ($attr instanceof PropertyPath) {
$attr = function ($choice) use ($accessor, $attr) {
return $accessor->getValue($choice, $attr);
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use Symfony\Component\Form\Util\FormUtil;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\PropertyAccess\PropertyPath;

class ChoiceType extends AbstractType
{
Expand Down Expand Up @@ -320,6 +321,14 @@ public function configureOptions(OptionsResolver $resolver)
return $choiceTranslationDomain;
};

$choiceAttrNormalizer = function (Options $options, $choiceAttr) {
if (is_string($choiceAttr) || $choiceAttr instanceof PropertyPath) {
@trigger_error(sprintf('Using "choice_attr" option as a string property path or a "%s" instance is deprecated since version 3.3 and will throw an exception in 4.0. Use a callable instead.', PropertyPath::class), E_USER_DEPRECATED);
}

return $choiceAttr;
};

$resolver->setDefaults(array(
'multiple' => false,
'expanded' => false,
Expand All @@ -346,6 +355,7 @@ public function configureOptions(OptionsResolver $resolver)
$resolver->setNormalizer('placeholder', $placeholderNormalizer);
$resolver->setNormalizer('choice_translation_domain', $choiceTranslationDomainNormalizer);
$resolver->setNormalizer('choices_as_values', $choicesAsValuesNormalizer);
$resolver->setNormalizer('choice_attr', $choiceAttrNormalizer);

$resolver->setAllowedTypes('choices', array('null', 'array', '\Traversable'));
$resolver->setAllowedTypes('choice_translation_domain', array('null', 'bool', 'string'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ public function testCreateViewAssumeNullIfGroupsPropertyPathUnreadable()
));
}

/**
* @group legacy
*/
public function testCreateViewAttrAsPropertyPath()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
Expand Down Expand Up @@ -460,6 +463,9 @@ public function testCreateViewAttrAsPropertyPathWithCallableString()
));
}

/**
* @group legacy
*/
public function testCreateViewAttrAsPropertyPathInstance()
{
$list = $this->getMockBuilder('Symfony\Component\Form\ChoiceList\ChoiceListInterface')->getMock();
Expand Down
0