-
-
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
Open
WedgeSama
wants to merge
1
commit into
symfony:7.4
Choose a base branch
from
WedgeSama:form-attribute
base: 7.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
src/Symfony/Bundle/FrameworkBundle/Resources/config/form_metadata.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <fabien@symfony.com> | ||
* | ||
* 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') | ||
; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <fabien@symfony.com> | ||
* | ||
* 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 <git@wedgesama.fr> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_CLASS)] | ||
final readonly class AsFormType | ||
{ | ||
/** | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function __construct( | ||
private array $options = [], | ||
) { | ||
} | ||
|
||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* 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 <git@wedgesama.fr> | ||
*/ | ||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
final readonly class Type | ||
{ | ||
/** | ||
* @param class-string|null $type | ||
* @param array<string, mixed> $options | ||
*/ | ||
public function __construct( | ||
private ?string $type = null, | ||
private array $options = [], | ||
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. What about also adding |
||
) { | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function getOptions(): array | ||
{ | ||
return $this->options; | ||
} | ||
|
||
/** | ||
* @return class-string|null | ||
*/ | ||
public function getType(): ?string | ||
{ | ||
return $this->type; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/Symfony/Component/Form/Exception/MetadataException.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <fabien@symfony.com> | ||
* | ||
* 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 <git@wedgesama.fr> | ||
*/ | ||
class MetadataException extends LogicException | ||
{ | ||
} |
71 changes: 71 additions & 0 deletions
71
src/Symfony/Component/Form/Extension/Metadata/MetadataExtension.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <fabien@symfony.com> | ||
* | ||
* 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 <git@wedgesama.fr> | ||
*/ | ||
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; | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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
Field
?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.
I think
Field
is wrong. Because it's biased by how it displays. But a "Field" could be an entire form. so I think we should keepType
but maybe rename other occurences of Field's to Type's. WDYT ?