vendor/symfony/form/Extension/Core/Type/FormType.php line 29

  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\Extension\Core\Type;
  11. use Symfony\Component\Form\Exception\LogicException;
  12. use Symfony\Component\Form\Extension\Core\DataAccessor\CallbackAccessor;
  13. use Symfony\Component\Form\Extension\Core\DataAccessor\ChainAccessor;
  14. use Symfony\Component\Form\Extension\Core\DataAccessor\PropertyPathAccessor;
  15. use Symfony\Component\Form\Extension\Core\DataMapper\DataMapper;
  16. use Symfony\Component\Form\Extension\Core\EventListener\TrimListener;
  17. use Symfony\Component\Form\FormBuilderInterface;
  18. use Symfony\Component\Form\FormInterface;
  19. use Symfony\Component\Form\FormView;
  20. use Symfony\Component\OptionsResolver\Options;
  21. use Symfony\Component\OptionsResolver\OptionsResolver;
  22. use Symfony\Component\PropertyAccess\PropertyAccess;
  23. use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
  24. use Symfony\Contracts\Translation\TranslatableInterface;
  25. class FormType extends BaseType
  26. {
  27.     private DataMapper $dataMapper;
  28.     public function __construct(PropertyAccessorInterface $propertyAccessor null)
  29.     {
  30.         $this->dataMapper = new DataMapper(new ChainAccessor([
  31.             new CallbackAccessor(),
  32.             new PropertyPathAccessor($propertyAccessor ?? PropertyAccess::createPropertyAccessor()),
  33.         ]));
  34.     }
  35.     public function buildForm(FormBuilderInterface $builder, array $options)
  36.     {
  37.         parent::buildForm($builder$options);
  38.         $isDataOptionSet \array_key_exists('data'$options);
  39.         $builder
  40.             ->setRequired($options['required'])
  41.             ->setErrorBubbling($options['error_bubbling'])
  42.             ->setEmptyData($options['empty_data'])
  43.             ->setPropertyPath($options['property_path'])
  44.             ->setMapped($options['mapped'])
  45.             ->setByReference($options['by_reference'])
  46.             ->setInheritData($options['inherit_data'])
  47.             ->setCompound($options['compound'])
  48.             ->setData($isDataOptionSet $options['data'] : null)
  49.             ->setDataLocked($isDataOptionSet)
  50.             ->setDataMapper($options['compound'] ? $this->dataMapper null)
  51.             ->setMethod($options['method'])
  52.             ->setAction($options['action']);
  53.         if ($options['trim']) {
  54.             $builder->addEventSubscriber(new TrimListener());
  55.         }
  56.         $builder->setIsEmptyCallback($options['is_empty_callback']);
  57.     }
  58.     public function buildView(FormView $viewFormInterface $form, array $options)
  59.     {
  60.         parent::buildView($view$form$options);
  61.         $name $form->getName();
  62.         $helpTranslationParameters $options['help_translation_parameters'];
  63.         if ($view->parent) {
  64.             if ('' === $name) {
  65.                 throw new LogicException('Form node with empty name can be used only as root form node.');
  66.             }
  67.             // Complex fields are read-only if they themselves or their parents are.
  68.             if (!isset($view->vars['attr']['readonly']) && isset($view->parent->vars['attr']['readonly']) && false !== $view->parent->vars['attr']['readonly']) {
  69.                 $view->vars['attr']['readonly'] = true;
  70.             }
  71.             $helpTranslationParameters array_merge($view->parent->vars['help_translation_parameters'], $helpTranslationParameters);
  72.         }
  73.         $formConfig $form->getConfig();
  74.         $view->vars array_replace($view->vars, [
  75.             'errors' => $form->getErrors(),
  76.             'valid' => $form->isSubmitted() ? $form->isValid() : true,
  77.             'value' => $form->getViewData(),
  78.             'data' => $form->getNormData(),
  79.             'required' => $form->isRequired(),
  80.             'label_attr' => $options['label_attr'],
  81.             'help' => $options['help'],
  82.             'help_attr' => $options['help_attr'],
  83.             'help_html' => $options['help_html'],
  84.             'help_translation_parameters' => $helpTranslationParameters,
  85.             'compound' => $formConfig->getCompound(),
  86.             'method' => $formConfig->getMethod(),
  87.             'action' => $formConfig->getAction(),
  88.             'submitted' => $form->isSubmitted(),
  89.         ]);
  90.     }
  91.     public function finishView(FormView $viewFormInterface $form, array $options)
  92.     {
  93.         $multipart false;
  94.         foreach ($view->children as $child) {
  95.             if ($child->vars['multipart']) {
  96.                 $multipart true;
  97.                 break;
  98.             }
  99.         }
  100.         $view->vars['multipart'] = $multipart;
  101.     }
  102.     public function configureOptions(OptionsResolver $resolver)
  103.     {
  104.         parent::configureOptions($resolver);
  105.         // Derive "data_class" option from passed "data" object
  106.         $dataClass = function (Options $options) {
  107.             return isset($options['data']) && \is_object($options['data']) ? \get_class($options['data']) : null;
  108.         };
  109.         // Derive "empty_data" closure from "data_class" option
  110.         $emptyData = function (Options $options) {
  111.             $class $options['data_class'];
  112.             if (null !== $class) {
  113.                 return function (FormInterface $form) use ($class) {
  114.                     return $form->isEmpty() && !$form->isRequired() ? null : new $class();
  115.                 };
  116.             }
  117.             return function (FormInterface $form) {
  118.                 return $form->getConfig()->getCompound() ? [] : '';
  119.             };
  120.         };
  121.         // Wrap "post_max_size_message" in a closure to translate it lazily
  122.         $uploadMaxSizeMessage = function (Options $options) {
  123.             return function () use ($options) {
  124.                 return $options['post_max_size_message'];
  125.             };
  126.         };
  127.         // For any form that is not represented by a single HTML control,
  128.         // errors should bubble up by default
  129.         $errorBubbling = function (Options $options) {
  130.             return $options['compound'] && !$options['inherit_data'];
  131.         };
  132.         // If data is given, the form is locked to that data
  133.         // (independent of its value)
  134.         $resolver->setDefined([
  135.             'data',
  136.         ]);
  137.         $resolver->setDefaults([
  138.             'data_class' => $dataClass,
  139.             'empty_data' => $emptyData,
  140.             'trim' => true,
  141.             'required' => true,
  142.             'property_path' => null,
  143.             'mapped' => true,
  144.             'by_reference' => true,
  145.             'error_bubbling' => $errorBubbling,
  146.             'label_attr' => [],
  147.             'inherit_data' => false,
  148.             'compound' => true,
  149.             'method' => 'POST',
  150.             // According to RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt)
  151.             // section 4.2., empty URIs are considered same-document references
  152.             'action' => '',
  153.             'attr' => [],
  154.             'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
  155.             'upload_max_size_message' => $uploadMaxSizeMessage// internal
  156.             'allow_file_upload' => false,
  157.             'help' => null,
  158.             'help_attr' => [],
  159.             'help_html' => false,
  160.             'help_translation_parameters' => [],
  161.             'invalid_message' => 'This value is not valid.',
  162.             'invalid_message_parameters' => [],
  163.             'is_empty_callback' => null,
  164.             'getter' => null,
  165.             'setter' => null,
  166.         ]);
  167.         $resolver->setAllowedTypes('label_attr''array');
  168.         $resolver->setAllowedTypes('action''string');
  169.         $resolver->setAllowedTypes('upload_max_size_message', ['callable']);
  170.         $resolver->setAllowedTypes('help', ['string''null'TranslatableInterface::class]);
  171.         $resolver->setAllowedTypes('help_attr''array');
  172.         $resolver->setAllowedTypes('help_html''bool');
  173.         $resolver->setAllowedTypes('is_empty_callback', ['null''callable']);
  174.         $resolver->setAllowedTypes('getter', ['null''callable']);
  175.         $resolver->setAllowedTypes('setter', ['null''callable']);
  176.         $resolver->setInfo('getter''A callable that accepts two arguments (the view data and the current form field) and must return a value.');
  177.         $resolver->setInfo('setter''A callable that accepts three arguments (a reference to the view data, the submitted value and the current form field).');
  178.     }
  179.     public function getParent(): ?string
  180.     {
  181.         return null;
  182.     }
  183.     public function getBlockPrefix(): string
  184.     {
  185.         return 'form';
  186.     }
  187. }