Skip to content

[Form] Add "form_attr" FormType option #40430

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

Merged
merged 1 commit into from
Mar 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,16 @@ public function buildView(FormView $view, FormInterface $form, array $options)
}

$helpTranslationParameters = array_merge($view->parent->vars['help_translation_parameters'], $helpTranslationParameters);

$rootFormAttrOption = $form->getRoot()->getConfig()->getOption('form_attr');
if ($options['form_attr'] || $rootFormAttrOption) {
$view->vars['attr']['form'] = \is_string($rootFormAttrOption) ? $rootFormAttrOption : $form->getRoot()->getName();
if (empty($view->vars['attr']['form'])) {
throw new LogicException('"form_attr" option must be a string identifier on root form when it has no id.');
}
}
} elseif (\is_string($options['form_attr'])) {
$view->vars['id'] = $options['form_attr'];
}

$formConfig = $form->getConfig();
Expand Down Expand Up @@ -210,6 +220,7 @@ public function configureOptions(OptionsResolver $resolver)
'is_empty_callback' => null,
'getter' => null,
'setter' => null,
'form_attr' => false,
]);

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

$resolver->setInfo('getter', 'A callable that accepts two arguments (the view data and the current form field) and must return a value.');
$resolver->setInfo('setter', 'A callable that accepts three arguments (a reference to the view data, the submitted value and the current form field).');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\CallbackTransformer;
use Symfony\Component\Form\DataMapperInterface;
use Symfony\Component\Form\Exception\InvalidArgumentException;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Exception\TransformationFailedException;
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
use Symfony\Component\Form\Extension\Core\Type\FormType;
Expand Down Expand Up @@ -774,6 +775,67 @@ public function testErrorBubblingDoesNotSkipCompoundFieldsWithInheritDataConfigu
$this->assertSame($error, $form->get('inherit_data_type')->getErrors()[0]);
$this->assertCount(0, $form->get('inherit_data_type')->get('child')->getErrors());
}

public function testFormAttrOnRoot()
{
$view = $this->factory
->createNamedBuilder('parent', self::TESTED_TYPE, null, [
'form_attr' => true,
])
->add('child1', $this->getTestedType())
->add('child2', $this->getTestedType())
->getForm()
->createView();
$this->assertArrayNotHasKey('form', $view->vars['attr']);
$this->assertSame($view->vars['id'], $view['child1']->vars['attr']['form']);
$this->assertSame($view->vars['id'], $view['child2']->vars['attr']['form']);
}

public function testFormAttrOnChild()
{
$view = $this->factory
->createNamedBuilder('parent', self::TESTED_TYPE)
->add('child1', $this->getTestedType(), [
'form_attr' => true,
])
->add('child2', $this->getTestedType())
->getForm()
->createView();
$this->assertArrayNotHasKey('form', $view->vars['attr']);
$this->assertSame($view->vars['id'], $view['child1']->vars['attr']['form']);
$this->assertArrayNotHasKey('form', $view['child2']->vars['attr']);
}

public function testFormAttrAsBoolWithNoId()
{
$this->expectException(LogicException::class);
$this->expectErrorMessage('form_attr');
$this->factory
->createNamedBuilder('', self::TESTED_TYPE, null, [
'form_attr' => true,
])
->add('child1', $this->getTestedType())
->add('child2', $this->getTestedType())
->getForm()
->createView();
}

public function testFormAttrAsStringWithNoId()
{
$stringId = 'custom-identifier';
$view = $this->factory
->createNamedBuilder('', self::TESTED_TYPE, null, [
'form_attr' => $stringId,
])
->add('child1', $this->getTestedType())
->add('child2', $this->getTestedType())
->getForm()
->createView();
$this->assertArrayNotHasKey('form', $view->vars['attr']);
$this->assertSame($stringId, $view->vars['id']);
$this->assertSame($view->vars['id'], $view['child1']->vars['attr']['form']);
$this->assertSame($view->vars['id'], $view['child2']->vars['attr']['form']);
}
}

class Money
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"by_reference",
"data",
"disabled",
"form_attr",
"getter",
"help",
"help_attr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ Symfony\Component\Form\Extension\Core\Type\ChoiceType (Block prefix: "choice")
expanded by_reference
group_by data
multiple disabled
placeholder getter
preferred_choices help
placeholder form_attr
preferred_choices getter
help
help_attr
help_html
help_translation_parameters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"disabled",
"empty_data",
"error_bubbling",
"form_attr",
"getter",
"help",
"help_attr",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Symfony\Component\Form\Extension\Core\Type\FormType (Block prefix: "form")
disabled
empty_data
error_bubbling
form_attr
getter
help
help_attr
Expand Down