diff --git a/src/Forms/InvisibleReCaptchaField.php b/src/Forms/InvisibleReCaptchaField.php index 99b57f8..ceab5f0 100644 --- a/src/Forms/InvisibleReCaptchaField.php +++ b/src/Forms/InvisibleReCaptchaField.php @@ -5,7 +5,7 @@ use Contributte\ReCaptcha\ReCaptchaProvider; use Nette\Forms\Controls\HiddenField; use Nette\Forms\Form; -use Nette\InvalidStateException; +use Nette\Forms\Rules; use Nette\Utils\Html; class InvisibleReCaptchaField extends HiddenField @@ -17,6 +17,9 @@ class InvisibleReCaptchaField extends HiddenField /** @var bool */ private $configured = false; + /** @var string|null */ + private $message; + public function __construct(ReCaptchaProvider $provider, ?string $message = null) { parent::__construct(); @@ -26,9 +29,7 @@ public function __construct(ReCaptchaProvider $provider, ?string $message = null $this->control = Html::el('div'); $this->control->addClass('g-recaptcha'); - if ($message !== null) { - $this->setMessage($message); - } + $this->message = $message; } public function loadHttpData(): void @@ -39,17 +40,33 @@ public function loadHttpData(): void public function setMessage(string $message): self { - if ($this->configured === true) { - throw new InvalidStateException('Please call setMessage() only once or don\'t pass $message over addInvisibleReCaptcha()'); + $this->message = $message; + return $this; + } + + public function validate(): void + { + $this->configureValidation(); + parent::validate(); + } + + public function getRules(): Rules + { + $this->configureValidation(); + return parent::getRules(); + } + + private function configureValidation(): void + { + if ($this->configured) { + return; } + $message = $this->message ?? 'Are you a bot?'; $this->addRule(function ($code) { return $this->verify() === true; }, $message); - $this->configured = true; - - return $this; } public function verify(): bool @@ -59,6 +76,8 @@ public function verify(): bool public function getControl(?string $caption = null): Html { + $this->configureValidation(); + $el = parent::getControl(); $el->addAttributes([ 'data-sitekey' => $this->provider->getSiteKey(), diff --git a/src/Forms/ReCaptchaField.php b/src/Forms/ReCaptchaField.php index ac62686..3aa677c 100644 --- a/src/Forms/ReCaptchaField.php +++ b/src/Forms/ReCaptchaField.php @@ -5,7 +5,7 @@ use Contributte\ReCaptcha\ReCaptchaProvider; use Nette\Forms\Controls\TextInput; use Nette\Forms\Form; -use Nette\InvalidStateException; +use Nette\Forms\Rules; use Nette\Utils\Html; class ReCaptchaField extends TextInput @@ -17,6 +17,9 @@ class ReCaptchaField extends TextInput /** @var bool */ private $configured = false; + /** @var string|null */ + private $message; + public function __construct(ReCaptchaProvider $provider, ?string $label = null, ?string $message = null) { parent::__construct($label); @@ -26,9 +29,7 @@ public function __construct(ReCaptchaProvider $provider, ?string $label = null, $this->control = Html::el('div'); $this->control->addClass('g-recaptcha'); - if ($message !== null) { - $this->setMessage($message); - } + $this->message = $message; } public function loadHttpData(): void @@ -38,17 +39,33 @@ public function loadHttpData(): void public function setMessage(string $message): self { - if ($this->configured === true) { - throw new InvalidStateException('Please call setMessage() only once or don\'t pass $message over addReCaptcha()'); + $this->message = $message; + return $this; + } + + public function validate(): void + { + $this->configureValidation(); + parent::validate(); + } + + public function getRules(): Rules + { + $this->configureValidation(); + return parent::getRules(); + } + + private function configureValidation(): void + { + if ($this->configured) { + return; } + $message = $this->message ?? 'Are you a bot?'; $this->addRule(function ($code) { return $this->verify() === true; }, $message); - $this->configured = true; - - return $this; } public function verify(): bool @@ -58,6 +75,8 @@ public function verify(): bool public function getControl(): Html { + $this->configureValidation(); + $el = parent::getControl(); $el->addAttributes([ 'id' => $this->getHtmlId(),