8000 [Form] Allow choices with duplicated content by ewgRa · Pull Request #16436 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Form] Allow choices with duplicated content #16436

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 12 commits into from
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
refactoring
  • Loading branch information
ewgRa committed Nov 3, 2015
commit 6d15ee7c54d9319ee6c967136985478a36030a8d
25 changes: 22 additions & 3 deletions src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class ArrayChoiceList implements ChoiceListInterface
*/
protected $valueCallback;

protected $rawChoices;

protected $rawKeys;


/**
* Creates a list with the given choices and values.
*
Expand Down Expand Up @@ -88,10 +93,12 @@ public function __construct($choices, $value = null)
// If the choices are given as recursive array (i.e. with explicit
// choice groups), flatten the array. The grouping information is needed
// in the view only.
$this->flatten($choices, $value, $choicesByValues, $keysByValues, $structuredValues);
$this->flatten($choices, $value, $choicesByValues, $rawChoices, $keysByValues, $rawKeys, $structuredValues);

$this->choices = $choicesByValues;
$this->rawChoices = $rawChoices;
$this->originalKeys = $keysByValues;
$this->rawKeys = $rawKeys;
$this->structuredValues = $structuredValues;
}

Expand Down Expand Up @@ -127,6 +134,16 @@ public function getOriginalKeys()
return $this->originalKeys;
}

public function getRawChoices()
{
return $this->rawChoices;
}

public function getRawKeys()
{
return $this->rawKeys;
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -186,7 +203,7 @@ public function getValuesForChoices(array $choices)
*
* @internal Must not be used by user-land code
*/
protected function flatten(array $choices, $value, &$choicesByValues, &$keysByValues, &$structuredValues)
protected function flatten(array $choices, $value, &$choicesByValues, &$rawChoices, &$keysByValues, &$rawKeys, &$structuredValues)
{
if (null === $choicesByValues) {
$choicesByValues = array();
Expand All @@ -196,14 +213,16 @@ protected function flatten(array $choices, $value, &$choicesByValues, &$keysByVa

foreach ($choices as $key => $choice) {
if (is_array($choice)) {
$this->flatten($choice, $value, $choicesByValues, $keysByValues, $structuredValues[$key]);
$this->flatten($choice, $value, $choicesByValues, $rawChoices[$key], $keysByValues, $rawKeys[$key], $structuredValues[$key]);

continue;
}

$choiceValue = (string) call_user_func($value, $choice);
$choicesByValues[$choiceValue] = $choice;
$rawChoices[$key] = $choiceValue;
$keysByValues[$choiceValue] = $key;
$rawKeys[$key] = $key;
$structuredValues[$key] = $choiceValue;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/Symfony/Component/Form/ChoiceList/ArrayKeyChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,24 +162,28 @@ public function getValuesForChoices(array $choices)
*
* @internal Must not be used by user-land code
*/
protected function flatten(array $choices, $value, &$choicesByValues, &$keysByValues, &$structuredValues)
protected function flatten(array $choices, $value, &$choicesByValues, &$rawChoices, &$keysByValues, &$rawKeys, &$structuredValues)
{
if (null === $choicesByValues) {
$choicesByValues = array();
$keysByValues = array();
$structuredValues = array();
$rawChoices = array();
$rawKeys = array();
}

foreach ($choices as $choice => $key) {
if (is_array($key)) {
$this->flatten($key, $value, $choicesByValues, $keysByValues, $structuredValues[$choice]);
$this->flatten($key, $value, $choicesByValues, $rawChoices[$choice], $keysByValues, $rawKeys[$choice], $structuredValues[$choice]);

continue;
}

$choiceValue = (string) call_user_func($value, $choice);
$choicesByValues[$choiceValue] = $choice;
$rawChoices[$choiceValue] = $key;
$keysByValues[$choiceValue] = $key;
$rawKeys[$choiceValue] = $choiceValue;
$structuredValues[$key] = $choiceValue;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,10 @@ public function createView(ChoiceListInterface $list, $preferredChoices = null,
} else {
// Otherwise use the original structure of the choices
self::addStructuredChoiceViews(
$list->getStructuredValues(),
$list->getRawChoices(),
$label,
$choices,
$keys,
$list->getRawKeys(),
$index,
$attr,
$preferredChoices,
Expand Down Expand Up @@ -188,7 +188,7 @@ private static function addStructuredChoiceViews($structuredValues, $label, $cho
$value,
$label,
$choices,
$keys,
$keys[$key],
$index,
$attr,
$isPreferred,
Expand All @@ -209,10 +209,10 @@ private static function addStructuredChoiceViews($structuredValues, $label, $cho

// Add ungrouped items directly
self::addChoiceView(
$choices[$value],
$choices[$structuredValues[$key]],
$value,
$label,
$keys[$value],
$key,
$index,
$attr,
$isPreferred,
Expand Down
0