8000 [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
Next Next commit
[Form] deprecated choice_attr as nested arrays mapped by indexes
  • Load 8000 ing branch information
HeahDude committed Mar 26, 2017
commit e5cc7cd12648e403c8a87bae3a0ba90a7ead4101
49 changes: 49 additions & 0 deletions UPGRADE-3.3.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,55 @@ Finder

* The `ExceptionInterface` has been deprecated and will be removed in 4.0.

Form
----

* Using `choice_attr` option as an array of nested arrays has been deprecated
and indexes will be considered as attributes in 4.0. Use a unique array for
all choices or a `callable` instead.

Before:

```php
// Single array for all choices using callable
'choice_attr' => function () {
return array('class' => 'choice-options');
},

// Different arrays per choices using array
'choices' => array(
'Yes' => true,
'No' => false,
'Maybe' => null,
'choice_attr' => array(
'Yes' => array('class' => 'option-green'),
'No' => array('class' => 'option-red'),
),
```

After:

```php
// Single array for all choices using array
'choice_attr' => array('class' => 'choice-options'),

// Different arrays per choices using callable
'choices' => array(
'Yes' => true,
'No' => false,
'Maybe' => null,
'choice_attr' => function ($choice, $index, $value) {
if ('Yes' === $index) {
return array('class' => 'option-green');
}
if ('No' === $index) {
return array('class' => 'option-red');
}

return array();
},
```

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

Expand Down
46 changes: 46 additions & 0 deletions UPGRADE-4.0.md
8000
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,52 @@ Form
}
```

* Usage of `choice_attr` option as an array of nested arrays has been removed
and indexes will be considered as attributes in 4.0. Use a unique array for
all choices or a `callable` instead.

Before:

```php
// Single array for all choices using callable
'choice_attr' => function () {
return array('class' => 'choice-options');
},

// Different arrays per choices using array
'choices' => array(
'Yes' => true,
'No' => false,
'Maybe' => null,
'choice_attr' => array(
'Yes' => array('class' => 'option-green'),
'No' => array('class' => 'option-red'),
),
```

After:

```php
// Single array for all choices using array
'choice_attr' => array('class' => 'choice-options'),

// Different arrays per choices using callable
'choices' => array(
'Yes' => true,
'No' => false,
'Maybe' => null,
'choice_attr' => function ($choice, $index, $value) {
if ('Yes' === $index) {
return array('class' => 'option-green');
}
if ('No' === $index) {
return array('class' => 'option-red');
}

return array();
},
```

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

Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"require-dev": {
"symfony/asset": "~2.8|~3.0",
"symfony/finder": "~2.8|~3.0",
"symfony/form": "^3.2.5",
"symfony/form": "~3.3",
"symfony/http-kernel": "~3.2",
"symfony/polyfill-intl-icu": "~1.0",
"symfony/routing": "~2.8|~3.0",
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 @@ -7,6 +7,7 @@ CHANGELOG
* added `Symfony\Component\Form\FormErrorIterator::findByCodes()`
* 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

3.2.0
-----
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,31 @@ private static function addChoiceView($choice, $value, $label, $keys, &$index, $
$label = false === $dynamicLabel ? false : (string) $dynamicLabel;
}

// BC
if (is_array($attr)) {
if (isset($attr[$key])) {
@trigger_error('Passing an array of arrays to the "choice_attr" option with choice keys as keys is deprecated since version 3.3 and will no longer be supported in 4.0. Use a "\Closure" instead.', E_USER_DEPRECATED);
$attr = $attr[$key];
} else {
foreach ($attr as $a) {
if (is_array($a)) {
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 give an example of when this is necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The deprecated case where an array is used and you have to nest arrays of attributes by choice indexes. Then one may not define all indexes. But we need to support the new use case too. So if a nested array exists but not for the current choice, we need to define its attributes to an empty array.

Maybe the comment is not explicit enough?

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think making this more clear in a comment would be good.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@xabbuh @fabpot this last comment is now addressed

// Using the deprecated way of choice keys as keys allows to not define all choices.
// When $attr[$key] is not set for this one but is for another we need to
// prevent using an array as HTML attribute
$attr = array();

break;
}
}
}
}

$view = new ChoiceView(
$choice,
$value,
$label,
// The attributes may be a callable or a mapping from choice indices
// to nested arrays
is_callable($attr) ? call_user_func($attr, $choice, $key, $value) : (isset($attr[$key]) ? $attr[$key] : array())
// The attributes may be a callable or an array
is_callable($attr) ? call_user_func($attr, $choice, $key, $value) : (null !== $attr ? $attr : array())
);

// $isPreferred may be null if no choices are preferred
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ public function configureOptions(OptionsResolver $resolver)
'choice_label' => null,
'choice_name' => null,
'choice_value' => null,
'choice_attr' => null,
'choice_attr' => array(),
'preferred_choices' => array(),
'group_by' => null,
'empty_data' => $emptyData,
Expand Down
Loading
0