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

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