Skip to content

Commit 71cc347

Browse files
committed
Mostly revert "Fixes for nested multipliers (#59)"
This partially reverts commit 39725d3. The commit introduced a regression in `testGroupManualRenderWithButtons`, changed the internal architecture in a way that made it harder to reason about (the Multiplier inconsistently took over some responsibilities of ComponentResolver). Additionally, it applied the following changes that are unrelated to the purported fix and not really necessary: - Latte 2 macros are trivial to migrate and unclearly named, no need to keep them for BC. - The onSuccess handlers were already fixed in ed96ba0. - `testGroupManualRenderWithButtons` look like remnants of debugging effort, they do not fix the test. We are only keeping the following: - Test for nested support (to be implemented later). - Fix for `assertMatchesRegularExpression` deprecation in PHPUnit. For reference, the reverted change is the following commit that was squashed into 39725d3, except for the change to `testSendNested`: 2c33de2
1 parent bfe52eb commit 71cc347

File tree

6 files changed

+34
-78
lines changed

6 files changed

+34
-78
lines changed

src/ComponentResolver.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,22 @@ final class ComponentResolver
2121
/** @var mixed[] */
2222
private ?array $purgedHttpData = null;
2323

24+
/** @var mixed[] */
25+
private array $defaults = [];
26+
2427
private int $minCopies;
2528

2629
private bool $reached = false;
2730

2831
/**
2932
* @param mixed[] $httpData
33+
* @param mixed[] $defaults
3034
*/
31-
public function __construct(array $httpData, ?int $maxCopies, int $minCopies)
35+
public function __construct(array $httpData, array $defaults, ?int $maxCopies, int $minCopies)
3236
{
3337
$this->httpData = $httpData;
3438
$this->maxCopies = $maxCopies;
39+
$this->defaults = $defaults;
3540
$this->minCopies = $minCopies;
3641

3742
foreach ($httpData as $index => $_) {
@@ -66,7 +71,7 @@ public function getCreateNum(): int
6671
*/
6772
public function getDefaults(): array
6873
{
69-
return array_slice([], 0, $this->maxCopies, true);
74+
return array_slice($this->defaults, 0, $this->maxCopies, true);
7075
}
7176

7277
/**

src/Latte/Extension/MultiplierExtension.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ public function getTags(): array
2020
'n:multiplier' => [MultiplierNode::class, 'create'],
2121
'multiplier:remove' => [MultiplierRemoveNode::class, 'create'],
2222
'multiplier:add' => [MultiplierAddNode::class, 'create'],
23-
'btnRemove' => [MultiplierRemoveNode::class, 'create'],
24-
'btnCreate' => [MultiplierAddNode::class, 'create'],
2523
];
2624
}
2725

src/Multiplier.php

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class Multiplier extends Container
5252

5353
protected ?RemoveButton $removeButton = null;
5454

55+
/** @var mixed[] */
56+
protected array $httpData = [];
57+
5558
protected ?int $maxCopies = null;
5659

5760
protected int $totalCopies = 0;
@@ -63,8 +66,6 @@ class Multiplier extends Container
6366
/** @var Container[] */
6467
protected array $noValidate = [];
6568

66-
protected ComponentResolver $resolver;
67-
6869
private ?Form $form = null;
6970

7071
private bool $attachedCalled = false;
@@ -211,33 +212,28 @@ public function addCopy(?int $number = null, array|object $defaults = []): Conta
211212
return $container;
212213
}
213214

214-
public function createCopies(bool $forceValues = false): void
215+
public function createCopies(): void
215216
{
216217
if ($this->created === true) {
217218
return;
218219
}
219220

220221
$this->created = true;
221222

222-
if (!isset($this->resolver)) {
223-
$this->resolver = new ComponentResolver($this->values, $this->maxCopies, $this->minCopies);
224-
}
223+
$resolver = new ComponentResolver($this->httpData, $this->values, $this->maxCopies, $this->minCopies);
225224

226225
$this->attachCreateButtons();
227-
$this->createComponents($forceValues);
226+
$this->createComponents($resolver);
228227
$this->detachCreateButtons();
229228

230229
if ($this->maxCopies === null || $this->totalCopies < $this->maxCopies) {
231230
$this->attachCreateButtons();
232231
}
233232

234-
if (
235-
$this->form !== null &&
236-
$this->resolver->isRemoveAction() &&
237-
$this->totalCopies >= $this->minCopies &&
238-
!$this->resolver->reachedMinLimit()
239-
) {
240-
$this->form->setSubmittedBy($this->removeButton->create($this));
233+
if ($this->form !== null && $resolver->isRemoveAction() && $this->totalCopies >= $this->minCopies && !$resolver->reachedMinLimit()) {
234+
/** @var RemoveButton $removeButton */
235+
$removeButton = $this->removeButton;
236+
$this->form->setSubmittedBy($removeButton->create($this));
241237

242238
$this->resetFormEvents();
243239

@@ -337,12 +333,10 @@ public function setValues(array|object $values, bool $erase = false, bool $onlyD
337333
if ($this->created) {
338334
foreach ($this->getContainers() as $container) {
339335
$this->removeComponent($container);
340-
$this->totalCopies--;
341336
}
342337

343338
$this->created = false;
344339
$this->detachCreateButtons();
345-
$this->resolver = new ComponentResolver($this->values, $this->maxCopies, $this->minCopies);
346340
$this->createCopies();
347341
}
348342

@@ -389,11 +383,10 @@ protected function isFormSubmitted(): bool
389383

390384
protected function loadHttpData(): void
391385
{
392-
if ($this->isFormSubmitted()) {
386+
if ($this->form !== null && $this->isFormSubmitted()) {
393387
/** @var array<mixed> $httpData The other types from the union can only be returned when the htmlName argument is passed. https://github.com/nette/forms/pull/333 */
394-
$httpData = $this->getForm()->getHttpData();
395-
$httpData = Arrays::get($httpData, $this->getHtmlName(), []);
396-
$this->resolver = new ComponentResolver($httpData ?? [], $this->maxCopies, $this->minCopies);
388+
$httpData = $this->form->getHttpData();
389+
$this->httpData = (array) Arrays::get($httpData, $this->getHtmlName(), []);
397390
}
398391
}
399392

@@ -457,34 +450,31 @@ protected function removeComponentProperly(IComponent $component): void
457450
$this->removeComponent($component);
458451
}
459452

460-
private function createComponents(bool $forceValues = false): void
453+
private function createComponents(ComponentResolver $resolver): void
461454
{
462455
$containers = [];
463-
$containerDefaults = $this->createContainer()->getValues(self::Array);
464456

465457
// Components from httpData
466-
if ($this->isFormSubmitted() && !$forceValues) {
467-
foreach ($this->resolver->getValues() as $number => $_) {
458+
if ($this->isFormSubmitted()) {
459+
foreach ($resolver->getValues() as $number => $_) {
468460
$containers[] = $container = $this->addCopy($number);
469461

470462
/** @var BaseControl $control */
471-
foreach ($container->getComponents(false, Control::class) as $control) {
463+
foreach ($container->getControls() as $control) {
472464
$control->loadHttpData();
473465
}
474466
}
475467
} else { // Components from default values
476-
foreach ($this->resolver->getValues() as $number => $values) {
477-
$containers[] = $container = $this->addCopy($number, $values);
478-
$container->setValues($values);
468+
foreach ($resolver->getDefaults() as $number => $values) {
469+
$containers[] = $this->addCopy($number, $values);
479470
}
480471
}
481472

482473
// Default number of copies
483-
if (!$this->values) {
474+
if (!$this->isFormSubmitted() && !$this->values) {
484475
$copyNumber = $this->copyNumber;
485-
while ($copyNumber > 0 && $this->isValidMaxCopies() && $this->totalCopies < $this->minCopies) {
476+
while ($copyNumber > 0 && $this->isValidMaxCopies()) {
486477
$containers[] = $container = $this->addCopy();
487-
$container->setValues($containerDefaults);
488478
$copyNumber--;
489479
}
490480
}
@@ -495,11 +485,11 @@ private function createComponents(bool $forceValues = false): void
495485
}
496486

497487
// New containers, if create button hitted
498-
if ($this->form !== null && $this->resolver->isCreateAction() && $this->form->isValid()) {
499-
$count = $this->resolver->getCreateNum();
488+
if ($this->form !== null && $resolver->isCreateAction() && $this->form->isValid()) {
489+
$count = $resolver->getCreateNum();
500490
while ($count > 0 && $this->isValidMaxCopies()) {
501491
$this->noValidate[] = $containers[] = $container = $this->addCopy();
502-
$container->setValues($containerDefaults);
492+
$container->setValues($this->createContainer()->getValues(self::Array));
503493
$count--;
504494
}
505495
}

tests/Unit/CreateButtonTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,7 @@ public function testCallback()
9494
$submitter->setHtmlAttribute('class', 'add-btn');
9595
});
9696

97-
$response = $this->services->form->createRequest($factory
98-
->formModifier(function (\Nette\Application\UI\Form $form) {
99-
$form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () {
100-
};
101-
})->createForm())->setPost([
97+
$response = $this->services->form->createRequest($factory->createForm())->setPost([
10298
'm' => [
10399
['bar' => ''],
104100
['bar' => ''],

tests/Unit/MultiplierTest.php

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ public function testSendBase()
4949
$this->parameters['onCreate'][] = $container;
5050
};
5151
})
52-
->formModifier(function (Form $form) {
53-
$form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () {
54-
};
55-
})
5652
->createForm()
5753
)
5854
->setPost($params = [
@@ -102,10 +98,6 @@ public function testSendCopy2()
10298
$this->parameters['onCreate'][] = $container;
10399
};
104100
})
105-
->formModifier(function (Form $form) {
106-
$form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () {
107-
};
108-
})
109101
->createForm()
110102
)
111103
->setPost($params = [
@@ -156,10 +148,6 @@ public function testSendMaxCopy()
156148
$this->parameters['onCreate'][] = $container;
157149
};
158150
})
159-
->formModifier(function (Form $form) {
160-
$form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () {
161-
};
162-
})
163151
->createForm()
164152
)
165153
->setPost([
@@ -226,10 +214,6 @@ public function testSendNested()
226214
}));
227215
$container['m2']->addCreateButton('create');
228216
})
229-
->formModifier(function (Form $form) {
230-
$form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () {
231-
};
232-
})
233217
->createForm()
234218
);
235219
$request->setPost([
@@ -396,15 +380,14 @@ public function testGroupManualRenderWithButtons()
396380
->multiplierModifier(function (Multiplier $multiplier) {
397381
$multiplier->onCreate[] = function (Container $container) {
398382
$this->parameters['onCreate'][] = $container;
399-
$container->setParent(null, 'X');
400-
//var_dump($container);
401383
};
402384
$multiplier->addCreateButton();
403385
$multiplier->addRemoveButton();
404-
//$multiplier->setMinCopies(1);
386+
$multiplier->setMinCopies(1);
405387
})
406388
->createForm());
407389
$dom = $request->render(__DIR__ . '/templates/group.latte')->toDomQuery();
390+
408391
$this->assertDomHas($dom, 'input[name="m[0][multiplier_remover]"]');
409392
$this->assertDomHas($dom, 'input[name="m[1][multiplier_remover]"]');
410393
}
@@ -473,10 +456,6 @@ public function testPromptSelect()
473456
->setPrompt('Select');
474457
})
475458
->addCreateButton()
476-
->formModifier(function (Form $form) {
477-
$form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () {
478-
};
479-
})
480459
->createForm()
481460
)
482461
->setPost($params = [

tests/Unit/RemoveButtonTest.php

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,6 @@ public function testAddClass()
193193
$submitter->setHtmlAttribute('class', 'btn btn-remove');
194194
})
195195
->addCreateButton()
196-
->formModifier(function (Form $form) {
197-
$form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () {
198-
};
199-
})
200196
->createForm()
201197
)->setPost([
202198
'm' => [
@@ -219,10 +215,6 @@ public function testDeleteLastElementToZero()
219215
->setMinCopies(0)
220216
->addRemoveButton()
221217
->addCreateButton()
222-
->formModifier(function (Form $form) {
223-
$form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () {
224-
};
225-
})
226218
->createForm()
227219
)->modifyForm(function (Form $form) {
228220
$form['m']->setValues([
@@ -252,10 +244,6 @@ public function testOnRemoveEvent()
252244
$called = true;
253245
};
254246
})
255-
->formModifier(function (Form $form) {
256-
$form->onSuccess[] = $form->onError[] = $form->onSubmit[] = function () {
257-
};
258-
})
259247
->createForm()
260248
)->setPost([
261249
'm' => [

0 commit comments

Comments
 (0)