From b94c0caf7df8d7f887c5c25c3ed42a737beb5eef Mon Sep 17 00:00:00 2001 From: David Grudl Date: Sun, 18 Feb 2024 01:55:02 +0100 Subject: [PATCH] SubmitButton::setValidationScope() accepts strings --- src/Forms/Controls/SubmitButton.php | 23 ++++++++++++++--------- tests/Forms/Forms.validationScope.phpt | 2 +- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/Forms/Controls/SubmitButton.php b/src/Forms/Controls/SubmitButton.php index 69d7eb98a..fd7cdc2f7 100644 --- a/src/Forms/Controls/SubmitButton.php +++ b/src/Forms/Controls/SubmitButton.php @@ -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 */ 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 */ public function getValidationScope(): ?array { diff --git a/tests/Forms/Forms.validationScope.phpt b/tests/Forms/Forms.validationScope.phpt index 4dfc8a56b..df12ef058 100644 --- a/tests/Forms/Forms.validationScope.phpt +++ b/tests/Forms/Forms.validationScope.phpt @@ -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]]);