Skip to content

[Form] Add form type guesser for EnumType #61297

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
wants to merge 1 commit into
base: 7.4
Choose a base branch
from

Conversation

mttsch
Copy link
Contributor

@mttsch mttsch commented Aug 1, 2025

Q A
Branch? 7.4
Bug fix? no
New feature? yes
Deprecations? no
Issues Fix #61272
License MIT

This new form type guesser guesses the form type of an enum property and also sets the class option of the EnumType to the relevant enum name.

@mttsch mttsch force-pushed the feature/61272-form-enum-type-guesser branch 4 times, most recently from 30ce63c to 0796aa4 Compare August 1, 2025 16:27
@mttsch

This comment was marked as outdated.

@mttsch mttsch force-pushed the feature/61272-form-enum-type-guesser branch 2 times, most recently from 80d176c to 78a2346 Compare August 3, 2025 03:40
@mttsch mttsch force-pushed the feature/61272-form-enum-type-guesser branch 2 times, most recently from 1fbb523 to a5eebb2 Compare August 5, 2025 09:53

namespace Symfony\Component\Form\Tests\Fixtures;

final class EnumFormTypeGuesserCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why final?

use Symfony\Component\Form\Tests\Fixtures\EnumFormTypeGuesserCase;
use Symfony\Component\Form\Tests\Fixtures\EnumFormTypeGuesserCaseEnum;

final class EnumFormTypeGuesserTest extends TestCase
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final is pure visual overhead ;)

Suggested change
final class EnumFormTypeGuesserTest extends TestCase
class EnumFormTypeGuesserTest extends TestCase


public function guessType(string $class, string $property): ?TypeGuess
{
if (null === ($propertyType = $this->getPropertyType($class, $property))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (null === ($propertyType = $this->getPropertyType($class, $property))) {
if (!$propertyType = $this->getPropertyType($class, $property)) {


private function getPropertyType(string $class, string $property): ?\ReflectionNamedType
{
if (isset($this->cache[$class]) && \array_key_exists($property, $this->cache[$class])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (isset($this->cache[$class]) && \array_key_exists($property, $this->cache[$class])) {
if (isset($this->cache[$class][$property])) {

Comment on lines 57 to 60
if (!isset($this->cache[$class])) {
$this->cache[$class] = [];
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!isset($this->cache[$class])) {
$this->cache[$class] = [];
}

$classReflection = new \ReflectionClass($class);
$propertyReflection = $classReflection->getProperty($property);
} catch (\ReflectionException) {
return $this->cache[$class][$property] = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return $this->cache[$class][$property] = null;
return $this->cache[$class][$property] = false;

Comment on lines 62 to 63
$classReflection = new \ReflectionClass($class);
$propertyReflection = $classReflection->getProperty($property);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
$classReflection = new \ReflectionClass($class);
$propertyReflection = $classReflection->getProperty($property);
$propertyReflection = new \ReflectionProperty($class, $property);

Comment on lines 68 to 72
if (!$propertyReflection->getType() instanceof \ReflectionNamedType || !enum_exists($propertyReflection->getType()->getName())) {
return $this->cache[$class][$property] = null;
}

return $this->cache[$class][$property] = $propertyReflection->getType();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (!$propertyReflection->getType() instanceof \ReflectionNamedType || !enum_exists($propertyReflection->getType()->getName())) {
return $this->cache[$class][$property] = null;
}
return $this->cache[$class][$property] = $propertyReflection->getType();
$type = $propertyReflection->getType();
if (!$type instanceof \ReflectionNamedType || !enum_exists($enum->getName())) {
$type = false;
}
return $this->cache[$class][$property] = $type;

@mttsch mttsch force-pushed the feature/61272-form-enum-type-guesser branch from a5eebb2 to 1e540eb Compare August 5, 2025 14:51
@@ -564,6 +565,10 @@ public function load(array $configs, ContainerBuilder $container): void
if (!$this->readConfigEnabled('html_sanitizer', $container, $config['html_sanitizer']) || !class_exists(TextTypeHtmlSanitizerExtension::class)) {
$container->removeDefinition('form.type_extension.form.html_sanitizer');
}

if (!class_exists(EnumFormTypeGuesser::class)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do this inside registerFormConfiguration()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@mttsch mttsch force-pushed the feature/61272-form-enum-type-guesser branch from 1e540eb to dc492c9 Compare August 6, 2025 03:28
@mttsch
Copy link
Contributor Author

mttsch commented Aug 19, 2025

@nicolas-grekas @xabbuh Is there anything more to do for me in this PR?

$type = false;
}

return $this->cache[$class][$property] = $type;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be better in terms of memory usage to only store the enum name here (i.e. the result of $type->getName()) instead of a full reflection object?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am also using the result of $type->allowsNull() in guessRequired(), so that we be two values needed.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can add that information in the string by prefixing it with a question mark.

Copy link
Member

@xabbuh xabbuh Aug 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My main concern with the current approach is that I am unsure if it plays well with long running processes where the guesser is used while serving many requests.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by prefixing it with a question mark

done

@mttsch mttsch force-pushed the feature/61272-form-enum-type-guesser branch from dc492c9 to 7918e76 Compare August 22, 2025 14:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Form] With form builder, auto-detect Enum properties
4 participants