Spam protection for standalone forms

Green
Member | 5
+
0
-

Hi,
I`m using nette/form as standalone package in version 3. There is a way/package to protect my forms from spam? (reCaptcha or sth)

dakur
Member | 493
+
0
-

@Green Hi, yes, there is contributte.org website where you can find various Nette-related packages. reCaptcha is here: https://contributte.org/…CAPTCHA.html#…

Last edited by dakur (2021-03-25 12:29)

Green
Member | 5
+
0
-

Thx, for fast reply!

This package, from what i see, is for version 2.4, and this configuration:

extensions:
	recaptcha: Contributte\ReCaptcha\DI\ReCaptchaExtension

recaptcha:
	secretKey: ***
	siteKey: ***

I suppose, is for the full version of the framework? How can I use it for standalone forms? Where should I pass secretKey and siteKey?

dakur
Member | 493
+
+1
-

Aha, I'm sorry, I've overlooked you are using standalone version. When you don't need integration for Nette framework, just use Google's PHP SDK: https://github.com/…le/recaptcha

The package mentioned above is just adapter of this Google's SDK for Nette. — update: ok, it isn't, but that does not change anything.

Last edited by dakur (2021-03-25 12:44)

David Matějka
Moderator | 6445
+
+1
-

try something like this:

$provider = new \Contributte\ReCaptcha\ReCaptchaProvider($siteKey, $siteSecret);
\Contributte\ReCaptcha\Forms\InvisibleReCaptchBinding::bind($provider);
\Contributte\ReCaptcha\Forms\ReCaptchBinding::bind($provider);

// and now you can call
$form->addInvisibleRecaptcha('recaptcha');
// or
$form->addRecaptcha('recaptcha');

you can also add a recaptcha component to the form directly without extension methods:

$provider = new \Contributte\ReCaptcha\ReCaptchaProvider($siteKey, $siteSecret);
$form['recaptcha'] = new Contributte\ReCaptcha\Forms\RecaptchaField();
$form['recaptcha']->setRequried(true);

to avoid installing unnecessary nette/di dependency, you can use “replace” section in composer.json

dakur
Member | 493
+
0
-

…ehm, yeah, that's better option. 😳

Green
Member | 5
+
0
-

Great! I`ll try it :)

Thank you guys for fast help! :)

Green
Member | 5
+
0
-

Sorry I`m back :)

I have small problem displaying the error. In other fields, when I do var_dump like $form['other_field']->errors, I get array of specific field errors, unfortunately in recapatcha case, this array is always empty, but when i want display all errors on top of the form in this way $form->errors then the error from recaptcha is in array.

Do you know why this is happening and how to get a single error from recapthca?