Skip to content

Commit

Permalink
setValidationScope WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 19, 2022
1 parent 88c7266 commit ffdffaa
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function getValues(string|object|bool|null $returnType = null, ?array $co
}

if ($controls === null && $submitter instanceof SubmitterControl) {
$controls = $submitter->getValidationScope();
$controls = $submitter->getValidationScope($this->getComponents());
}
}

Expand Down
17 changes: 11 additions & 6 deletions src/Forms/Controls/SubmitButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class SubmitButton extends Button implements Nette\Forms\SubmitterControl
public iterable $onInvalidClick = [];

private ?array $validationScope = null;
private bool $scopeMode = true;


public function __construct(string|Stringable|null $caption = null)
Expand Down Expand Up @@ -61,19 +62,18 @@ public function isSubmittedBy(): bool
/**
* Sets the validation scope. Clicking the button validates only the controls within the specified scope.
*/
public function setValidationScope(?iterable $scope): static
public function setValidationScope(?iterable $scope, $mode = true): 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;
}
$this->validationScope[] = $scope ?: [];
$this->scopeMode = $mode;
}

return $this;
Expand All @@ -83,9 +83,13 @@ public function setValidationScope(?iterable $scope): static
/**
* Gets the validation scope.
*/
public function getValidationScope(): ?array
public function getValidationScope(array $components = []): ?array
{
return $this->validationScope;
if ($this->scopeMode) {
return $this->validationScope;
} else {
return array_diff($components, $this->validationScope);
}
}


Expand All @@ -108,6 +112,7 @@ public function getControl($caption = null): Nette\Utils\Html
return parent::getControl($caption)->addAttributes([
'formnovalidate' => $this->validationScope !== null,
'data-nette-validation-scope' => $scope ?: null,
'data-nette-validation-mode' => $this->scopeMode, // or null
]);
}
}
2 changes: 1 addition & 1 deletion src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,7 @@ public function validate(?array $controls = null): void
{
$this->cleanErrors();
if ($controls === null && $this->submittedBy instanceof SubmitterControl) {
$controls = $this->submittedBy->getValidationScope();
$controls = $this->submittedBy->getValidationScope($this->getComponents());
}

$this->validateMaxPostSize();
Expand Down

0 comments on commit ffdffaa

Please sign in to comment.