Skip to content

Commit 84b5b6c

Browse files
Bahaa OdehBahaa Odeh
authored andcommitted
setup recaptch v3
1 parent 9717587 commit 84b5b6c

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

README.md

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,36 @@ Usage
2727

2828
Once the extension is installed, simply use it in your code by :
2929

30+
add this to your components main.php
31+
32+
```php
33+
'components' => [
34+
...
35+
'recaptchaV3' => [
36+
'class' => 'Baha2Odeh\RecaptchaV3\RecaptchaV3',
37+
'site_key' => '###',
38+
'secret_key' => '###',
39+
],
40+
41+
```
42+
43+
and in your model
44+
45+
acceptance_score the minimum score for this request (0.0 - 1.0) or null
46+
3047
```php
31-
<?= \Baha2Odeh\RecaptchaV3\AutoloadExample::widget(); ?>```
48+
public $code;
49+
50+
public function rules(){
51+
return [
52+
...
53+
[['code'],RecaptchaV3Validator::className(),'acceptance_score'=>null]
54+
];
55+
}
56+
```
57+
58+
```php
59+
<?= $form->field($model,'code')->widget(\Baha2Odeh\RecaptchaV3\RecaptchaV3Widget::className());
60+
```
61+
62+

RecaptchaV3Validator.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RecaptchaV3Validator extends Validator
3030

3131

3232
/**
33-
* the score for this request (0.0 - 1.0)
33+
* the minimum score for this request (0.0 - 1.0)
3434
* @var null|int
3535
*/
3636
public $acceptance_score = null;
@@ -62,7 +62,6 @@ public function init()
6262
*/
6363
protected function validateValue($value)
6464
{
65-
6665
$result = $this->_component->validateValue($value);
6766
if($result === false){
6867
return [$this->message, []];

RecaptchaV3Widget.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public function run()
7272

7373

7474
$this->view->registerJs(<<<JS
75+
76+
grecaptcha.ready(function() {
77+
grecaptcha.execute('{$this->_component->site_key}', {action: '{$this->actionName}'}).then(function(token) {
78+
$('#{$inputId}').val(token);
79+
});
80+
});
7581
$('#{$formId}').on('beforeSubmit',function(){
7682
if(!$('#{$inputId}').val()){
7783
grecaptcha.ready(function() {
@@ -80,9 +86,9 @@ public function run()
8086
$('#{$formId}').submit();
8187
});
8288
});
83-
return true;
84-
}else{
8589
return false;
90+
}else{
91+
return true;
8692
}
8793
});
8894
JS

0 commit comments

Comments
 (0)