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

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.         if ($view->parent) {
  61.             if ('' === $name) {
  62.                 throw new LogicException('Form node with empty name can be used only as root form node.');
  63.             }
  64.             // Complex fields are read-only if they themselves or their parents are.
  65.             if (!isset($view->vars['attr']['readonly']) && isset($view->parent->vars['attr']['readonly']) && false !== $view->parent->vars['attr']['readonly']) {
  66.                 $view->vars['attr']['readonly'] = true;
  67.             }
  68.         }
  69.         $formConfig $form->getConfig();
  70.         $view->vars array_replace($view->vars, [
  71.             'errors' => $form->getErrors(),
  72.             'valid' => $form->isSubmitted() ? $form->isValid() : true,
  73.             'value' => $form->getViewData(),
  74.             'data' => $form->getNormData(),
  75.             'required' => $form->isRequired(),
  76.             'size' => null,
  77.             'label_attr' => $options['label_attr'],
  78.             'compound' => $formConfig->getCompound(),
  79.             'method' => $formConfig->getMethod(),
  80.             'action' => $formConfig->getAction(),
  81.             'submitted' => $form->isSubmitted(),
  82.         ]);
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function finishView(FormView $viewFormInterface $form, array $options)
  88.     {
  89.         $multipart false;
  90.         foreach ($view->children as $child) {
  91.             if ($child->vars['multipart']) {
  92.                 $multipart true;
  93.                 break;
  94.             }
  95.         }
  96.         $view->vars['multipart'] = $multipart;
  97.     }
  98.     /**
  99.      * {@inheritdoc}
  100.      */
  101.     public function configureOptions(OptionsResolver $resolver)
  102.     {
  103.         parent::configureOptions($resolver);
  104.         // Derive "data_class" option from passed "data" object
  105.         $dataClass = function (Options $options) {
  106.             return isset($options['data']) && \is_object($options['data']) ? \get_class($options['data']) : null;
  107.         };
  108.         // Derive "empty_data" closure from "data_class" option
  109.         $emptyData = function (Options $options) {
  110.             $class $options['data_class'];
  111.             if (null !== $class) {
  112.                 return function (FormInterface $form) use ($class) {
  113.                     return $form->isEmpty() && !$form->isRequired() ? null : new $class();
  114.                 };
  115.             }
  116.             return function (FormInterface $form) {
  117.                 return $form->getConfig()->getCompound() ? [] : '';
  118.             };
  119.         };
  120.         // Wrap "post_max_size_message" in a closure to translate it lazily
  121.         $uploadMaxSizeMessage = function (Options $options) {
  122.             return function () use ($options) {
  123.                 return $options['post_max_size_message'];
  124.             };
  125.         };
  126.         // For any form that is not represented by a single HTML control,
  127.         // errors should bubble up by default
  128.         $errorBubbling = function (Options $options) {
  129.             return $options['compound'];
  130.         };
  131.         // If data is given, the form is locked to that data
  132.         // (independent of its value)
  133.         $resolver->setDefined([
  134.             'data',
  135.         ]);
  136.         $resolver->setDefaults([
  137.             'data_class' => $dataClass,
  138.             'empty_data' => $emptyData,
  139.             'trim' => true,
  140.             'required' => true,
  141.             'property_path' => null,
  142.             'mapped' => true,
  143.             'by_reference' => true,
  144.             'error_bubbling' => $errorBubbling,
  145.             'label_attr' => [],
  146.             'inherit_data' => false,
  147.             'compound' => true,
  148.             'method' => 'POST',
  149.             // According to RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt)
  150.             // section 4.2., empty URIs are considered same-document references
  151.             'action' => '',
  152.             'attr' => [],
  153.             'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
  154.             'upload_max_size_message' => $uploadMaxSizeMessage// internal
  155.             'allow_file_upload' => false,
  156.         ]);
  157.         $resolver->setAllowedTypes('label_attr''array');
  158.         $resolver->setAllowedTypes('upload_max_size_message', ['callable']);
  159.     }
  160.     /**
  161.      * {@inheritdoc}
  162.      */
  163.     public function getParent()
  164.     {
  165.         return null;
  166.     }
  167.     /**
  168.      * {@inheritdoc}
  169.      */
  170.     public function getBlockPrefix()
  171.     {
  172.         return 'form';
  173.     }
  174. }