我已经使用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(); ?>
使用以下链接创建了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代码移到了表单之外。一切正常,但验证码,但坐在一些随机的位置,如下:
如何解决此问题?
答案 0 :(得分:2)
如果您使用的是CakePHP安全组件,则此消息可以显示,并且此组件无法识别您的某个表单字段。您应该使用以下方法解锁此字段:
$this->Form->unlockField('g-recaptcha-response');