FieldRenderer.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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\Renderer;
  11. use Symfony\Component\Form\FieldInterface;
  12. use Symfony\Component\Form\Renderer\Engine\EngineInterface;
  13. class FieldRenderer implements RendererInterface
  14. {
  15. private $field;
  16. private $engine;
  17. public function __construct(FieldInterface $field, EngineInterface $engine)
  18. {
  19. $this->field = $field;
  20. $this->engine = $engine;
  21. }
  22. protected function getField()
  23. {
  24. return $this->field;
  25. }
  26. protected function getEngine()
  27. {
  28. return $this->engine;
  29. }
  30. public function __toString()
  31. {
  32. return $this->widget();
  33. }
  34. /**
  35. * Renders the HTML enctype in the form tag, if necessary
  36. *
  37. * Example usage in Twig templates:
  38. *
  39. * <form action="..." method="post" {{ form.render.enctype }}>
  40. *
  41. * @param Form $form The form for which to render the encoding type
  42. */
  43. public function enctype()
  44. {
  45. return $this->field->isMultipart() ? 'enctype="multipart/form-data"' : '';
  46. }
  47. /**
  48. * Renders a field row.
  49. *
  50. * @param FieldInterface $field The field to render as a row
  51. */
  52. public function row()
  53. {
  54. return $this->engine->render($this->field, 'field_row', array(
  55. 'child' => $this->field,
  56. ));
  57. }
  58. /**
  59. * Renders the HTML for an individual form field
  60. *
  61. * Example usage in Twig:
  62. *
  63. * {{ form_field(field) }}
  64. *
  65. * You can pass attributes element during the call:
  66. *
  67. * {{ form_field(field, {'class': 'foo'}) }}
  68. *
  69. * Some fields also accept additional variables as parameters:
  70. *
  71. * {{ form_field(field, {}, {'separator': '+++++'}) }}
  72. *
  73. * @param FieldInterface $field The field to render
  74. * @param array $attributes HTML attributes passed to the template
  75. * @param array $parameters Additional variables passed to the template
  76. * @param array|string $resources A resource or array of resources
  77. */
  78. public function widget(array $attributes = array(), array $parameters = array(), $resources = null)
  79. {
  80. if (null !== $resources && !is_array($resources)) {
  81. $resources = array($resources);
  82. }
  83. return $this->engine->render($this->field, 'field', array(
  84. 'field' => $this->field,
  85. 'attr' => $attributes,
  86. 'params' => $parameters,
  87. ), $resources);
  88. }
  89. /**
  90. * Renders all hidden fields of the given field group
  91. *
  92. * @param FormInterface $group The field group
  93. * @param array $params Additional variables passed to the
  94. * template
  95. */
  96. public function hidden(array $parameters = array())
  97. {
  98. return $this->engine->render($this->field, 'hidden', array(
  99. 'field' => $this->field,
  100. 'params' => $parameters,
  101. ));
  102. }
  103. /**
  104. * Renders the errors of the given field
  105. *
  106. * @param FieldInterface $field The field to render the errors for
  107. * @param array $params Additional variables passed to the template
  108. */
  109. public function errors(array $parameters = array())
  110. {
  111. return $this->engine->render($this->field, 'errors', array(
  112. 'field' => $this->field,
  113. 'params' => $parameters,
  114. ));
  115. }
  116. /**
  117. * Renders the label of the given field
  118. *
  119. * @param FieldInterface $field The field to render the label for
  120. * @param array $params Additional variables passed to the template
  121. */
  122. public function label($label = null, array $parameters = array())
  123. {
  124. return $this->render($this->field, 'label', array(
  125. 'field' => $this->field,
  126. 'params' => $parameters,
  127. 'label' => null !== $label ? $label : ucfirst(strtolower(str_replace('_', ' ', $this->field->getKey()))),
  128. ));
  129. }
  130. }