The problem with the following code is that it saves the data to the users table before the condition if($usersTable->save($user) && $responseData->success ) is met and therefore prints out ‘Either the passwords don't match or the email address is already in use’ but when I put the condition in front of all the $this->request->getData() code, null values are saved to the database. I see that $user is saving status and verified fields to the database but when I comment them out, all of the data still gets saved to the database.
$usersTable = $this->fetchTable('Users');
$user = $usersTable->newEmptyEntity();
$fullname = $this->request->getData('contributor');
$email = $this->request->getData('email');
$password = $this->request->getData('password');
$password_confirm = $this->request->getData('password_confirm');
$token = Security::hash(Security::randomBytes(32));
$user->status = '0';
$user->verified = '0';
//responseData is for server side recaptcha verification
if($usersTable->save($user) && $responseData->success ) {
//here is some code that sends an email
$this->Flash->success(__('A verification link has been sent to your email account.'));
return $this->redirect(['controller' => 'Users', 'action' => 'login']);
} else {
$this->Flash->error(__('Either the passwords don\'t match or the email address is already in use'));
}
as you can see the ‘g-recaptcha-response’ at the end of the data call is empty.
data-callback callback Optional. The name of your callback function, executed when the user submits a successful response. The g-recaptcha-response token is passed to your callback.
My data-callback is enableBtn as you can see here:
<script>
function enableBtn(){
document.getElementById("button1").disabled = false;
}
</script>
It should however work like the following:
Explanation: This code snippet indicates that when a user successfully completes the reCAPTCHA, the "myVerificationCallback" function will be executed, receiving the "g-recaptcha-response" token as input
No example myVerificationCallback script is shown and I don’t know how to receive the the “g-recaptcha-response” token as input
When I add alert(response) to the enableBtn(response) function, the token appears in the pop-up but when I use debug($this->request) in my controller, the protected data still returns an empty variable 'g-recaptcha-response' => ''.
If I change the name of the g-recaptcha-response hidden input to something other than response, it doesn’t work. But maybe I will have browser incompatibility issues if I don’t fix it.
var response = document.getElementById("g-recaptcha-response").value; gets the value from the input. What you want to do is set the value into the input, with the setAttribute('value' call that Kevin shared, i.e.