Skip to content

Commit

Permalink
added event onCreateComponents, addCopy is public [Fixed #31]
Browse files Browse the repository at this point in the history
  • Loading branch information
MartkCz committed Jul 25, 2018
1 parent 9e5d3f1 commit b90847e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
20 changes: 12 additions & 8 deletions src/Multiplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ class Multiplier extends Container {
/** @var callable[] */
public $onCreate = [];

/** @var callable[] */
public $onCreateComponents = [];

/** @var Container[] */
protected $noValidate = [];

Expand Down Expand Up @@ -245,7 +248,7 @@ public function validate(array $controls = null) {
* @param array|ArrayHash $defaults
* @return Container|null
*/
protected function addCopy($number = null, $defaults = []) {
public function addCopy($number = null, $defaults = []) {
if (!is_numeric($number)) {
$number = $this->createNumber();
} else if ($component = parent::getComponent($number, false)) {
Expand All @@ -258,6 +261,9 @@ protected function addCopy($number = null, $defaults = []) {
$container->setDefaults($defaults, $this->erase);
}
$this->attachContainer($container, $number);
$this->attachRemoveButton($container);

$this->totalCopies++;

return $container;
}
Expand All @@ -268,30 +274,29 @@ private function createComponents(ComponentResolver $resolver) {
// Create components with values
foreach ($resolver->getValuesForComponents() as $number => $values) {
$containers[] = $container = $this->addCopy($number, $values);
$this->attachRemoveButton($container);
$this->totalCopies++;
}

// Default number of copies
if (!$this->isSubmitted() && !$this->values) {
$copyNumber = $this->copyNumber;
while ($copyNumber > 0 && $this->isValidMaxCopies()) {
$containers[] = $container = $this->addCopy();
$this->attachRemoveButton($container);
$this->applyDefaultValues($container);
$this->totalCopies++;
$copyNumber--;
}
}

// Dynamic
foreach ($this->onCreateComponents as $callback) {
$callback($this);
}

// New containers, if create button hitted
if ($resolver->isCreateAction() && $this->form->isValid()) {
$count = $resolver->getCreateNum();
while ($count > 0 && $this->isValidMaxCopies()) {
$this->noValidate[] = $containers[] = $container = $this->addCopy();
$this->attachRemoveButton($container);
$this->applyDefaultValues($container);
$this->totalCopies++;
$count--;
}
}
Expand Down Expand Up @@ -326,7 +331,6 @@ public function createCopies() {

if ($resolver->isRemoveAction() && $this->totalCopies >= $this->minCopies && !$resolver->reachedMinLimit()) {
$container = $this->addCopy($resolver->getRemoveId());
$this->attachRemoveButton($container);
$container->getComponent(self::SUBMIT_REMOVE_NAME)->onClick[] = function () use ($container) {
$this->removeAllComponents($container);

Expand Down
14 changes: 14 additions & 0 deletions tests/unit/MultiplierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,18 @@ public function testOnCreateEvent() {
}
}

public function testAddDynamic() {
$request = $this->services->form->createRequest('base')->modifyForm(function (Form $form) {
$form['m']->onCreateComponents[] = function (Multiplier $multiplier) {
$multiplier->addCopy(99)['bar']->setHtmlAttribute('class', 'myClass');
};
});


$dom = $request->render()->toDomQuery();

$this->assertDomHas($dom, '[name="m[99][bar]"]');
$this->assertDomHas($dom, 'input.myClass');
}

}

0 comments on commit b90847e

Please sign in to comment.