| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* This file is part of the Nette Framework (https://nette.org) |
| 5 |
* Copyright (c) 2004 David Grudl (https://davidgrudl.com) |
| 6 |
*/ |
| 7 |
declare (strict_types=1); |
| 8 |
namespace Packetery\Nette\Forms\Controls; |
| 9 |
|
| 10 |
use Packetery\Nette; |
| 11 |
/** |
| 12 |
* Submittable button control. |
| 13 |
* |
| 14 |
* @property-read bool $submittedBy |
| 15 |
*/ |
| 16 |
class SubmitButton extends Button implements \Packetery\Nette\Forms\SubmitterControl |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Occurs when the button is clicked and form is successfully validated |
| 20 |
* @var array<callable(self, array|object): void|callable(\Packetery\Nette\Forms\Form, array|object): void|callable(array|object): void> |
| 21 |
*/ |
| 22 |
public $onClick = []; |
| 23 |
/** @var array<callable(self): void> Occurs when the button is clicked and form is not validated */ |
| 24 |
public $onInvalidClick = []; |
| 25 |
/** @var array|null */ |
| 26 |
private $validationScope; |
| 27 |
/** |
| 28 |
* @param string|object $caption |
| 29 |
*/ |
| 30 |
public function __construct($caption = null) |
| 31 |
{ |
| 32 |
parent::__construct($caption); |
| 33 |
$this->control->type = 'submit'; |
| 34 |
$this->setOmitted(\true); |
| 35 |
} |
| 36 |
public function loadHttpData() : void |
| 37 |
{ |
| 38 |
parent::loadHttpData(); |
| 39 |
if ($this->isFilled()) { |
| 40 |
$this->getForm()->setSubmittedBy($this); |
| 41 |
} |
| 42 |
} |
| 43 |
/** |
| 44 |
* Tells if the form was submitted by this button. |
| 45 |
*/ |
| 46 |
public function isSubmittedBy() : bool |
| 47 |
{ |
| 48 |
return $this->getForm()->isSubmitted() === $this; |
| 49 |
} |
| 50 |
/** |
| 51 |
* Sets the validation scope. Clicking the button validates only the controls within the specified scope. |
| 52 |
* @return static |
| 53 |
*/ |
| 54 |
public function setValidationScope(?iterable $scope) |
| 55 |
{ |
| 56 |
if ($scope === null) { |
| 57 |
$this->validationScope = null; |
| 58 |
} else { |
| 59 |
$this->validationScope = []; |
| 60 |
foreach ($scope ?: [] as $control) { |
| 61 |
if (!$control instanceof \Packetery\Nette\Forms\Container && !$control instanceof \Packetery\Nette\Forms\Control) { |
| 62 |
throw new \Packetery\Nette\InvalidArgumentException('Validation scope accepts only \\Packetery\\Nette\\Forms\\Container or \\Packetery\\Nette\\Forms\\Control instances.'); |
| 63 |
} |
| 64 |
$this->validationScope[] = $control; |
| 65 |
} |
| 66 |
} |
| 67 |
return $this; |
| 68 |
} |
| 69 |
/** |
| 70 |
* Gets the validation scope. |
| 71 |
*/ |
| 72 |
public function getValidationScope() : ?array |
| 73 |
{ |
| 74 |
return $this->validationScope; |
| 75 |
} |
| 76 |
/** |
| 77 |
* Fires click event. |
| 78 |
*/ |
| 79 |
public function click() : void |
| 80 |
{ |
| 81 |
\Packetery\Nette\Utils\Arrays::invoke($this->onClick, $this); |
| 82 |
} |
| 83 |
public function getControl($caption = null) : \Packetery\Nette\Utils\Html |
| 84 |
{ |
| 85 |
$scope = []; |
| 86 |
foreach ((array) $this->validationScope as $control) { |
| 87 |
$scope[] = $control->lookupPath(\Packetery\Nette\Forms\Form::class); |
| 88 |
} |
| 89 |
return parent::getControl($caption)->addAttributes(['formnovalidate' => $this->validationScope !== null, 'data-nette-validation-scope' => $scope ?: null]); |
| 90 |
} |
| 91 |
} |
| 92 |
|