N°8771 - Add Symfony form component to iTop core (#760)

- Add Symfony Form Component
- Add Symfony CSRF security component
- Add iTop default form template
- Add Twig debug extension to Twig Environment
- Add iTop abstract controller facility to get form builder
- Add Twig filter to make trans an alias of dict_s filter
This commit is contained in:
Benjamin Dalsass
2025-10-10 16:02:25 +02:00
committed by GitHub
parent 82395727bf
commit 5dd450e9bf
605 changed files with 60106 additions and 12 deletions

View File

@@ -0,0 +1,38 @@
<?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\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class Form extends Constraint
{
public const NOT_SYNCHRONIZED_ERROR = '1dafa156-89e1-4736-b832-419c2e501fca';
public const NO_SUCH_FIELD_ERROR = '6e5212ed-a197-4339-99aa-5654798a4854';
protected const ERROR_NAMES = [
self::NOT_SYNCHRONIZED_ERROR => 'NOT_SYNCHRONIZED_ERROR',
self::NO_SUCH_FIELD_ERROR => 'NO_SUCH_FIELD_ERROR',
];
/**
* @deprecated since Symfony 6.1, use const ERROR_NAMES instead
*/
protected static $errorNames = self::ERROR_NAMES;
public function getTargets(): string|array
{
return self::CLASS_CONSTRAINT;
}
}

View File

@@ -0,0 +1,279 @@
<?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\Validator\Constraints;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Composite;
use Symfony\Component\Validator\Constraints\GroupSequence;
use Symfony\Component\Validator\Constraints\Valid;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FormValidator extends ConstraintValidator
{
/**
* @var \SplObjectStorage<FormInterface, array<int, string|string[]|GroupSequence>>
*/
private \SplObjectStorage $resolvedGroups;
/**
* @return void
*/
public function validate(mixed $form, Constraint $formConstraint)
{
if (!$formConstraint instanceof Form) {
throw new UnexpectedTypeException($formConstraint, Form::class);
}
if (!$form instanceof FormInterface) {
return;
}
/** @var FormInterface $form */
$config = $form->getConfig();
$validator = $this->context->getValidator()->inContext($this->context);
if ($form->isSubmitted() && $form->isSynchronized()) {
// Validate the form data only if transformation succeeded
$groups = $this->getValidationGroups($form);
if (!$groups) {
return;
}
$data = $form->getData();
// Validate the data against its own constraints
$validateDataGraph = $form->isRoot()
&& (\is_object($data) || \is_array($data))
&& (($groups && \is_array($groups)) || ($groups instanceof GroupSequence && $groups->groups))
;
// Validate the data against the constraints defined in the form
/** @var Constraint[] $constraints */
$constraints = $config->getOption('constraints', []);
$hasChildren = $form->count() > 0;
if ($hasChildren && $form->isRoot()) {
$this->resolvedGroups = new \SplObjectStorage();
}
if ($groups instanceof GroupSequence) {
// Validate the data, the form AND nested fields in sequence
$violationsCount = $this->context->getViolations()->count();
foreach ($groups->groups as $group) {
if ($validateDataGraph) {
$validator->atPath('data')->validate($data, null, $group);
}
if ($groupedConstraints = self::getConstraintsInGroups($constraints, $group)) {
$validator->atPath('data')->validate($data, $groupedConstraints, $group);
}
foreach ($form->all() as $field) {
if ($field->isSubmitted()) {
// remember to validate this field in one group only
// otherwise resolving the groups would reuse the same
// sequence recursively, thus some fields could fail
// in different steps without breaking early enough
$this->resolvedGroups[$field] = (array) $group;
$fieldFormConstraint = new Form();
$fieldFormConstraint->groups = $group;
$this->context->setNode($this->context->getValue(), $field, $this->context->getMetadata(), $this->context->getPropertyPath());
$validator->atPath(\sprintf('children[%s]', $field->getName()))->validate($field, $fieldFormConstraint, $group);
}
}
if ($violationsCount < $this->context->getViolations()->count()) {
break;
}
}
} else {
if ($validateDataGraph) {
$validator->atPath('data')->validate($data, null, $groups);
}
$groupedConstraints = [];
foreach ($constraints as $constraint) {
// For the "Valid" constraint, validate the data in all groups
if ($constraint instanceof Valid) {
if (\is_object($data) || \is_array($data)) {
$validator->atPath('data')->validate($data, $constraint, $groups);
}
continue;
}
// Otherwise validate a constraint only once for the first
// matching group
foreach ($groups as $group) {
if (\in_array($group, $constraint->groups)) {
$groupedConstraints[$group][] = $constraint;
// Prevent duplicate validation
if (!$constraint instanceof Composite) {
continue 2;
}
}
}
}
foreach ($groupedConstraints as $group => $constraint) {
$validator->atPath('data')->validate($data, $constraint, $group);
}
foreach ($form->all() as $field) {
if ($field->isSubmitted()) {
$this->resolvedGroups[$field] = $groups;
$this->context->setNode($this->context->getValue(), $field, $this->context->getMetadata(), $this->context->getPropertyPath());
$validator->atPath(\sprintf('children[%s]', $field->getName()))->validate($field, $formConstraint);
}
}
}
if ($hasChildren && $form->isRoot()) {
// destroy storage to avoid memory leaks
$this->resolvedGroups = new \SplObjectStorage();
}
} elseif (!$form->isSynchronized()) {
$childrenSynchronized = true;
/** @var FormInterface $child */
foreach ($form as $child) {
if (!$child->isSynchronized()) {
$childrenSynchronized = false;
$this->context->setNode($this->context->getValue(), $child, $this->context->getMetadata(), $this->context->getPropertyPath());
$validator->atPath(\sprintf('children[%s]', $child->getName()))->validate($child, $formConstraint);
}
}
// Mark the form with an error if it is not synchronized BUT all
// of its children are synchronized. If any child is not
// synchronized, an error is displayed there already and showing
// a second error in its parent form is pointless, or worse, may
// lead to duplicate errors if error bubbling is enabled on the
// child.
// See also https://github.com/symfony/symfony/issues/4359
if ($childrenSynchronized) {
$clientDataAsString = \is_scalar($form->getViewData())
? (string) $form->getViewData()
: get_debug_type($form->getViewData());
$failure = $form->getTransformationFailure();
$this->context->setConstraint($formConstraint);
$this->context->buildViolation($failure->getInvalidMessage() ?? $config->getOption('invalid_message'))
->setParameters(array_replace(
['{{ value }}' => $clientDataAsString],
$config->getOption('invalid_message_parameters'),
$failure->getInvalidMessageParameters()
))
->setInvalidValue($form->getViewData())
->setCode(Form::NOT_SYNCHRONIZED_ERROR)
->setCause($failure)
->addViolation();
}
}
// Mark the form with an error if it contains extra fields
if (!$config->getOption('allow_extra_fields') && \count($form->getExtraData()) > 0) {
$this->context->setConstraint($formConstraint);
$this->context->buildViolation($config->getOption('extra_fields_message', ''))
->setParameter('{{ extra_fields }}', '"'.implode('", "', array_keys($form->getExtraData())).'"')
->setPlural(\count($form->getExtraData()))
->setInvalidValue($form->getExtraData())
->setCode(Form::NO_SUCH_FIELD_ERROR)
->addViolation();
}
}
/**
* Returns the validation groups of the given form.
*
* @return string|GroupSequence|array<string|GroupSequence>
*/
private function getValidationGroups(FormInterface $form): string|GroupSequence|array
{
// Determine the clicked button of the complete form tree
$clickedButton = null;
if (method_exists($form, 'getClickedButton')) {
$clickedButton = $form->getClickedButton();
}
if (null !== $clickedButton) {
$groups = $clickedButton->getConfig()->getOption('validation_groups');
if (null !== $groups) {
return self::resolveValidationGroups($groups, $form);
}
}
do {
$groups = $form->getConfig()->getOption('validation_groups');
if (null !== $groups) {
return self::resolveValidationGroups($groups, $form);
}
if (isset($this->resolvedGroups[$form])) {
return $this->resolvedGroups[$form];
}
$form = $form->getParent();
} while (null !== $form);
return [Constraint::DEFAULT_GROUP];
}
/**
* Post-processes the validation groups option for a given form.
*
* @param string|GroupSequence|array<string|GroupSequence>|callable $groups The validation groups
*
* @return GroupSequence|array<string|GroupSequence>
*/
private static function resolveValidationGroups(string|GroupSequence|array|callable $groups, FormInterface $form): GroupSequence|array
{
if (!\is_string($groups) && \is_callable($groups)) {
$groups = $groups($form);
}
if ($groups instanceof GroupSequence) {
return $groups;
}
return (array) $groups;
}
private static function getConstraintsInGroups(array $constraints, string|array $group): array
{
$groups = (array) $group;
return array_filter($constraints, static function (Constraint $constraint) use ($groups) {
foreach ($groups as $group) {
if (\in_array($group, $constraint->groups, true)) {
return true;
}
}
return false;
});
}
}

View File

@@ -0,0 +1,58 @@
<?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\Validator\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\Extension\Validator\Constraints\Form;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapperInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Validator\Validator\ValidatorInterface;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ValidationListener implements EventSubscriberInterface
{
private ValidatorInterface $validator;
private ViolationMapperInterface $violationMapper;
public static function getSubscribedEvents(): array
{
return [FormEvents::POST_SUBMIT => 'validateForm'];
}
public function __construct(ValidatorInterface $validator, ViolationMapperInterface $violationMapper)
{
$this->validator = $validator;
$this->violationMapper = $violationMapper;
}
/**
* @return void
*/
public function validateForm(FormEvent $event)
{
$form = $event->getForm();
if ($form->isRoot()) {
// Form groups are validated internally (FormValidator). Here we don't set groups as they are retrieved into the validator.
foreach ($this->validator->validate($form) as $violation) {
// Allow the "invalid" constraint to be put onto
// non-synchronized forms
$allowNonSynchronized = $violation->getConstraint() instanceof Form && Form::NOT_SYNCHRONIZED_ERROR === $violation->getCode();
$this->violationMapper->mapViolation($violation, $form, $allowNonSynchronized);
}
}
}
}

View File

@@ -0,0 +1,59 @@
<?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\Validator\Type;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\GroupSequence;
/**
* Encapsulates common logic of {@link FormTypeValidatorExtension} and
* {@link SubmitTypeValidatorExtension}.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
abstract class BaseValidatorExtension extends AbstractTypeExtension
{
/**
* @return void
*/
public function configureOptions(OptionsResolver $resolver)
{
// Make sure that validation groups end up as null, closure or array
$validationGroupsNormalizer = static function (Options $options, $groups) {
if (false === $groups) {
return [];
}
if (empty($groups)) {
return null;
}
if (\is_callable($groups)) {
return $groups;
}
if ($groups instanceof GroupSequence) {
return $groups;
}
return (array) $groups;
};
$resolver->setDefaults([
'validation_groups' => null,
]);
$resolver->setNormalizer('validation_groups', $validationGroupsNormalizer);
}
}

View File

@@ -0,0 +1,74 @@
<?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\Validator\Type;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener;
use Symfony\Component\Form\Extension\Validator\ViolationMapper\ViolationMapper;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormRendererInterface;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class FormTypeValidatorExtension extends BaseValidatorExtension
{
private ValidatorInterface $validator;
private ViolationMapper $violationMapper;
private bool $legacyErrorMessages;
public function __construct(ValidatorInterface $validator, bool $legacyErrorMessages = true, ?FormRendererInterface $formRenderer = null, ?TranslatorInterface $translator = null)
{
$this->validator = $validator;
$this->violationMapper = new ViolationMapper($formRenderer, $translator);
}
/**
* @return void
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->addEventSubscriber(new ValidationListener($this->validator, $this->violationMapper));
}
/**
* @return void
*/
public function configureOptions(OptionsResolver $resolver)
{
parent::configureOptions($resolver);
// Constraint should always be converted to an array
$constraintsNormalizer = static fn (Options $options, $constraints) => \is_object($constraints) ? [$constraints] : (array) $constraints;
$resolver->setDefaults([
'error_mapping' => [],
'constraints' => [],
'invalid_message' => 'This value is not valid.',
'invalid_message_parameters' => [],
'allow_extra_fields' => false,
'extra_fields_message' => 'This form should not contain extra fields.',
]);
$resolver->setAllowedTypes('constraints', [Constraint::class, Constraint::class.'[]']);
$resolver->setNormalizer('constraints', $constraintsNormalizer);
}
public static function getExtendedTypes(): iterable
{
return [FormType::class];
}
}

View File

@@ -0,0 +1,41 @@
<?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\Validator\Type;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\RepeatedType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class RepeatedTypeValidatorExtension extends AbstractTypeExtension
{
/**
* @return void
*/
public function configureOptions(OptionsResolver $resolver)
{
// Map errors to the first field
$errorMapping = static fn (Options $options) => ['.' => $options['first_name']];
$resolver->setDefaults([
'error_mapping' => $errorMapping,
]);
}
public static function getExtendedTypes(): iterable
{
return [RepeatedType::class];
}
}

View File

@@ -0,0 +1,25 @@
<?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\Validator\Type;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class SubmitTypeValidatorExtension extends BaseValidatorExtension
{
public static function getExtendedTypes(): iterable
{
return [SubmitType::class];
}
}

View File

@@ -0,0 +1,49 @@
<?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\Validator\Type;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @author Abdellatif Ait boudad <a.aitboudad@gmail.com>
* @author David Badura <d.a.badura@gmail.com>
*/
class UploadValidatorExtension extends AbstractTypeExtension
{
private TranslatorInterface $translator;
private ?string $translationDomain;
public function __construct(TranslatorInterface $translator, ?string $translationDomain = null)
{
$this->translator = $translator;
$this->translationDomain = $translationDomain;
}
/**
* @return void
*/
public function configureOptions(OptionsResolver $resolver)
{
$translator = $this->translator;
$translationDomain = $this->translationDomain;
$resolver->setNormalizer('upload_max_size_message', static fn (Options $options, $message) => static fn () => $translator->trans($message(), [], $translationDomain));
}
public static function getExtendedTypes(): iterable
{
return [FormType::class];
}
}

View File

@@ -0,0 +1,68 @@
<?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\Validator;
use Symfony\Component\Form\AbstractExtension;
use Symfony\Component\Form\Extension\Validator\Constraints\Form;
use Symfony\Component\Form\FormRendererInterface;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Validator\Constraints\Traverse;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* Extension supporting the Symfony Validator component in forms.
*
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ValidatorExtension extends AbstractExtension
{
private ValidatorInterface $validator;
private ?FormRendererInterface $formRenderer;
private ?TranslatorInterface $translator;
private bool $legacyErrorMessages;
public function __construct(ValidatorInterface $validator, bool $legacyErrorMessages = true, ?FormRendererInterface $formRenderer = null, ?TranslatorInterface $translator = null)
{
$this->legacyErrorMessages = $legacyErrorMessages;
/** @var ClassMetadata $metadata */
$metadata = $validator->getMetadataFor(\Symfony\Component\Form\Form::class);
// Register the form constraints in the validator programmatically.
// This functionality is required when using the Form component without
// the DIC, where the XML file is loaded automatically. Thus the following
// code must be kept synchronized with validation.xml
$metadata->addConstraint(new Form());
$metadata->addConstraint(new Traverse(false));
$this->validator = $validator;
$this->formRenderer = $formRenderer;
$this->translator = $translator;
}
public function loadTypeGuesser(): ?FormTypeGuesserInterface
{
return new ValidatorTypeGuesser($this->validator);
}
protected function loadTypeExtensions(): array
{
return [
new Type\FormTypeValidatorExtension($this->validator, $this->legacyErrorMessages, $this->formRenderer, $this->translator),
new Type\RepeatedTypeValidatorExtension(),
new Type\SubmitTypeValidatorExtension(),
];
}
}

View File

@@ -0,0 +1,293 @@
<?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\Validator;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\CountryType;
use Symfony\Component\Form\Extension\Core\Type\CurrencyType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\EmailType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\LanguageType;
use Symfony\Component\Form\Extension\Core\Type\LocaleType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\TimeType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormTypeGuesserInterface;
use Symfony\Component\Form\Guess\Guess;
use Symfony\Component\Form\Guess\TypeGuess;
use Symfony\Component\Form\Guess\ValueGuess;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints\Count;
use Symfony\Component\Validator\Constraints\Country;
use Symfony\Component\Validator\Constraints\Currency;
use Symfony\Component\Validator\Constraints\Date;
use Symfony\Component\Validator\Constraints\DateTime;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\Constraints\Image;
use Symfony\Component\Validator\Constraints\Ip;
use Symfony\Component\Validator\Constraints\IsFalse;
use Symfony\Component\Validator\Constraints\IsTrue;
use Symfony\Component\Validator\Constraints\Language;
use Symfony\Component\Validator\Constraints\Length;
use Symfony\Component\Validator\Constraints\Locale;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\NotNull;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Constraints\Regex;
use Symfony\Component\Validator\Constraints\Time;
use Symfony\Component\Validator\Constraints\Type;
use Symfony\Component\Validator\Constraints\Url;
use Symfony\Component\Validator\Mapping\ClassMetadataInterface;
use Symfony\Component\Validator\Mapping\Factory\MetadataFactoryInterface;
class ValidatorTypeGuesser implements FormTypeGuesserInterface
{
private MetadataFactoryInterface $metadataFactory;
public function __construct(MetadataFactoryInterface $metadataFactory)
{
$this->metadataFactory = $metadataFactory;
}
public function guessType(string $class, string $property): ?TypeGuess
{
return $this->guess($class, $property, $this->guessTypeForConstraint(...));
}
public function guessRequired(string $class, string $property): ?ValueGuess
{
// If we don't find any constraint telling otherwise, we can assume
// that a field is not required (with LOW_CONFIDENCE)
return $this->guess($class, $property, $this->guessRequiredForConstraint(...), false);
}
public function guessMaxLength(string $class, string $property): ?ValueGuess
{
return $this->guess($class, $property, $this->guessMaxLengthForConstraint(...));
}
public function guessPattern(string $class, string $property): ?ValueGuess
{
return $this->guess($class, $property, $this->guessPatternForConstraint(...));
}
/**
* Guesses a field class name for a given constraint.
*/
public function guessTypeForConstraint(Constraint $constraint): ?TypeGuess
{
switch ($constraint::class) {
case Type::class:
switch ($constraint->type) {
case 'array':
return new TypeGuess(CollectionType::class, [], Guess::MEDIUM_CONFIDENCE);
case 'boolean':
case 'bool':
return new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE);
case 'double':
case 'float':
case 'numeric':
case 'real':
return new TypeGuess(NumberType::class, [], Guess::MEDIUM_CONFIDENCE);
case 'integer':
case 'int':
case 'long':
return new TypeGuess(IntegerType::class, [], Guess::MEDIUM_CONFIDENCE);
case \DateTime::class:
case '\DateTime':
return new TypeGuess(DateType::class, [], Guess::MEDIUM_CONFIDENCE);
case \DateTimeImmutable::class:
case '\DateTimeImmutable':
case \DateTimeInterface::class:
case '\DateTimeInterface':
return new TypeGuess(DateType::class, ['input' => 'datetime_immutable'], Guess::MEDIUM_CONFIDENCE);
case 'string':
return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE);
}
break;
case Country::class:
return new TypeGuess(CountryType::class, [], Guess::HIGH_CONFIDENCE);
case Currency::class:
return new TypeGuess(CurrencyType::class, [], Guess::HIGH_CONFIDENCE);
case Date::class:
return new TypeGuess(DateType::class, ['input' => 'string'], Guess::HIGH_CONFIDENCE);
case DateTime::class:
return new TypeGuess(DateTimeType::class, ['input' => 'string'], Guess::HIGH_CONFIDENCE);
case Email::class:
return new TypeGuess(EmailType::class, [], Guess::HIGH_CONFIDENCE);
case File::class:
case Image::class:
$options = [];
if ($constraint->mimeTypes) {
$options = ['attr' => ['accept' => implode(',', (array) $constraint->mimeTypes)]];
}
return new TypeGuess(FileType::class, $options, Guess::HIGH_CONFIDENCE);
case Language::class:
return new TypeGuess(LanguageType::class, [], Guess::HIGH_CONFIDENCE);
case Locale::class:
return new TypeGuess(LocaleType::class, [], Guess::HIGH_CONFIDENCE);
case Time::class:
return new TypeGuess(TimeType::class, ['input' => 'string'], Guess::HIGH_CONFIDENCE);
case Url::class:
return new TypeGuess(UrlType::class, [], Guess::HIGH_CONFIDENCE);
case Ip::class:
return new TypeGuess(TextType::class, [], Guess::MEDIUM_CONFIDENCE);
case Length::class:
case Regex::class:
return new TypeGuess(TextType::class, [], Guess::LOW_CONFIDENCE);
case Range::class:
return new TypeGuess(NumberType::class, [], Guess::LOW_CONFIDENCE);
case Count::class:
return new TypeGuess(CollectionType::class, [], Guess::LOW_CONFIDENCE);
case IsTrue::class:
case IsFalse::class:
return new TypeGuess(CheckboxType::class, [], Guess::MEDIUM_CONFIDENCE);
}
return null;
}
/**
* Guesses whether a field is required based on the given constraint.
*/
public function guessRequiredForConstraint(Constraint $constraint): ?ValueGuess
{
return match ($constraint::class) {
NotNull::class,
NotBlank::class,
IsTrue::class => new ValueGuess(true, Guess::HIGH_CONFIDENCE),
default => null,
};
}
/**
* Guesses a field's maximum length based on the given constraint.
*/
public function guessMaxLengthForConstraint(Constraint $constraint): ?ValueGuess
{
switch ($constraint::class) {
case Length::class:
if (is_numeric($constraint->max)) {
return new ValueGuess($constraint->max, Guess::HIGH_CONFIDENCE);
}
break;
case Type::class:
if (\in_array($constraint->type, ['double', 'float', 'numeric', 'real'])) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
}
break;
case Range::class:
if (is_numeric($constraint->max)) {
return new ValueGuess(\strlen((string) $constraint->max), Guess::LOW_CONFIDENCE);
}
break;
}
return null;
}
/**
* Guesses a field's pattern based on the given constraint.
*/
public function guessPatternForConstraint(Constraint $constraint): ?ValueGuess
{
switch ($constraint::class) {
case Length::class:
if (is_numeric($constraint->min)) {
return new ValueGuess(\sprintf('.{%s,}', (string) $constraint->min), Guess::LOW_CONFIDENCE);
}
break;
case Regex::class:
$htmlPattern = $constraint->getHtmlPattern();
if (null !== $htmlPattern) {
return new ValueGuess($htmlPattern, Guess::HIGH_CONFIDENCE);
}
break;
case Range::class:
if (is_numeric($constraint->min)) {
return new ValueGuess(\sprintf('.{%s,}', \strlen((string) $constraint->min)), Guess::LOW_CONFIDENCE);
}
break;
case Type::class:
if (\in_array($constraint->type, ['double', 'float', 'numeric', 'real'])) {
return new ValueGuess(null, Guess::MEDIUM_CONFIDENCE);
}
break;
}
return null;
}
/**
* Iterates over the constraints of a property, executes a constraints on
* them and returns the best guess.
*
* @param \Closure $closure The closure that returns a guess
* for a given constraint
* @param mixed $defaultValue The default value assumed if no other value
* can be guessed
*/
protected function guess(string $class, string $property, \Closure $closure, mixed $defaultValue = null): ?Guess
{
$guesses = [];
$classMetadata = $this->metadataFactory->getMetadataFor($class);
if ($classMetadata instanceof ClassMetadataInterface && $classMetadata->hasPropertyMetadata($property)) {
foreach ($classMetadata->getPropertyMetadata($property) as $memberMetadata) {
foreach ($memberMetadata->getConstraints() as $constraint) {
if ($guess = $closure($constraint)) {
$guesses[] = $guess;
}
}
}
}
if (null !== $defaultValue) {
$guesses[] = new ValueGuess($defaultValue, Guess::LOW_CONFIDENCE);
}
return Guess::getBestGuess($guesses);
}
}

View File

@@ -0,0 +1,78 @@
<?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\Validator\ViolationMapper;
use Symfony\Component\Form\Exception\ErrorMappingException;
use Symfony\Component\Form\FormInterface;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class MappingRule
{
private FormInterface $origin;
private string $propertyPath;
private string $targetPath;
public function __construct(FormInterface $origin, string $propertyPath, string $targetPath)
{
$this->origin = $origin;
$this->propertyPath = $propertyPath;
$this->targetPath = $targetPath;
}
public function getOrigin(): FormInterface
{
return $this->origin;
}
/**
* Matches a property path against the rule path.
*
* If the rule matches, the form mapped by the rule is returned.
* Otherwise this method returns false.
*/
public function match(string $propertyPath): ?FormInterface
{
return $propertyPath === $this->propertyPath ? $this->getTarget() : null;
}
/**
* Matches a property path against a prefix of the rule path.
*/
public function isPrefix(string $propertyPath): bool
{
$length = \strlen($propertyPath);
$prefix = substr($this->propertyPath, 0, $length);
$next = $this->propertyPath[$length] ?? null;
return $prefix === $propertyPath && ('[' === $next || '.' === $next);
}
/**
* @throws ErrorMappingException
*/
public function getTarget(): FormInterface
{
$childNames = explode('.', $this->targetPath);
$target = $this->origin;
foreach ($childNames as $childName) {
if (!$target->has($childName)) {
throw new ErrorMappingException(\sprintf('The child "%s" of "%s" mapped by the rule "%s" in "%s" does not exist.', $childName, $target->getName(), $this->targetPath, $this->origin->getName()));
}
$target = $target->get($childName);
}
return $target;
}
}

View File

@@ -0,0 +1,35 @@
<?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\Validator\ViolationMapper;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\PropertyAccess\PropertyPath;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class RelativePath extends PropertyPath
{
private FormInterface $root;
public function __construct(FormInterface $root, string $propertyPath)
{
parent::__construct($propertyPath);
$this->root = $root;
}
public function getRoot(): FormInterface
{
return $this->root;
}
}

View File

@@ -0,0 +1,343 @@
<?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\Validator\ViolationMapper;
use Symfony\Component\Form\FileUploadError;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormRendererInterface;
use Symfony\Component\Form\Util\InheritDataAwareIterator;
use Symfony\Component\PropertyAccess\PropertyPathBuilder;
use Symfony\Component\PropertyAccess\PropertyPathIterator;
use Symfony\Component\PropertyAccess\PropertyPathIteratorInterface;
use Symfony\Component\Validator\Constraints\File;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Contracts\Translation\TranslatorInterface;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ViolationMapper implements ViolationMapperInterface
{
private ?FormRendererInterface $formRenderer;
private ?TranslatorInterface $translator;
private bool $allowNonSynchronized = false;
public function __construct(?FormRendererInterface $formRenderer = null, ?TranslatorInterface $translator = null)
{
$this->formRenderer = $formRenderer;
$this->translator = $translator;
}
/**
* @return void
*/
public function mapViolation(ConstraintViolation $violation, FormInterface $form, bool $allowNonSynchronized = false)
{
$this->allowNonSynchronized = $allowNonSynchronized;
// The scope is the currently found most specific form that
// an error should be mapped to. After setting the scope, the
// mapper will try to continue to find more specific matches in
// the children of scope. If it cannot, the error will be
// mapped to this scope.
$scope = null;
$violationPath = null;
$relativePath = null;
$match = false;
// Don't create a ViolationPath instance for empty property paths
if ('' !== $violation->getPropertyPath()) {
$violationPath = new ViolationPath($violation->getPropertyPath());
$relativePath = $this->reconstructPath($violationPath, $form);
}
// This case happens if the violation path is empty and thus
// the violation should be mapped to the root form
if (null === $violationPath) {
$scope = $form;
}
// In general, mapping happens from the root form to the leaf forms
// First, the rules of the root form are applied to determine
// the subsequent descendant. The rules of this descendant are then
// applied to find the next and so on, until we have found the
// most specific form that matches the violation.
// If any of the forms found in this process is not synchronized,
// mapping is aborted. Non-synchronized forms could not reverse
// transform the value entered by the user, thus any further violations
// caused by the (invalid) reverse transformed value should be
// ignored.
if (null !== $relativePath) {
// Set the scope to the root of the relative path
// This root will usually be $form. If the path contains
// an unmapped form though, the last unmapped form found
// will be the root of the path.
$scope = $relativePath->getRoot();
$it = new PropertyPathIterator($relativePath);
while ($this->acceptsErrors($scope) && null !== ($child = $this->matchChild($scope, $it))) {
$scope = $child;
$it->next();
$match = true;
}
}
// This case happens if an error happened in the data under a
// form inheriting its parent data that does not match any of the
// children of that form.
if (null !== $violationPath && !$match) {
// If we could not map the error to anything more specific
// than the root element, map it to the innermost directly
// mapped form of the violation path
// e.g. "children[foo].children[bar].data.baz"
// Here the innermost directly mapped child is "bar"
$scope = $form;
$it = new ViolationPathIterator($violationPath);
// Note: acceptsErrors() will always return true for forms inheriting
// their parent data, because these forms can never be non-synchronized
// (they don't do any data transformation on their own)
while ($this->acceptsErrors($scope) && $it->valid() && $it->mapsForm()) {
if (!$scope->has($it->current())) {
// Break if we find a reference to a non-existing child
break;
}
$scope = $scope->get($it->current());
$it->next();
}
}
// Follow dot rules until we have the final target
$mapping = $scope->getConfig()->getOption('error_mapping');
while ($this->acceptsErrors($scope) && isset($mapping['.'])) {
$dotRule = new MappingRule($scope, '.', $mapping['.']);
$scope = $dotRule->getTarget();
$mapping = $scope->getConfig()->getOption('error_mapping');
}
// Only add the error if the form is synchronized
if ($this->acceptsErrors($scope)) {
if ($violation->getConstraint() instanceof File && (string) \UPLOAD_ERR_INI_SIZE === $violation->getCode()) {
$errorsTarget = $scope;
while (null !== $errorsTarget->getParent() && $errorsTarget->getConfig()->getErrorBubbling()) {
$errorsTarget = $errorsTarget->getParent();
}
$errors = $errorsTarget->getErrors();
$errorsTarget->clearErrors();
foreach ($errors as $error) {
if (!$error instanceof FileUploadError) {
$errorsTarget->addError($error);
}
}
}
$message = $violation->getMessage();
$messageTemplate = $violation->getMessageTemplate();
if (str_contains($message, '{{ label }}') || str_contains($messageTemplate, '{{ label }}')) {
$form = $scope;
do {
$labelFormat = $form->getConfig()->getOption('label_format');
} while (null === $labelFormat && null !== $form = $form->getParent());
if (null !== $labelFormat) {
$label = str_replace(
[
'%name%',
'%id%',
],
[
$scope->getName(),
(string) $scope->getPropertyPath(),
],
$labelFormat
);
} else {
$label = $scope->getConfig()->getOption('label');
}
if (false !== $label) {
if (null === $label && null !== $this->formRenderer) {
$label = $this->formRenderer->humanize($scope->getName());
} else {
$label ??= $scope->getName();
}
if (null !== $this->translator) {
$form = $scope;
$translationParameters[] = $form->getConfig()->getOption('label_translation_parameters', []);
do {
$translationDomain = $form->getConfig()->getOption('translation_domain');
array_unshift(
$translationParameters,
$form->getConfig()->getOption('label_translation_parameters', [])
);
} while (null === $translationDomain && null !== $form = $form->getParent());
$translationParameters = array_merge([], ...$translationParameters);
$label = $this->translator->trans(
$label,
$translationParameters,
$translationDomain
);
}
$message = str_replace('{{ label }}', $label, $message);
$messageTemplate = str_replace('{{ label }}', $label, $messageTemplate);
}
}
$scope->addError(new FormError(
$message,
$messageTemplate,
$violation->getParameters(),
$violation->getPlural(),
$violation
));
}
}
/**
* Tries to match the beginning of the property path at the
* current position against the children of the scope.
*
* If a matching child is found, it is returned. Otherwise
* null is returned.
*/
private function matchChild(FormInterface $form, PropertyPathIteratorInterface $it): ?FormInterface
{
$target = null;
$chunk = '';
$foundAtIndex = null;
// Construct mapping rules for the given form
$rules = [];
foreach ($form->getConfig()->getOption('error_mapping') as $propertyPath => $targetPath) {
// Dot rules are considered at the very end
if ('.' !== $propertyPath) {
$rules[] = new MappingRule($form, $propertyPath, $targetPath);
}
}
$children = iterator_to_array(new \RecursiveIteratorIterator(new InheritDataAwareIterator($form)), false);
while ($it->valid()) {
if ($it->isIndex()) {
$chunk .= '['.$it->current().']';
} else {
$chunk .= ('' === $chunk ? '' : '.').$it->current();
}
// Test mapping rules as long as we have any
foreach ($rules as $key => $rule) {
/** @var MappingRule $rule */
// Mapping rule matches completely, terminate.
if (null !== ($form = $rule->match($chunk))) {
return $form;
}
// Keep only rules that have $chunk as prefix
if (!$rule->isPrefix($chunk)) {
unset($rules[$key]);
}
}
/** @var FormInterface $child */
foreach ($children as $i => $child) {
$childPath = (string) $child->getPropertyPath();
if ($childPath === $chunk) {
$target = $child;
$foundAtIndex = $it->key();
} elseif (str_starts_with($childPath, $chunk)) {
continue;
}
unset($children[$i]);
}
$it->next();
}
if (null !== $foundAtIndex) {
$it->seek($foundAtIndex);
}
return $target;
}
/**
* Reconstructs a property path from a violation path and a form tree.
*/
private function reconstructPath(ViolationPath $violationPath, FormInterface $origin): ?RelativePath
{
$propertyPathBuilder = new PropertyPathBuilder($violationPath);
$it = $violationPath->getIterator();
$scope = $origin;
// Remember the current index in the builder
$i = 0;
// Expand elements that map to a form (like "children[address]")
for ($it->rewind(); $it->valid() && $it->mapsForm(); $it->next()) {
if (!$scope->has($it->current())) {
// Scope relates to a form that does not exist
// Bail out
break;
}
// Process child form
$scope = $scope->get($it->current());
if ($scope->getConfig()->getInheritData()) {
// Form inherits its parent data
// Cut the piece out of the property path and proceed
$propertyPathBuilder->remove($i);
} else {
/** @var \Symfony\Component\PropertyAccess\PropertyPathInterface $propertyPath */
$propertyPath = $scope->getPropertyPath();
if (null === $propertyPath) {
// Property path of a mapped form is null
// Should not happen, bail out
break;
}
$propertyPathBuilder->replace($i, 1, $propertyPath);
$i += $propertyPath->getLength();
}
}
$finalPath = $propertyPathBuilder->getPropertyPath();
return null !== $finalPath ? new RelativePath($origin, $finalPath) : null;
}
private function acceptsErrors(FormInterface $form): bool
{
return $this->allowNonSynchronized || $form->isSynchronized();
}
}

View File

@@ -0,0 +1,31 @@
<?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\Validator\ViolationMapper;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Validator\ConstraintViolation;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
interface ViolationMapperInterface
{
/**
* Maps a constraint violation to a form in the form tree under
* the given form.
*
* @param bool $allowNonSynchronized Whether to allow mapping to non-synchronized forms
*
* @return void
*/
public function mapViolation(ConstraintViolation $violation, FormInterface $form, bool $allowNonSynchronized = false);
}

View File

@@ -0,0 +1,217 @@
<?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\Validator\ViolationMapper;
use Symfony\Component\Form\Exception\OutOfBoundsException;
use Symfony\Component\PropertyAccess\PropertyPath;
use Symfony\Component\PropertyAccess\PropertyPathInterface;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*
* @implements \IteratorAggregate<int, string>
*/
class ViolationPath implements \IteratorAggregate, PropertyPathInterface
{
/** @var list<string> */
private array $elements = [];
private array $isIndex = [];
private array $mapsForm = [];
private string $pathAsString = '';
private int $length = 0;
/**
* Creates a new violation path from a string.
*
* @param string $violationPath The property path of a {@link \Symfony\Component\Validator\ConstraintViolation} object
*/
public function __construct(string $violationPath)
{
$path = new PropertyPath($violationPath);
$elements = $path->getElements();
$data = false;
for ($i = 0, $l = \count($elements); $i < $l; ++$i) {
if (!$data) {
// The element "data" has not yet been passed
if ('children' === $elements[$i] && $path->isProperty($i)) {
// Skip element "children"
++$i;
// Next element must exist and must be an index
// Otherwise consider this the end of the path
if ($i >= $l || !$path->isIndex($i)) {
break;
}
// All the following index items (regardless if .children is
// explicitly used) are children and grand-children
for (; $i < $l && $path->isIndex($i); ++$i) {
$this->elements[] = $elements[$i];
$this->isIndex[] = true;
$this->mapsForm[] = true;
}
// Rewind the pointer as the last element above didn't match
// (even if the pointer was moved forward)
--$i;
} elseif ('data' === $elements[$i] && $path->isProperty($i)) {
// Skip element "data"
++$i;
// End of path
if ($i >= $l) {
break;
}
$this->elements[] = $elements[$i];
$this->isIndex[] = $path->isIndex($i);
$this->mapsForm[] = false;
$data = true;
} else {
// Neither "children" nor "data" property found
// Consider this the end of the path
break;
}
} else {
// Already after the "data" element
// Pick everything as is
$this->elements[] = $elements[$i];
$this->isIndex[] = $path->isIndex($i);
$this->mapsForm[] = false;
}
}
$this->length = \count($this->elements);
$this->buildString();
}
public function __toString(): string
{
return $this->pathAsString;
}
public function getLength(): int
{
return $this->length;
}
public function getParent(): ?PropertyPathInterface
{
if ($this->length <= 1) {
return null;
}
$parent = clone $this;
--$parent->length;
array_pop($parent->elements);
array_pop($parent->isIndex);
array_pop($parent->mapsForm);
$parent->buildString();
return $parent;
}
public function getElements(): array
{
return $this->elements;
}
public function getElement(int $index): string
{
if (!isset($this->elements[$index])) {
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the violation path.', $index));
}
return $this->elements[$index];
}
public function isProperty(int $index): bool
{
if (!isset($this->isIndex[$index])) {
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the violation path.', $index));
}
return !$this->isIndex[$index];
}
public function isIndex(int $index): bool
{
if (!isset($this->isIndex[$index])) {
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the violation path.', $index));
}
return $this->isIndex[$index];
}
public function isNullSafe(int $index): bool
{
return false;
}
/**
* Returns whether an element maps directly to a form.
*
* Consider the following violation path:
*
* children[address].children[office].data.street
*
* In this example, "address" and "office" map to forms, while
* "street does not.
*
* @throws OutOfBoundsException if the offset is invalid
*/
public function mapsForm(int $index): bool
{
if (!isset($this->mapsForm[$index])) {
throw new OutOfBoundsException(\sprintf('The index "%s" is not within the violation path.', $index));
}
return $this->mapsForm[$index];
}
/**
* Returns a new iterator for this path.
*/
public function getIterator(): ViolationPathIterator
{
return new ViolationPathIterator($this);
}
/**
* Builds the string representation from the elements.
*/
private function buildString(): void
{
$this->pathAsString = '';
$data = false;
foreach ($this->elements as $index => $element) {
if ($this->mapsForm[$index]) {
$this->pathAsString .= ".children[$element]";
} elseif (!$data) {
$this->pathAsString .= '.data'.($this->isIndex[$index] ? "[$element]" : ".$element");
$data = true;
} else {
$this->pathAsString .= $this->isIndex[$index] ? "[$element]" : ".$element";
}
}
if ('' !== $this->pathAsString) {
// remove leading dot
$this->pathAsString = substr($this->pathAsString, 1);
}
}
}

View File

@@ -0,0 +1,33 @@
<?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\Validator\ViolationMapper;
use Symfony\Component\PropertyAccess\PropertyPathIterator;
/**
* @author Bernhard Schussek <bschussek@gmail.com>
*/
class ViolationPathIterator extends PropertyPathIterator
{
public function __construct(ViolationPath $violationPath)
{
parent::__construct($violationPath);
}
/**
* @return bool
*/
public function mapsForm()
{
return $this->path->mapsForm($this->key());
}
}