/src/Symfony/Component/Form/Extension/Core/Type/FieldType.php

https://github.com/Kiruban2011/symfony · PHP · 182 lines · 134 code · 20 blank · 28 comment · 13 complexity · ab5d986925525c48429457004af25191 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\AbstractType;
  12. use Symfony\Component\Form\Util\PropertyPath;
  13. use Symfony\Component\Form\FormBuilder;
  14. use Symfony\Component\Form\FormInterface;
  15. use Symfony\Component\Form\FormFactoryInterface;
  16. use Symfony\Component\Form\FormView;
  17. use Symfony\Component\Form\Extension\Core\EventListener\TrimListener;
  18. use Symfony\Component\Form\Extension\Core\Validator\DefaultValidator;
  19. use Symfony\Component\EventDispatcher\EventDispatcher;
  20. use Symfony\Component\Form\Exception\FormException;
  21. class FieldType extends AbstractType
  22. {
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public function buildForm(FormBuilder $builder, array $options)
  27. {
  28. if (null === $options['property_path']) {
  29. $options['property_path'] = $builder->getName();
  30. }
  31. if (false === $options['property_path'] || '' === $options['property_path']) {
  32. $options['property_path'] = null;
  33. } else {
  34. $options['property_path'] = new PropertyPath($options['property_path']);
  35. }
  36. if (!is_array($options['attr'])) {
  37. throw new FormException('The "attr" option must be "array".');
  38. }
  39. $builder
  40. ->setRequired($options['required'])
  41. ->setReadOnly($options['read_only'])
  42. ->setErrorBubbling($options['error_bubbling'])
  43. ->setEmptyData($options['empty_data'])
  44. ->setAttribute('by_reference', $options['by_reference'])
  45. ->setAttribute('property_path', $options['property_path'])
  46. ->setAttribute('error_mapping', $options['error_mapping'])
  47. ->setAttribute('max_length', $options['max_length'])
  48. ->setAttribute('pattern', $options['pattern'])
  49. ->setAttribute('label', $options['label'] ?: $this->humanize($builder->getName()))
  50. ->setAttribute('attr', $options['attr'] ?: array())
  51. ->setAttribute('invalid_message', $options['invalid_message'])
  52. ->setAttribute('invalid_message_parameters', $options['invalid_message_parameters'])
  53. ->setAttribute('translation_domain', $options['translation_domain'])
  54. ->setData($options['data'])
  55. ->addValidator(new DefaultValidator())
  56. ;
  57. if ($options['trim']) {
  58. $builder->addEventSubscriber(new TrimListener());
  59. }
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function buildView(FormView $view, FormInterface $form)
  65. {
  66. $name = $form->getName();
  67. if ($view->hasParent()) {
  68. $parentId = $view->getParent()->get('id');
  69. $parentFullName = $view->getParent()->get('full_name');
  70. $id = sprintf('%s_%s', $parentId, $name);
  71. $fullName = sprintf('%s[%s]', $parentFullName, $name);
  72. } else {
  73. $id = $name;
  74. $fullName = $name;
  75. }
  76. $types = array();
  77. foreach ($form->getTypes() as $type) {
  78. $types[] = $type->getName();
  79. }
  80. $view
  81. ->set('form', $view)
  82. ->set('id', $id)
  83. ->set('name', $name)
  84. ->set('full_name', $fullName)
  85. ->set('errors', $form->getErrors())
  86. ->set('value', $form->getClientData())
  87. ->set('read_only', $form->isReadOnly())
  88. ->set('required', $form->isRequired())
  89. ->set('max_length', $form->getAttribute('max_length'))
  90. ->set('pattern', $form->getAttribute('pattern'))
  91. ->set('size', null)
  92. ->set('label', $form->getAttribute('label'))
  93. ->set('multipart', false)
  94. ->set('attr', $form->getAttribute('attr'))
  95. ->set('types', $types)
  96. ->set('translation_domain', $form->getAttribute('translation_domain'))
  97. ;
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function getDefaultOptions(array $options)
  103. {
  104. $defaultOptions = array(
  105. 'data' => null,
  106. 'data_class' => null,
  107. 'trim' => true,
  108. 'required' => true,
  109. 'read_only' => false,
  110. 'max_length' => null,
  111. 'pattern' => null,
  112. 'property_path' => null,
  113. 'by_reference' => true,
  114. 'error_bubbling' => false,
  115. 'error_mapping' => array(),
  116. 'label' => null,
  117. 'attr' => array(),
  118. 'invalid_message' => 'This value is not valid',
  119. 'invalid_message_parameters' => array(),
  120. 'translation_domain' => 'messages',
  121. );
  122. $class = isset($options['data_class']) ? $options['data_class'] : null;
  123. // If no data class is set explicitly and an object is passed as data,
  124. // use the class of that object as data class
  125. if (!$class && isset($options['data']) && is_object($options['data'])) {
  126. $defaultOptions['data_class'] = $class = get_class($options['data']);
  127. }
  128. if ($class) {
  129. $defaultOptions['empty_data'] = function () use ($class) {
  130. return new $class();
  131. };
  132. } else {
  133. $defaultOptions['empty_data'] = '';
  134. }
  135. return $defaultOptions;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function createBuilder($name, FormFactoryInterface $factory, array $options)
  141. {
  142. return new FormBuilder($name, $factory, new EventDispatcher(), $options['data_class']);
  143. }
  144. /**
  145. * {@inheritdoc}
  146. */
  147. public function getParent(array $options)
  148. {
  149. return null;
  150. }
  151. /**
  152. * {@inheritdoc}
  153. */
  154. public function getName()
  155. {
  156. return 'field';
  157. }
  158. private function humanize($text)
  159. {
  160. return ucfirst(strtolower(str_replace('_', ' ', $text)));
  161. }
  162. }