|
@@ 39-58 (lines=20) @@
|
| 36 |
|
* |
| 37 |
|
* @see \Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface::loadValuesForChoices() |
| 38 |
|
*/ |
| 39 |
|
public function loadValuesForChoices(array $choices, $value = null) { |
| 40 |
|
// is called on form creat with $choices containing the preset of the bound entity |
| 41 |
|
$values = array(); |
| 42 |
|
foreach($choices as $key => $choice) { |
| 43 |
|
// we use a DataTransformer, thus only plain values arrive as choices which can be used directly as value |
| 44 |
|
if(is_callable($value)) { |
| 45 |
|
$values[$key] = (string)call_user_func($value, $choice, $key); |
| 46 |
|
} else { |
| 47 |
|
$values[$key] = $choice; |
| 48 |
|
} |
| 49 |
|
} |
| 50 |
|
|
| 51 |
|
// this has to be done by yourself: array( label => value ) |
| 52 |
|
// $labeledValues = MyLabelService::getLabels($values); |
| 53 |
|
|
| 54 |
|
// // create internal choice list from loaded values |
| 55 |
|
// $this->choiceList = new ArrayChoiceList($labeledValues, $value); |
| 56 |
|
|
| 57 |
|
return $values; |
| 58 |
|
} |
| 59 |
|
|
| 60 |
|
/** |
| 61 |
|
* |
|
@@ 85-105 (lines=21) @@
|
| 82 |
|
* @see \Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface::loadChoicesForValues() |
| 83 |
|
* |
| 84 |
|
*/ |
| 85 |
|
public function loadChoicesForValues(array $values, $value = null) { |
| 86 |
|
// is called on form submit after loadValuesForChoices of form create and loadChoiceList of form view create |
| 87 |
|
$choices = array(); |
| 88 |
|
foreach($values as $key => $val) { |
| 89 |
|
// we use a DataTransformer, thus only plain values arrive as choices which can be used directly as value |
| 90 |
|
if(is_callable($value)) { |
| 91 |
|
$choices[$key] = (string)call_user_func($value, $val, $key); |
| 92 |
|
} else { |
| 93 |
|
$choices[$key] = $val; |
| 94 |
|
} |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
// dump($values); |
| 98 |
|
// this has to be done by yourself: array( label => value ) |
| 99 |
|
// $labeledValues = MyLabelService::getLabels($values); |
| 100 |
|
|
| 101 |
|
// // reset internal choice list |
| 102 |
|
// $this->choiceList = new ArrayChoiceList($labeledValues, $value); |
| 103 |
|
|
| 104 |
|
return $choices; |
| 105 |
|
} |
| 106 |
|
} |