1 | <?php |
||
20 | abstract class AbstractFormRenderer implements FormRendererInterface |
||
21 | { |
||
22 | /** @var DOMDocument $dom */ |
||
23 | protected $dom; |
||
24 | |||
25 | /** @var DomElement $form */ |
||
26 | protected $form; |
||
27 | |||
28 | /** @var bool $displayErrors */ |
||
29 | protected $displayErrors; |
||
30 | |||
31 | /** @var ErrorRendererInterface $errorRenderer */ |
||
32 | protected $errorRenderer; |
||
33 | |||
34 | /** @var DomElement $label The label element*/ |
||
35 | protected $label; |
||
36 | |||
37 | /** @var DomElement $element the field element */ |
||
38 | protected $element; |
||
39 | |||
40 | /** @var DomElement $errors The error block html*/ |
||
41 | protected $errors; |
||
42 | |||
43 | /** @var DomElement $block The containing html block */ |
||
44 | protected $block; |
||
45 | |||
46 | /** @var FieldInterface $field The current field being processed */ |
||
47 | protected $field; |
||
48 | |||
49 | 24 | public function __construct() |
|
55 | |||
56 | /** |
||
57 | * @param FormInterface $form |
||
58 | * @param bool $displayErrors |
||
59 | * @return string |
||
60 | */ |
||
61 | 11 | public function render(FormInterface $form, $displayErrors = true) |
|
72 | |||
73 | /** |
||
74 | * @param FormInterface $form |
||
75 | */ |
||
76 | 11 | private function setFormAttributes(FormInterface $form) |
|
77 | { |
||
78 | 11 | $attributes = $form->getAttributes(); |
|
79 | 11 | foreach ($attributes as $key => $value) { |
|
80 | 11 | $this->form->setAttribute($key, $value); |
|
81 | } |
||
82 | |||
83 | // set Id as name or method as post if not set |
||
84 | 11 | $method = $this->getMethod($form); |
|
85 | 11 | $id = $this->getId($form); |
|
86 | |||
87 | 11 | $this->form->setAttribute('id', $id); |
|
88 | 11 | $this->form->setAttribute('method', $method); |
|
89 | 11 | } |
|
90 | |||
91 | /** |
||
92 | * @param FormInterface $form |
||
93 | * @return string |
||
94 | */ |
||
95 | 11 | private function getMethod(FormInterface $form) |
|
99 | |||
100 | /** |
||
101 | * @param FormInterface $form |
||
102 | * @return string |
||
103 | */ |
||
104 | 11 | private function getId(FormInterface $form) |
|
108 | |||
109 | 11 | private function processFields(FieldCollection $fields) |
|
110 | { |
||
111 | 11 | $fields->rewind(); |
|
112 | 11 | while ($fields->valid()) { |
|
113 | 10 | $this->block = $this->dom->createElement('div'); |
|
114 | 10 | $this->field = $fields->current(); |
|
115 | 10 | $this->label = $this->renderFieldLabel(); |
|
116 | 10 | $this->element = $this->field->getRenderer()->render($this->dom, $this->field); |
|
117 | 9 | $this->errors = $this->field->isValid() ? null : $this->renderError(); |
|
118 | 9 | $this->block = $this->renderFieldBlock(); |
|
119 | 9 | $this->form->appendChild($this->block); |
|
120 | 9 | $fields->next(); |
|
121 | } |
||
122 | 10 | $fields->rewind(); |
|
123 | 10 | } |
|
124 | |||
125 | |||
126 | |||
127 | /** |
||
128 | * @return \DOMElement|null |
||
129 | */ |
||
130 | 5 | public function renderError() |
|
131 | { |
||
132 | 5 | $errorBlock = null; |
|
133 | 5 | if ($this->errorRenderer->shouldRender($this->field) && $this->displayErrors === true) { |
|
134 | 3 | $this->block->setAttribute('class', 'has-error '); |
|
135 | 3 | $errorBlock = $this->errorRenderer->render($this->field); |
|
136 | } |
||
137 | 5 | return $errorBlock; |
|
138 | } |
||
139 | |||
140 | /** |
||
141 | * @return \DOMElement |
||
142 | */ |
||
143 | 10 | protected function createLabelElement() |
|
144 | { |
||
145 | 10 | $label = $this->dom->createElement('label'); |
|
146 | 10 | $label->setAttribute('for', $this->field->getId()); |
|
147 | 10 | if ($this->field->isRequired()) { |
|
148 | 3 | $label = $this->addRequiredAsterisk($label); |
|
149 | } |
||
150 | 10 | return $label; |
|
151 | } |
||
152 | |||
153 | |||
154 | 3 | public function addRequiredAsterisk(DomElement $label) |
|
163 | |||
164 | } |