Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ echo form_close();
```
helper(['form', 'reCaptcha']);

echo form_open();
echo form_open('/form_processing_path', array('id' => 'contactForm'));

echo reCaptcha3('reCaptcha3', ['id' => 'recaptcha_v3'], ['action' => 'contactForm']);

Expand All @@ -71,6 +71,10 @@ public $validationRules = [
];
```

In the settings of the reCaptcha3 validator, the first parameter you specify is expectedAction, this parameter is not required.
In the settings of the reCaptcha3 validator, the first parameter you specify is
expectedAction. The form id attribute needs to share the same name as the action.
This allows grecaptcha.execute to be called on form submission to prevent token
expiration warnings.

You can override a global scoreThreshold parameter in the second reCaptcha3 rule parameter.
You can override a global scoreThreshold parameter in the second reCaptcha3 rule
parameter.
25 changes: 20 additions & 5 deletions Views/init_v3.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,25 @@
<script type="text/javascript">

grecaptcha.ready(function() {
grecaptcha.execute('<?= $key;?>', <?= json_encode($options);?>).then(function(token) {
document.getElementById('<?= $id;?>').value = token;
document.getElementById('<?= $id;?>').oninput();
});
var subCapStart = false;
var subCapInMotion = false;
var subCapForm = document.getElementById('<?= $options['action'] ?>');
subCapForm.addEventListener('submit', formCapSubmit);
function formCapSubmit(event){
if (!subCapStart){
event.preventDefault();
subCapStart = true;
grecaptcha.execute('<?= $key;?>', <?= json_encode($options);?>).then(function(token) {
document.getElementById('<?= $id;?>').value = token;
setTimeout(function(){subCapForm.submit();}, 500);
// document.getElementById('<?= $id;?>').oninput();
});
} else if (subCapInMotion){
// prevents double submission when activating grecaptcha
event.preventDefault();
}
subCapInMotion = true;
}
});

</script>
</script>