Skip to content

Commit

Permalink
ControlGroup: added remove() and removeOrphans() [Closes #155]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jun 19, 2017
1 parent f088f45 commit 78f11ed
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Forms/ControlGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ public function add(...$items)
}


public function remove(IControl $control): void
{
$this->controls->detach($control);
}


public function removeOrphans(): void
{
foreach ($this->controls as $control) {
if (!$control->getForm(FALSE)) {
$this->controls->detach($control);
}
}
}


/**
* @return IControl[]
*/
Expand Down
23 changes: 23 additions & 0 deletions tests/Forms/ControlGroup.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,26 @@ Assert::equal(1, count($group->getControls()));
Assert::equal([$form['outer']['inner']['text']], $group->getControls());

Assert::equal(6, count($form->getGroups()));


// remove()
$form = new Nette\Forms\Form;
$group = $form->addGroup();
$form->addText('name');

Assert::equal(1, count($group->getControls()));
$group->remove($form['name']);
Assert::equal(0, count($group->getControls()));


// removeOrphans()
$form = new Nette\Forms\Form;
$group = $form->addGroup();
$form->addText('orphan');
$form->addText('name');
unset($form['orphan']);

Assert::equal(2, count($group->getControls()));
$group->removeOrphans();
Assert::equal(1, count($group->getControls()));
Assert::same($form['name'], $group->getControls()[0]);

0 comments on commit 78f11ed

Please sign in to comment.