CakePHP 3 上 POST 数据中的意外字段 'g-recaptcha-response'
Unexpected field 'g-recaptcha-response' in POST data on CakePHP 3
我已经使用 CakePHP 3 创建了一个站点。我有一个静态页面,它通过类似这样的方式与我们联系:
里面 contactus.ctp:
<?=$this->Form->create(); ?>
<?=$this->Form->hidden('form_type',['value' => 'contact']) ?>
<?=$this->Form->input('name',[
'label' => false,
'placeholder' => 'Your Full Name',
'required' => true
]); ?>
<?=$this->Form->input('email',[
'label' => false,
'placeholder' => 'Your Email',
'type' => 'email',
'require' => true
]); ?>
<?=$this->Form->textarea('message',[
'placeholder' => 'Your message...'
]) ?>
<?= $this->Recaptcha->display()?>
<button>Submit Query!</button>
<?=$this->Form->end(); ?>
使用以下 link 我创建了 Recaptcha:
https://github.com/agiletechvn/Recaptcha
就在“提交”按钮旁边,我有 Recaptcha。
在 pageController 中,我进行了提交检查:
if($this->request->is(['post']) && $this->request->data('form_type') == 'contact'){
$name = $this->request->data('name');
$email = $this->request->data('email');
$message = $this->request->data('message');
if(!$name){
$this->Flash->set('Please enter a name' . $name,['element' => 'error']);
} elseif (!$email || filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$this->Flash->set('The email you entered is invalid',['element' => 'error']);
} elseif(!$message){
$this->Flash->set('Message cannot be blank',['element' => 'error']);
} else {
if($this->Recaptcha->verify()) {
$emailObj = new Email();
$emailObj
->from(['contactus@mydomain.com' => 'Developer'])
->replyTo([$email => $name])
->to(['contactus@contactus.com'])
->template('pages/contactus')
->viewVars([
'quickAction' => [
'description' => 'Contact form',
'action' => "from: $name"
],
'name' => 'contactus@mydomain.com',
'senderName' => $name,
'email' => $email,
'message' => $message
])
->subject('Contact email from ' . $name)
->send();
$this->Flash->set('Your message has been sent', ['element' => 'success']);
}
$this->Flash->error(__('Please pass Google Recaptcha first'));
}
如果我点击提交按钮,我得到:
Unexpected field 'g-recaptcha-response' in POST data
我将 reCaptcha 代码移到了表单之外。一切正常,但验证码却位于这样的随机位置之外:
如何解决这个问题?
如果您使用的是 CakePHP 安全组件,并且此组件无法识别您的表单域之一,则此消息会显示。您应该使用以下方式解锁此字段:
$this->Form->unlockField('g-recaptcha-response');
我已经使用 CakePHP 3 创建了一个站点。我有一个静态页面,它通过类似这样的方式与我们联系:
里面 contactus.ctp:
<?=$this->Form->create(); ?>
<?=$this->Form->hidden('form_type',['value' => 'contact']) ?>
<?=$this->Form->input('name',[
'label' => false,
'placeholder' => 'Your Full Name',
'required' => true
]); ?>
<?=$this->Form->input('email',[
'label' => false,
'placeholder' => 'Your Email',
'type' => 'email',
'require' => true
]); ?>
<?=$this->Form->textarea('message',[
'placeholder' => 'Your message...'
]) ?>
<?= $this->Recaptcha->display()?>
<button>Submit Query!</button>
<?=$this->Form->end(); ?>
使用以下 link 我创建了 Recaptcha:
https://github.com/agiletechvn/Recaptcha
就在“提交”按钮旁边,我有 Recaptcha。
在 pageController 中,我进行了提交检查:
if($this->request->is(['post']) && $this->request->data('form_type') == 'contact'){
$name = $this->request->data('name');
$email = $this->request->data('email');
$message = $this->request->data('message');
if(!$name){
$this->Flash->set('Please enter a name' . $name,['element' => 'error']);
} elseif (!$email || filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
$this->Flash->set('The email you entered is invalid',['element' => 'error']);
} elseif(!$message){
$this->Flash->set('Message cannot be blank',['element' => 'error']);
} else {
if($this->Recaptcha->verify()) {
$emailObj = new Email();
$emailObj
->from(['contactus@mydomain.com' => 'Developer'])
->replyTo([$email => $name])
->to(['contactus@contactus.com'])
->template('pages/contactus')
->viewVars([
'quickAction' => [
'description' => 'Contact form',
'action' => "from: $name"
],
'name' => 'contactus@mydomain.com',
'senderName' => $name,
'email' => $email,
'message' => $message
])
->subject('Contact email from ' . $name)
->send();
$this->Flash->set('Your message has been sent', ['element' => 'success']);
}
$this->Flash->error(__('Please pass Google Recaptcha first'));
}
如果我点击提交按钮,我得到:
Unexpected field 'g-recaptcha-response' in POST data
我将 reCaptcha 代码移到了表单之外。一切正常,但验证码却位于这样的随机位置之外:
如何解决这个问题?
如果您使用的是 CakePHP 安全组件,并且此组件无法识别您的表单域之一,则此消息会显示。您应该使用以下方式解锁此字段:
$this->Form->unlockField('g-recaptcha-response');