Skip to content

[Form] Make ButtonType handle form_attr option #48801

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
Dec 28, 2022
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
13 changes: 12 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/BaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use Symfony\Component\Form\AbstractRendererEngine;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
Expand Down Expand Up @@ -70,8 +71,16 @@ public function buildView(FormView $view, FormInterface $form, array $options)
if (!$labelFormat) {
$labelFormat = $view->parent->vars['label_format'];
}

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

Expand Down Expand Up @@ -137,13 +146,15 @@ public function configureOptions(OptionsResolver $resolver)
'translation_domain' => null,
'auto_initialize' => true,
'priority' => 0,
'form_attr' => false,
]);

$resolver->setAllowedTypes('block_prefix', ['null', 'string']);
$resolver->setAllowedTypes('attr', 'array');
$resolver->setAllowedTypes('row_attr', 'array');
$resolver->setAllowedTypes('label_html', 'bool');
$resolver->setAllowedTypes('priority', 'int');
$resolver->setAllowedTypes('form_attr', ['bool', 'string']);

$resolver->setInfo('priority', 'The form rendering priority (higher priorities will be rendered first)');
}
Expand Down
12 changes: 0 additions & 12 deletions src/Symfony/Component/Form/Extension/Core/Type/FormType.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,16 +97,6 @@ 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 @@ -221,7 +211,6 @@ public function configureOptions(OptionsResolver $resolver)
'is_empty_callback' => null,
'getter' => null,
'setter' => null,
'form_attr' => false,
]);

$resolver->setAllowedTypes('label_attr', 'array');
Expand All @@ -233,7 +222,6 @@ 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 @@ -234,6 +234,7 @@ public function provideCompletionSuggestions(): iterable
'translation_domain',
'auto_initialize',
'priority',
'form_attr',
],
];

Expand All @@ -253,6 +254,7 @@ public function provideCompletionSuggestions(): iterable
'translation_domain',
'auto_initialize',
'priority',
'form_attr',
],
];

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

use Symfony\Component\Form\Button;
use Symfony\Component\Form\Exception\BadMethodCallException;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Extension\Core\Type\FormType;

/**
* @author Bernhard Schussek <bschussek@gmail.com>
Expand All @@ -36,4 +38,65 @@ public function testSubmitNullUsesDefaultEmptyData($emptyData = 'empty', $expect
$this->expectExceptionMessage('Buttons do not support empty data.');
parent::testSubmitNullUsesDefaultEmptyData($emptyData, $expectedData);
}

public function testFormAttrOnRoot()
{
$view = $this->factory
->createNamedBuilder('parent', FormType::class, 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')
->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('', FormType::class, null, [
'form_attr' => true,
])
->add('child1', $this->getTestedType())
->add('child2', $this->getTestedType())
->getForm()
->createView();
}

public function testFormAttrAsStringWithNoId()
{
$stringId = 'custom-identifier';
$view = $this->factory
->createNamedBuilder('', FormType::class, 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']);
}
}