-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Add AsFormType
attribute to create FormType directly on model classes
#60563
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
base: 7.4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -82,6 +82,7 @@ | |||||||||||||||||||||||||
use Symfony\Component\Filesystem\Filesystem; | ||||||||||||||||||||||||||
use Symfony\Component\Finder\Finder; | ||||||||||||||||||||||||||
use Symfony\Component\Finder\Glob; | ||||||||||||||||||||||||||
use Symfony\Component\Form\Attribute\AsFormType; | ||||||||||||||||||||||||||
use Symfony\Component\Form\Extension\HtmlSanitizer\Type\TextTypeHtmlSanitizerExtension; | ||||||||||||||||||||||||||
use Symfony\Component\Form\Form; | ||||||||||||||||||||||||||
use Symfony\Component\Form\FormTypeExtensionInterface; | ||||||||||||||||||||||||||
|
@@ -891,6 +892,18 @@ private function registerFormConfiguration(array $config, ContainerBuilder $cont | |||||||||||||||||||||||||
$container->setParameter('form.type_extension.csrf.enabled', false); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if ($config['form']['use_attribute']) { | ||||||||||||||||||||||||||
$loader->load('form_metadata.php'); | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
$container->registerAttributeForAutoconfiguration(AsFormType::class, static function (ChildDefinition $definition, AsFormType $attribute, \ReflectionClass $ref) { | ||||||||||||||||||||||||||
$definition | ||||||||||||||||||||||||||
->addTag('container.excluded.form.metadata.form_type', ['class_name' => $ref->getName()]) | ||||||||||||||||||||||||||
->addTag('container.excluded') | ||||||||||||||||||||||||||
->setAbstract(true) | ||||||||||||||||||||||||||
; | ||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
Comment on lines
+898
to
+905
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new As of now, have not tag name prefixed with I think reading the class name from the service definition in the compiler pass is more resilient. It is also easier to tag a service "manually" without repeating the class name in the tag if someone don't use autoconfiguration.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if (!ContainerBuilder::willBeAvailable('symfony/translation', Translator::class, ['symfony/framework-bundle', 'symfony/form'])) { | ||||||||||||||||||||||||||
$container->removeDefinition('form.type_extension.upload.validator'); | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
||
use Symfony\Component\Form\Extension\Metadata\MetadataExtension; | ||
use Symfony\Component\Form\Metadata\Loader\AttributeLoader; | ||
|
||
return static function (ContainerConfigurator $container) { | ||
$container->services() | ||
->set('form.metadata.attribute_loader', AttributeLoader::class) | ||
|
||
->alias('form.metadata.default_loader', 'form.metadata.attribute_loader') | ||
; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Attribute; | ||
|
||
/** | ||
* Register a model class (e.g. DTO, entity, model, etc...) as a FormType. | ||
* | ||
* @author Benjamin Georgeault <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final readonly class AsFormType | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We have many examples of extending attribute classes to set some repeated default options. It would make sense to remove |
||
{ | ||
/** | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function __construct( | ||
private array $options = [], | ||
) { | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Attribute; | ||
|
||
/** | ||
* Add an AsFormType class property as a FormType's field. | ||
* | ||
* @author Benjamin Georgeault <[email protected]> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
final readonly class Type | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To stay consistent with the purpose of this attribute, as described in the class description, it represents a form field, so I’d prefer to name this class There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe a description like this is more clear and accurate:
IMHO, the term And agreed with @Neirda24 to keep it |
||
{ | ||
/** | ||
* @param class-string|null $type the FormType class name to use for this field | ||
* @param array<string, mixed> $options your form options | ||
* @param string|null $name change the form view field's name | ||
*/ | ||
public function __construct( | ||
private ?string $type = null, | ||
private array $options = [], | ||
GromNaN marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private ?string $name = null, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
/** | ||
* @return class-string|null | ||
*/ | ||
public function getType(): ?string | ||
{ | ||
return $this->type; | ||
} | ||
|
||
public function getName(): ?string | ||
{ | ||
return $this->name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -17,8 +17,11 @@ | |||||||||||||
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait; | ||||||||||||||
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass; | ||||||||||||||
use Symfony\Component\DependencyInjection\ContainerBuilder; | ||||||||||||||
use Symfony\Component\DependencyInjection\Definition; | ||||||||||||||
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||||||||||||||
use Symfony\Component\DependencyInjection\Reference; | ||||||||||||||
use Symfony\Component\Form\Extension\Metadata\Type\MetadataType; | ||||||||||||||
use Symfony\Component\Form\Metadata\FormMetadataInterface; | ||||||||||||||
|
||||||||||||||
/** | ||||||||||||||
* Adds all services with the tags "form.type", "form.type_extension" and | ||||||||||||||
|
@@ -46,6 +49,7 @@ private function processFormTypes(ContainerBuilder $container): Reference | |||||||||||||
{ | ||||||||||||||
// Get service locator argument | ||||||||||||||
$servicesMap = []; | ||||||||||||||
$metadataTypeMap = []; | ||||||||||||||
$namespaces = ['Symfony\Component\Form\Extension\Core\Type' => true]; | ||||||||||||||
$csrfTokenIds = []; | ||||||||||||||
|
||||||||||||||
|
@@ -61,10 +65,33 @@ private function processFormTypes(ContainerBuilder $container): Reference | |||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
foreach ($container->findTaggedResourceIds('container.excluded.form.metadata.form_type') as $excludedServiceId => $tag) { | ||||||||||||||
if (!isset($tag[0]['class_name'])) { | ||||||||||||||
throw new InvalidArgumentException(\sprintf('The excluded service "%s" with tag "container.excluded.form.metadata.form_type" must have the tag\'s attribute "class_name" set.', $excludedServiceId)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
$className = $tag[0]['class_name']; | ||||||||||||||
Comment on lines
+69
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here, you can get the class name from the service definition.
Suggested change
|
||||||||||||||
$formTypeId = $excludedServiceId.'.form_type'; | ||||||||||||||
$metadataId = $excludedServiceId.'.metadata'; | ||||||||||||||
|
||||||||||||||
$container->setDefinition($metadataId, new Definition(FormMetadataInterface::class)) | ||||||||||||||
->setFactory([new Reference('form.metadata.default_loader'), 'load']) | ||||||||||||||
->addArgument($className) | ||||||||||||||
->addTag('form.metadata'); | ||||||||||||||
|
||||||||||||||
$container->setDefinition($formTypeId, new Definition(MetadataType::class)) | ||||||||||||||
->addArgument(new Reference($metadataId)) | ||||||||||||||
->addTag('form.metadata_type', ['class_name' => $className]); | ||||||||||||||
|
||||||||||||||
$metadataTypeMap[$className] = new Reference($formTypeId); | ||||||||||||||
$namespaces[substr($className, 0, strrpos($className, '\\'))] = true; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if ($container->hasDefinition('console.command.form_debug')) { | ||||||||||||||
$commandDefinition = $container->getDefinition('console.command.form_debug'); | ||||||||||||||
$commandDefinition->setArgument(1, array_keys($namespaces)); | ||||||||||||||
$commandDefinition->setArgument(2, array_keys($servicesMap)); | ||||||||||||||
$commandDefinition->setArgument(6, array_keys($metadataTypeMap)); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if ($csrfTokenIds && $container->hasDefinition('form.type_extension.csrf')) { | ||||||||||||||
|
@@ -75,7 +102,7 @@ private function processFormTypes(ContainerBuilder $container): Reference | |||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
return ServiceLocatorTagPass::register($container, $servicesMap); | ||||||||||||||
return ServiceLocatorTagPass::register($container, [...$servicesMap, ...$metadataTypeMap]); | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private function processFormTypeExtensions(ContainerBuilder $container): array | ||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Exception; | ||
|
||
/** | ||
* Thrown when an error occurred during Metadata creation. | ||
* | ||
* @author Benjamin Georgeault <[email protected]> | ||
*/ | ||
class MetadataException extends LogicException | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,7 +27,7 @@ public function extractConfiguration(FormInterface $form): array | |
$data = [ | ||
'id' => $this->buildId($form), | ||
'name' => $form->getName(), | ||
'type_class' => $form->getConfig()->getType()->getInnerType()::class, | ||
'type_class' => $form->getConfig()->getType()->getInnerType()->getClassName(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can check if the inner type implements the new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (#60563 (comment)) Oh, I see... that was the current case. But then, what is There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's overriden in |
||
'synchronized' => $form->isSynchronized(), | ||
'passed_options' => [], | ||
'resolved_options' => [], | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Form\Extension\Metadata; | ||
|
||
use Symfony\Component\Form\Exception\InvalidArgumentException; | ||
use Symfony\Component\Form\Exception\MetadataException; | ||
use Symfony\Component\Form\Extension\Metadata\Type\MetadataType; | ||
use Symfony\Component\Form\FormExtensionInterface; | ||
use Symfony\Component\Form\FormTypeGuesserInterface; | ||
use Symfony\Component\Form\FormTypeInterface; | ||
use Symfony\Component\Form\Metadata\Loader\LoaderInterface; | ||
|
||
/** | ||
* Responsible for instantiating FormType based on a {@see \Symfony\Component\Form\Metadata\FormMetadataInterface}. | ||
* | ||
* @author Benjamin Georgeault <[email protected]> | ||
*/ | ||
final class MetadataExtension implements FormExtensionInterface | ||
{ | ||
/** | ||
* @var array<class-string, FormTypeInterface> | ||
*/ | ||
private array $loadedTypes = []; | ||
|
||
public function __construct( | ||
private readonly LoaderInterface $loader, | ||
) { | ||
} | ||
|
||
public function getType(string $name): FormTypeInterface | ||
{ | ||
if (null !== $type = $this->loadedTypes[$name] ?? null) { | ||
return $type; | ||
} | ||
|
||
try { | ||
return $this->loadedTypes[$name] = new MetadataType($this->loader->load($name)); | ||
} catch (MetadataException $e) { | ||
throw new InvalidArgumentException(\sprintf('Cannot instantiate a "%s" for the given class "%s".', FormTypeInterface::class, $name), previous: $e); | ||
} | ||
} | ||
|
||
public function hasType(string $name): bool | ||
{ | ||
return ($this->loadedTypes[$name] ?? false) || $this->loader->support($name); | ||
} | ||
|
||
public function getTypeExtensions(string $name): array | ||
{ | ||
return []; | ||
} | ||
|
||
public function hasTypeExtensions(string $name): bool | ||
{ | ||
return false; | ||
} | ||
|
||
public function getTypeGuesser(): ?FormTypeGuesserInterface | ||
{ | ||
return null; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using attributes is already opt-in as you have to add the attribute to a class that is autoconfigured. I understand the benefit of this new configuration in avoiding the declaration of unnecessary services, but it would be simpler for users of the framework if they did not need to activate this option.
Unnecessary service (
form.metadata.attribute_loader
) can be removed in the compiler pass.