项目作者: alexeevdv

项目描述 :
Yii2 component, widget and validator for Google ReCaptcha
高级语言: PHP
项目地址: git://github.com/alexeevdv/yii2-recaptcha-widget.git
创建时间: 2015-09-14T18:22:43Z
项目社区:https://github.com/alexeevdv/yii2-recaptcha-widget

开源协议:MIT License

下载


yii2-recaptcha-widget

Build Status
codecov
PHP 5.6
PHP 7.0
PHP 7.1
PHP 7.2
PHP 7.3

Yii2 wrapper for Google reCAPTCHA.

Installation

The preferred way to install this extension is through composer.

Either run

  1. $ php composer.phar require alexeevdv/yii2-recaptcha-widget "^1.0"

or add

  1. "alexeevdv/yii2-recaptcha-widget": "^1.0"

to the require section of your composer.json file.

Configuration

Through application component

  1. 'components' => [
  2. //...
  3. 'recaptcha' => [
  4. 'class' => \alexeevdv\recaptcha\Recaptcha::class,
  5. 'siteKey' => 'YOUR_SITE_KEY',
  6. 'secret' => 'YOUR_SECRET',
  7. ],
  8. //...
  9. ],

Through widget and validator params

  1. use alexeevdv\recaptcha\RecaptchaValidator;
  2. use alexeevdv\recaptcha\RecaptchaWidget;
  3. // Model validation rules
  4. public function rules()
  5. {
  6. return [
  7. //...
  8. [
  9. ['recaptcha'],
  10. RecaptchaValidator::class,
  11. 'secret' => 'YOUR_SECRET',
  12. 'minimalScore' => 0.6,
  13. 'onScoreReceived' => function ($score) {
  14. // Do smth on actual user score. F.e. log it somewhere
  15. },
  16. ],
  17. //...
  18. ];
  19. }
  20. // Widget params
  21. echo RecaptchaWidget::widget([
  22. 'siteKey' => 'YOUR_SITE_KEY',
  23. ]);

Usage

  1. use alexeevdv\recaptcha\RecaptchaValidator;
  2. use alexeevdv\recaptcha\RecaptchaWidget;
  3. // Using ActiveForm
  4. // In this case model validation rules will be applied
  5. // You'll need to specify RecaptchaValidator for attribute
  6. echo $form->field($model, 'recaptcha')->widget(RecaptchaWidget::class);
  7. // As standalone field
  8. echo RecaptchaWidget::widget(['name' => 'recaptcha']);
  9. // In this case you need to check value manually
  10. $validator = new RecaptchaValidator();
  11. $isValid = $validator->validateValue(Yii::$app->request->get('recaptcha'));

Usage in tests

To turn off recaptcha checking you need to add this in your test config:

  1. 'container' => [
  2. 'definitions' => [
  3. \alexeevdv\recaptcha\RecaptchaValidator::class => ['skipOnEmpty' => true],
  4. ],
  5. ],

Additional component and widget params

  1. /**
  2. * Optional. Color theme of the widget. "dark" or "light"
  3. * @var string
  4. */
  5. public $theme;
  6. /**
  7. * Optional. The type of CAPTCHA to serve. "image" or "audio"
  8. * @var string
  9. */
  10. public $type;
  11. /**
  12. * Optional. The size of the widget. "compact" or "normal"
  13. * @var string
  14. */
  15. public $size;
  16. /**
  17. * Optional. The tabindex of the widget and challenge.
  18. * If other elements in your page use tabindex, it should be set to make user navigation easier.
  19. * @var integer
  20. */
  21. public $tabindex;
  22. /**
  23. * Optional. The name of your callback function, executed when the user submits a successful response.
  24. * The g-recaptcha-response token is passed to your callback.
  25. * @var string|JsExpression
  26. */
  27. public $callback;
  28. /**
  29. * Optional. The name of your callback function, executed when the reCAPTCHA response expires
  30. * and the user needs to re-verify.
  31. * @var string|JsExpression
  32. */
  33. public $expiredCallback;
  34. /**
  35. * Optional. The name of your callback function, executed when reCAPTCHA encounters an error
  36. * (usually network connectivity) and cannot continue until connectivity is restored. If you specify
  37. * a function here, you are responsible for informing the user that they should retry.
  38. * @var string|JsExpression
  39. */
  40. public $errorCallback;
  41. /**
  42. * Optional. Forces the widget to render in a specific language
  43. * If not set then language is auto detected from application language
  44. * If set to false then language is autodetected on client side
  45. */
  46. public $language;