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

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