PageRenderTime 25ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://gitlab.com/Marwamimo/Crowdrise_Web
PHP | 231 lines | 154 code | 34 blank | 43 comment | 11 complexity | 198fb27ebf6fa1bd6ccceedaf0c6877b 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\OptionsResolverInterface;
  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 setDefaultOptions(OptionsResolverInterface $resolver)
  108. {
  109. parent::setDefaultOptions($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. // Uncomment this as soon as the deprecation note should be shown
  135. // 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);
  136. return $options['virtual'];
  137. }
  138. return false;
  139. };
  140. // If data is given, the form is locked to that data
  141. // (independent of its value)
  142. $resolver->setOptional(array(
  143. 'data',
  144. ));
  145. // BC clause for the "max_length" and "pattern" option
  146. // Add these values to the "attr" option instead
  147. $defaultAttr = function (Options $options) {
  148. $attributes = array();
  149. if (null !== $options['max_length']) {
  150. $attributes['maxlength'] = $options['max_length'];
  151. }
  152. if (null !== $options['pattern']) {
  153. $attributes['pattern'] = $options['pattern'];
  154. }
  155. return $attributes;
  156. };
  157. $resolver->setDefaults(array(
  158. 'data_class' => $dataClass,
  159. 'empty_data' => $emptyData,
  160. 'trim' => true,
  161. 'required' => true,
  162. 'read_only' => false,
  163. 'max_length' => null,
  164. 'pattern' => null,
  165. 'property_path' => null,
  166. 'mapped' => true,
  167. 'by_reference' => true,
  168. 'error_bubbling' => $errorBubbling,
  169. 'label_attr' => array(),
  170. 'virtual' => null,
  171. 'inherit_data' => $inheritData,
  172. 'compound' => true,
  173. 'method' => 'POST',
  174. // According to RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt)
  175. // section 4.2., empty URIs are considered same-document references
  176. 'action' => '',
  177. 'attr' => $defaultAttr,
  178. 'post_max_size_message' => 'The uploaded file was too large. Please try to upload a smaller file.',
  179. ));
  180. $resolver->setAllowedTypes(array(
  181. 'label_attr' => 'array',
  182. ));
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function getParent()
  188. {
  189. }
  190. /**
  191. * {@inheritdoc}
  192. */
  193. public function getName()
  194. {
  195. return 'form';
  196. }
  197. }