|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Del\Form\Renderer\Error; |
|
6
|
|
|
|
|
7
|
|
|
use Del\Form\Field\FieldInterface; |
|
8
|
|
|
use DOMElement; |
|
9
|
|
|
|
|
10
|
|
|
class HorizontalFormErrorRender extends AbstractErrorRender |
|
11
|
|
|
{ |
|
12
|
1 |
|
public function render(FieldInterface $field): DOMElement |
|
13
|
|
|
{ |
|
14
|
1 |
|
$helpBlock = $this->createElement('span'); |
|
15
|
1 |
|
$helpBlock->setAttribute('class', 'text-danger'); |
|
16
|
|
|
|
|
17
|
1 |
|
if ($this->shouldRender($field)) { |
|
18
|
1 |
|
$helpBlock = $field->hasCustomErrorMessage() |
|
19
|
1 |
|
? $this->addCustomErrorMessage($helpBlock, $field) |
|
20
|
1 |
|
: $this->addErrorMessages($helpBlock, $field); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
1 |
|
$div = $this->createElement('div'); |
|
24
|
1 |
|
$div->setAttribute('class', 'col-sm-offset-2 col-sm-10'); |
|
25
|
1 |
|
$div->appendChild($helpBlock); |
|
26
|
1 |
|
return $div; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
1 |
|
private function addCustomErrorMessage(DOMElement $helpBlock, FieldInterface $field): DOMElement |
|
30
|
|
|
{ |
|
31
|
1 |
|
$message = $field->getCustomErrorMessage(); |
|
32
|
1 |
|
$text = $this->createText($message); |
|
33
|
1 |
|
$helpBlock->appendChild($text); |
|
34
|
|
|
|
|
35
|
1 |
|
return $helpBlock; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
1 |
|
private function addErrorMessages(DOMElement $helpBlock, FieldInterface $field): DOMElement |
|
39
|
|
|
{ |
|
40
|
1 |
|
$messages = $field->getMessages(); |
|
41
|
|
|
|
|
42
|
1 |
|
foreach ($messages as $message) { |
|
43
|
1 |
|
$helpBlock = $this->appendMessage($helpBlock, $message); |
|
44
|
|
|
} |
|
45
|
1 |
|
return $helpBlock; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
1 |
|
private function appendMessage(DOMElement $helpBlock, string $message): DOMElement |
|
49
|
|
|
{ |
|
50
|
1 |
|
$text = $this->createText($message); |
|
51
|
1 |
|
$br = $this->createLineBreak(); |
|
52
|
1 |
|
$helpBlock->appendChild($text); |
|
53
|
1 |
|
$helpBlock->appendChild($br); |
|
54
|
1 |
|
return $helpBlock; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|