Skip to content

Commit 3be5ff1

Browse files
author
Thomas Jarrand
authored
Merge pull request #35 from Elao/placeholder
Support nested keys + default support for placeholder
2 parents 293ca4a + 11c5f61 commit 3be5ff1

3 files changed

Lines changed: 40 additions & 3 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function getConfigTreeBuilder()
4848
[
4949
'label' => 'label',
5050
'help' => 'help',
51+
'[attr][placeholder]' => 'placeholder',
5152
]
5253
)
5354
)
@@ -64,6 +65,7 @@ public function getConfigTreeBuilder()
6465
$this->addKeysConfig(
6566
'choice',
6667
[
68+
'placeholder' => 'placeholder',
6769
'empty_value' => 'empty_value',
6870
]
6971
)

Form/Extension/TreeAwareExtension.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\Form\AbstractTypeExtension;
1616
use Symfony\Component\Form\FormInterface;
1717
use Symfony\Component\Form\FormView;
18+
use Symfony\Component\PropertyAccess\PropertyAccess;
19+
use Symfony\Component\PropertyAccess\PropertyAccessor;
1820

1921
/**
2022
* Tree Aware Extension
@@ -56,6 +58,11 @@ abstract class TreeAwareExtension extends AbstractTypeExtension
5658
*/
5759
protected $defaultTranslationDomain;
5860

61+
/**
62+
* @var PropertyAccessor
63+
*/
64+
protected $propertyAccessor;
65+
5966
/**
6067
* Enable or disable automatic generation of missing labels
6168
*
@@ -113,7 +120,7 @@ public function finishView(FormView $view, FormInterface $form, array $options)
113120
{
114121
if ($this->treeBuilder && $this->keyBuilder) {
115122
foreach ($this->keys as $key => $value) {
116-
if (isset($options[$key]) && $options[$key] === true) {
123+
if ($this->optionEquals($options, $key, true)) {
117124
$this->generateKey($view, $key, $value);
118125
}
119126
}
@@ -133,6 +140,33 @@ protected function generateKey(FormView &$view, $key, $value)
133140
$view->vars['tree'] = $this->treeBuilder->getTree($view);
134141
}
135142

136-
$view->vars[$key] = $this->keyBuilder->buildKeyFromTree($view->vars['tree'], $value);
143+
$this->setVar($view->vars, $key, $this->keyBuilder->buildKeyFromTree($view->vars['tree'], $value));
144+
}
145+
146+
protected function setVar(array &$vars, string $key, $value): void
147+
{
148+
if ($this->getPropertyAccessor()->isWritable($vars, $key)) {
149+
$this->getPropertyAccessor()->setValue($vars, $key, $value);
150+
} else {
151+
$vars[$key] = $value;
152+
}
153+
}
154+
155+
protected function optionEquals(array $options, string $key, $value): bool
156+
{
157+
if ($this->getPropertyAccessor()->isReadable($options, $key)) {
158+
return $this->getPropertyAccessor()->getValue($options, $key) === $value;
159+
}
160+
161+
return isset($options[$key]) ? $options[$key] === $value : false;
162+
}
163+
164+
protected function getPropertyAccessor(): PropertyAccessor
165+
{
166+
if (!$this->propertyAccessor) {
167+
$this->propertyAccessor = PropertyAccess::createPropertyAccessor();
168+
}
169+
170+
return $this->propertyAccessor;
137171
}
138172
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
"require": {
1919
"php": "^7.1.0",
2020
"symfony/framework-bundle": "~2.8|~3.0|~4.0|~5.0",
21-
"symfony/form": "~2.8|~3.0|~4.0|~5.0"
21+
"symfony/form": "~2.8|~3.0|~4.0|~5.0",
22+
"symfony/property-access": "~2.8|~3.0|~4.0|~5.0"
2223
},
2324
"require-dev": {
2425
"symfony/phpunit-bridge": "^5.0",

0 commit comments

Comments
 (0)