Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 5e12bfc

Browse filesBrowse files
[Form] Add FormType form_attr option
1 parent fc016dd commit 5e12bfc
Copy full SHA for 5e12bfc

File tree

6 files changed

+79
-2
lines changed
Filter options

6 files changed

+79
-2
lines changed

‎src/Symfony/Component/Form/Extension/Core/Type/FormType.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Extension/Core/Type/FormType.php
+12Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ public function buildView(FormView $view, FormInterface $form, array $options)
9696
}
9797

9898
$helpTranslationParameters = array_merge($view->parent->vars['help_translation_parameters'], $helpTranslationParameters);
99+
100+
$rootFormAttrOption = $form->getRoot()->getConfig()->getOption('form_attr');
101+
if ($options['form_attr'] || $rootFormAttrOption) {
102+
$view->vars['attr']['form'] = \is_string($rootFormAttrOption) ? $rootFormAttrOption : $form->getRoot()->getName();
103+
if (empty($view->vars['attr']['form'])) {
104+
throw new LogicException('"form_attr" option must be a string identifier on root form when it has no id.');
105+
}
106+
}
107+
} elseif (\is_string($options['form_attr'])) {
108+
$view->vars['id'] = $options['form_attr'];
99109
}
100110

101111
$formConfig = $form->getConfig();
@@ -210,6 +220,7 @@ public function configureOptions(OptionsResolver $resolver)
210220
'is_empty_callback' => null,
211221
'getter' => null,
212222
'setter' => null,
223+
'form_attr' => false,
213224
]);
214225

215226
$resolver->setAllowedTypes('label_attr', 'array');
@@ -221,6 +232,7 @@ public function configureOptions(OptionsResolver $resolver)
221232
$resolver->setAllowedTypes('is_empty_callback', ['null', 'callable']);
222233
$resolver->setAllowedTypes('getter', ['null', 'callable']);
223234
$resolver->setAllowedTypes('setter', ['null', 'callable']);
235+
$resolver->setAllowedTypes('form_attr', ['bool', 'string']);
224236

225237
$resolver->setInfo('getter', 'A callable that accepts two arguments (the view data and the current form field) and must return a value.');
226238
$resolver->setInfo('setter', 'A callable that accepts three arguments (a reference to the view data, the submitted value and the current form field).');

‎src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Extension/Core/Type/FormTypeTest.php
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,67 @@ public function testErrorBubblingDoesNotSkipCompoundFieldsWithInheritDataConfigu
774774
$this->assertSame($error, $form->get('inherit_data_type')->getErrors()[0]);
775775
$this->assertCount(0, $form->get('inherit_data_type')->get('child')->getErrors());
776776
}
777+
778+
public function testFormAttrOnRoot()
779+
{
780+
$view = $this->factory
781+
->createNamedBuilder('parent', self::TESTED_TYPE, null, [
782+
'form_attr' => true,
783+
])
784+
->add('child1', $this->getTestedType())
785+
->add('child2', $this->getTestedType())
786+
->getForm()
787+
->createView();
788+
$this->assertArrayNotHasKey('form', $view->vars['attr']);
789+
$this->assertEquals($view->vars['id'], $view['child1']->vars['attr']['form']);
790+
$this->assertEquals($view->vars['id'], $view['child2']->vars['attr']['form']);
791+
}
792+
793+
public function testFormAttrOnChild()
794+
{
795+
$view = $this->factory
796+
->createNamedBuilder('parent', self::TESTED_TYPE)
797+
->add('child1', $this->getTestedType(), [
798+
'form_attr' => true,
799+
])
800+
->add('child2', $this->getTestedType())
801+
->getForm()
802+
->createView();
803+
$this->assertArrayNotHasKey('form', $view->vars['attr']);
804+
$this->assertEquals($view->vars['id'], $view['child1']->vars['attr']['form']);
805+
$this->assertArrayNotHasKey('form', $view['child2']->vars['attr']);
806+
}
807+
808+
public function testFormAttrAsBoolWithNoId()
809+
{
810+
$this->expectException('Symfony\Component\Form\Exception\LogicException');
811+
$this->expectErrorMessage('form_attr');
812+
$this->factory
813+
->createNamedBuilder('', self::TESTED_TYPE, null, [
814+
'form_attr' => true,
815+
])
816+
->add('child1', $this->getTestedType())
817+
->add('child2', $this->getTestedType())
818+
->getForm()
819+
->createView();
820+
}
821+
822+
public function testFormAttrAsStringWithNoId()
823+
{
824+
$stringId = 'custom-identifier';
825+
$view = $this->factory
826+
->createNamedBuilder('', self::TESTED_TYPE, null, [
827+
'form_attr' => $stringId,
828+
])
829+
->add('child1', $this->getTestedType())
830+
->add('child2', $this->getTestedType())
831+
->getForm()
832+
->createView();
833+
$this->assertArrayNotHasKey('form', $view->vars['attr']);
834+
$this->assertEquals($stringId, $view->vars['id']);
835+
$this->assertEquals($view->vars['id'], $view['child1']->vars['attr']['form']);
836+
$this->assertEquals($view->vars['id'], $view['child2']->vars['attr']['form']);
837+
}
777838
}
778839

779840
class Money

‎src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
"by_reference",
4141
"data",
4242
"disabled",
43+
"form_attr",
4344
"getter",
4445
"help",
4546
"help_attr",

‎src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.txt

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_1.txt
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
1717
expanded by_reference
1818
group_by data
1919
multiple disabled
20-
placeholder getter
21-
preferred_choices help
20+
placeholder form_attr
21+
preferred_choices getter
22+
help
2223
help_attr
2324
help_html
2425
help_translation_parameters

‎src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"disabled",
1818
"empty_data",
1919
"error_bubbling",
20+
"form_attr",
2021
"getter",
2122
"help",
2223
"help_attr",

‎src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.txt

Copy file name to clipboardExpand all lines: src/Symfony/Component/Form/Tests/Fixtures/Descriptor/resolved_form_type_2.txt
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")
1919
disabled
2020
empty_data
2121
error_bubbling
22+
form_attr
2223
getter
2324
help
2425
help_attr

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.