Skip to content

Commit

Permalink
SubmitButton::setValidationScope() accepts strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Feb 25, 2024
1 parent 0207b0c commit b94c0ca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
23 changes: 14 additions & 9 deletions src/Forms/Controls/SubmitButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,33 @@ public function isSubmittedBy(): bool

/**
* Sets the validation scope. Clicking the button validates only the controls within the specified scope.
* @param ?iterable<Nette\Forms\Control|Nette\Forms\Container|string>
*/
public function setValidationScope(?iterable $scope): static
{
if ($scope === null) {
$this->validationScope = null;
} else {
$this->validationScope = [];
foreach ($scope ?: [] as $control) {
if (!$control instanceof Nette\Forms\Container && !$control instanceof Nette\Forms\Control) {
throw new Nette\InvalidArgumentException('Validation scope accepts only Nette\Forms\Container or Nette\Forms\Control instances.');
}

$this->validationScope[] = $control;
}
return $this;
}

$this->validationScope = [];
foreach ($scope as $control) {
if (is_string($control)) {
$control = $this->getForm()->getComponent($control);
}
if (!$control instanceof Nette\Forms\Container && !$control instanceof Nette\Forms\Control) {
throw new Nette\InvalidArgumentException('Validation scope accepts only Nette\Forms\Container or Nette\Forms\Control instances.');
}

$this->validationScope[] = $control;
}
return $this;
}


/**
* Gets the validation scope.
* @return ?array<Nette\Forms\Control|Nette\Forms\Container>
*/
public function getValidationScope(): ?array
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Forms/Forms.validationScope.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ foreach ($datasets as $case) {
$form->addSubmit('send1');
$form->addSubmit('send2')->setValidationScope([]);
$form->addSubmit('send3')->setValidationScope([$form['name']]);
$form->addSubmit('send4')->setValidationScope([$form['details']['age']]);
$form->addSubmit('send4')->setValidationScope(['details-age']);
$form->addSubmit('send5')->setValidationScope([$form['details']]);

$form->setSubmittedBy($form[$case[0]]);
Expand Down

0 comments on commit b94c0ca

Please sign in to comment.