PageRenderTime 46ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Isaki/le331.fr
PHP | 229 lines | 153 code | 35 blank | 41 comment | 11 complexity | f791f9dd8fe49d6c2339163914484952 MD5 | raw file
  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 $view, FormInterface $form, array $options)
  60. {
  61. parent::buildView($view, $form, $options);
  62. $name = $form->getName();
  63. $readOnly = $options['read_only'];
  64. if ($view->parent) {
  65. if ('' === $name) {
  66. throw new LogicException('Form node with empty name can be used only as root form node.');
  67. }
  68. // Complex fields are read-only if they themselves or their parents are.
  69. if (!$readOnly) {
  70. $readOnly = $view->parent->vars['read_only'];
  71. }
  72. }
  73. $view->vars = array_replace($view->vars, array(
  74. 'read_only' => $readOnly,
  75. 'errors' => $form->getErrors(),
  76. 'valid' => $form->isSubmitted() ? $form->isValid() : true,
  77. 'value' => $form->getViewData(),
  78. 'data' => $form->getNormData(),
  79. 'required' => $form->isRequired(),
  80. 'max_length' => isset($options['attr']['maxlength']) ? $options['attr']['maxlength'] : null, // Deprecated
  81. 'pattern' => isset($options['attr']['pattern']) ? $options['attr']['pattern'] : null, // Deprecated
  82. 'size' => null,
  83. 'label_attr' => $options['label_attr'],
  84. 'compound' => $form->getConfig()->getCompound(),
  85. 'method' => $form->getConfig()->getMethod(),
  86. 'action' => $form->getConfig()->getAction(),
  87. 'submitted' => $form->isSubmitted(),
  88. ));
  89. }
  90. /**
  91. * {@inheritdoc}
  92. */
  93. public function finishView(FormView $view, FormInterface $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() ? array() : '';
  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. // BC with old "virtual" option
  132. $inheritData = function (Options $options) {
  133. if (null !== $options['virtual']) {
  134. @trigger_error('The form option "virtual" is deprecated since version 2.3 and will be removed in 3.0. Use "inherit_data" instead.', E_USER_DEPRECATED);
  135. return $options['virtual'];
  136. }
  137. return false;
  138. };
  139. // If data is given, the form is locked to that data
  140. // (independent of its value)
  141. $resolver->setDefined(array(
  142. 'data',
  143. ));
  144. // BC clause for the "max_length" and "pattern" option
  145. // Add these values to the "attr" option instead
  146. $defaultAttr = function (Options $options) {
  147. $attributes = array();
  148. if (null !== $options['max_length']) {
  149. $attributes['maxlength'] = $options['max_length'];
  150. }
  151. if (null !== $options['pattern']) {
  152. $attributes['pattern'] = $options['pattern'];
  153. }
  154. return $attributes;
  155. };
  156. $resolver->setDefaults(array(
  157. 'data_class' => $dataClass,
  158. 'empty_data' => $emptyData,
  159. 'trim' => true,
  160. 'required' => true,
  161. 'read_only' => false,
  162. 'max_length' => null,
  163. 'pattern' => null,
  164. 'property_path' => null,
  165. 'mapped' => true,
  166. 'by_reference' => true,
  167. 'error_bubbling' => $errorBubbling,
  168. 'label_attr' => array(),
  169. 'virtual' => null,
  170. 'inherit_data' => $inheritData,
  171. 'compound' => true,
  172. 'method' => 'POST',
  173. // According to RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt)
  174. // section 4.2., empty URIs are considered same-document references
  175. 'action' => '',
  176. 'attr' => $defaultAttr,
  177. 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
  178. ));
  179. $resolver->setAllowedTypes('label_attr', 'array');
  180. }
  181. /**
  182. * {@inheritdoc}
  183. */
  184. public function getParent()
  185. {
  186. }
  187. /**
  188. * {@inheritdoc}
  189. */
  190. public function getName()
  191. {
  192. return 'form';
  193. }
  194. }