Skip to content

Commit 518dc1c

Browse files
committed
attached() replaced with callbacks
1 parent 0cd3af0 commit 518dc1c

File tree

4 files changed

+15
-50
lines changed

4 files changed

+15
-50
lines changed

src/Forms/Controls/BaseControl.php

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ abstract class BaseControl extends Nette\ComponentModel\Component implements ICo
8383
*/
8484
public function __construct($caption = null)
8585
{
86-
$this->monitor(Form::class);
8786
parent::__construct();
8887
$this->control = Html::el('input', ['type' => null, 'name' => null]);
8988
$this->label = Html::el('label');
@@ -93,6 +92,11 @@ public function __construct($caption = null)
9392
$this->setRequired(false);
9493
}
9594
$this->setValue(null);
95+
$this->monitor(Form::class, function (Form $form) {
96+
if (!$this->isDisabled() && $form->isAnchored() && $form->isSubmitted()) {
97+
$this->loadHttpData();
98+
}
99+
});
96100
}
97101

98102

@@ -117,17 +121,6 @@ public function getCaption()
117121
}
118122

119123

120-
/**
121-
* This method will be called when the component becomes attached to Form.
122-
*/
123-
protected function attached(Nette\ComponentModel\IComponent $form): void
124-
{
125-
if (!$this->isDisabled() && $form instanceof Form && $form->isAnchored() && $form->isSubmitted()) {
126-
$this->loadHttpData();
127-
}
128-
}
129-
130-
131124
/**
132125
* Returns form.
133126
*/

src/Forms/Controls/CsrfProtection.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace Nette\Forms\Controls;
1111

1212
use Nette;
13+
use Nette\Application\UI\Presenter;
1314

1415

1516
/**
@@ -32,16 +33,12 @@ public function __construct($errorMessage)
3233
$this->setOmitted()
3334
->setRequired()
3435
->addRule(self::PROTECTION, $errorMessage);
35-
$this->monitor(Nette\Application\UI\Presenter::class);
36-
}
37-
3836

39-
protected function attached(Nette\ComponentModel\IComponent $parent): void
40-
{
41-
parent::attached($parent);
42-
if (!$this->session && $parent instanceof Nette\Application\UI\Presenter) {
43-
$this->session = $parent->getSession();
44-
}
37+
$this->monitor(Presenter::class, function (Presenter $presenter) {
38+
if (!$this->session) {
39+
$this->session = $presenter->getSession();
40+
}
41+
});
4542
}
4643

4744

src/Forms/Controls/UploadControl.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,13 @@ public function __construct($label = null, bool $multiple = false)
3434
$this->setOption('type', 'file');
3535
$this->addCondition(Forms\Form::FILLED)
3636
->addRule([$this, 'isOk'], Forms\Validator::$messages[self::VALID]);
37-
}
38-
3937

40-
/**
41-
* This method will be called when the component (or component's parent)
42-
* becomes attached to a monitored object. Do not call this method yourself.
43-
*/
44-
protected function attached(Nette\ComponentModel\IComponent $form): void
45-
{
46-
if ($form instanceof Nette\Forms\Form) {
38+
$this->monitor(Forms\Form::class, function (Forms\Form $form) {
4739
if (!$form->isMethod('post')) {
4840
throw new Nette\InvalidStateException('File upload requires method POST.');
4941
}
5042
$form->getElementPrototype()->enctype = 'multipart/form-data';
51-
}
52-
parent::attached($form);
43+
});
5344
}
5445

5546

src/Forms/Form.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,9 @@ public function __construct(string $name = null)
133133
$this[self::TRACKER_ID] = $tracker;
134134
$this->setParent(null, $name);
135135
}
136-
}
137-
138-
139-
protected function validateParent(Nette\ComponentModel\IContainer $parent): void
140-
{
141-
parent::validateParent($parent);
142-
$this->monitor(__CLASS__);
143-
}
144-
145-
146-
/**
147-
* This method will be called when the component (or component's parent)
148-
* becomes attached to a monitored object. Do not call this method yourself.
149-
*/
150-
protected function attached(Nette\ComponentModel\IComponent $obj): void
151-
{
152-
if ($obj instanceof self) {
136+
$this->monitor(__CLASS__, function () {
153137
throw new Nette\InvalidStateException('Nested forms are forbidden.');
154-
}
138+
});
155139
}
156140

157141

0 commit comments

Comments
 (0)