-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathForm.php
More file actions
145 lines (122 loc) · 3.59 KB
/
Copy pathForm.php
File metadata and controls
145 lines (122 loc) · 3.59 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace ADT\Forms;
use Nette;
use Nette\Application\UI\Presenter;
use Nette\Forms\Controls\BaseControl;
use stdClass;
class Form extends Nette\Application\UI\Form
{
use AnnotationsTrait;
use GetComponentTrait;
use SectionTrait;
use ElementsTrait;
const string GROUP_LEVEL_SEPARATOR = '-';
private ?BootstrapFormRenderer $renderer = null;
/** @var Nette\Forms\ControlGroup[] */
public array $ancestorGroups = [];
public function __construct(?Nette\ComponentModel\IContainer $parent = null, ?string $name = null)
{
parent::__construct($parent, $name);
$this->monitor(Presenter::class, function() {
// must be called here because onError and onRender callbacks are set in the constructor
$this->getRenderer();
});
}
public function getRenderer(): BootstrapFormRenderer
{
if ($this->renderer === null) {
$this->renderer = new BootstrapFormRenderer($this);
}
return $this->renderer;
}
/**
* Adds global error message.
* @param string|object $message
*/
public function addError($message, bool $translate = true): void
{
if ($translate && $this->getTranslator()) {
$message = $this->getTranslator()->translate($message);
}
parent::addError($message, false);
}
public function watchForSubmit(BaseControl $control): void
{
$control->setHtmlAttribute('data-adt-redraw-snippet', $this->addSubmit('submit')->getHtmlName());
}
/**
* Returns only validated values from the form.
* Values from controls that have validation errors are excluded.
*/
public function getValidatedValues(string|object|bool|null $returnType = null): object|array
{
$allValues = $this->getUntrustedValues($returnType);
return $this->filterValidValues($this, $allValues);
}
private function filterValidValues(Nette\Forms\Container $container, $values): array|stdClass
{
$result = is_array($values) ? [] : new stdClass;
$isArray = is_array($values);
foreach ($container->getComponents() as $name => $component) {
$name = (string) $name;
// Zkontroluj, jestli hodnota existuje
if ($isArray) {
if (!array_key_exists($name, $values)) {
continue;
}
$value = $values[$name];
} else {
if (!property_exists($values, $name)) {
continue;
}
$value = $values->$name;
}
if ($component instanceof Nette\Forms\Container) {
// Rekurzivně filtruj vnořený kontejner
$filtered = $this->filterValidValues($component, $value);
if (is_array($result)) {
$result[$name] = $filtered;
} else {
$result->$name = $filtered;
}
} elseif ($component instanceof BaseControl) {
// Zkontroluj, jestli je control validní
if ($this->isControlValid($component)) {
if (is_array($result)) {
$result[$name] = $value;
} else {
$result->$name = $value;
}
}
}
}
return $result;
}
private function isControlValid(BaseControl $control): bool
{
if ($control->isDisabled()) {
return false;
}
$rules = $control->getRules();
$emptyOptional = !$rules->isRequired() && !$control->isFilled();
return $this->validateBranch($rules, $emptyOptional);
}
private function validateBranch(Nette\Forms\Rules $branch, bool $emptyOptional): bool
{
foreach ($branch as $rule) {
if (!$rule->branch && $emptyOptional && $rule->validator !== Nette\Forms\Form::Filled) {
continue;
}
$success = $branch::validateRule($rule);
if (!$success && !$rule->branch) {
return false;
}
if ($success && $rule->branch) {
if (!$this->validateBranch($rule->branch, $rule->validator === Nette\Forms\Form::Blank ? false : $emptyOptional)) {
return false;
}
}
}
return true;
}
}