-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBaseContainer.php
More file actions
96 lines (73 loc) · 2.35 KB
/
Copy pathBaseContainer.php
File metadata and controls
96 lines (73 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
namespace ADT\Forms;
use Closure;
use Nette\Forms\Container;
abstract class BaseContainer extends Container
{
use AnnotationsTrait;
// because there is no "addError" method in Container class
// we have to create an IControl instance and call "addError" on it
// the control must not be an instance of "HiddenField"
// otherwise the error will be added to the form instead of the container
const string ERROR_CONTROL_NAME = '_containerError_';
private array $options = [];
private ?string $requiredMessage = null;
/**
* @param string|null $message
* @return static
*/
public function setRequired(?string $message): static
{
$this->requiredMessage = $message;
return $this;
}
protected function getRequiredMessage(): ?string
{
return $this->requiredMessage;
}
protected function isRequired(): bool
{
return (bool) $this->getRequiredMessage();
}
public function setOption($key, $value): static
{
if ($value === null) {
unset($this->options[$key]);
} else {
$this->options[$key] = $value;
}
return $this;
}
public function getOption($key, $default = null)
{
return $this->options[$key] ?? $default;
}
public function getOptions(): array
{
return $this->options;
}
public function addError($message, bool $translate = true): void
{
$this->addText(static::ERROR_CONTROL_NAME)
->addError($message, $translate);
}
public static function register(): void
{
Container::extensionMethod('addStaticContainer', function (Container $_this, string $name, Closure $factory, ?string $isFilledComponentName = null, ?string $isRequiredMessage = null) {
$control = (new StaticContainerFactory($name, $factory, $isFilledComponentName))
->create()
->setRequired($isRequiredMessage);
$control->currentGroup = $_this->currentGroup;
$_this->currentGroup?->add($control);
return $_this[$name] = $control;
});
Container::extensionMethod('addDynamicContainer', function (Container $_this, string $name, Closure $factory, ?string $isFilledComponentName = null, ?string $isRequiredMessage = null) {
$control = (new DynamicContainer)
->setStaticContainerFactory(new StaticContainerFactory($name, $factory, $isFilledComponentName))
->setRequired($isRequiredMessage);
$control->currentGroup = $_this->currentGroup;
$_this->currentGroup?->add($control);
return $_this[$name] = $control;
});
}
}