Skip to content

Commit

Permalink
Fix validation in case that no message is provided
Browse files Browse the repository at this point in the history
  • Loading branch information
mabar committed Mar 18, 2019
1 parent 1617b07 commit b5d9066
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
37 changes: 28 additions & 9 deletions src/Forms/InvisibleReCaptchaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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(),
Expand Down
37 changes: 28 additions & 9 deletions src/Forms/ReCaptchaField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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
Expand All @@ -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
Expand All @@ -58,6 +75,8 @@ public function verify(): bool

public function getControl(): Html
{
$this->configureValidation();

$el = parent::getControl();
$el->addAttributes([
'id' => $this->getHtmlId(),
Expand Down

0 comments on commit b5d9066

Please sign in to comment.