vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/Type/FormType.php line 147

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