我正在使用szmnmichalowski/ZF2-NoCaptcha进行验证码验证,我有以下设置.如何使用相同的验证器验证’captcha’表单元素,并生成并嵌入“g-recaptcha-response”以便即时生成?我得到’两个给定的令牌不匹配’错误消息,即使我键入正确的验证码.但我可以使用自定义验证器验证它.
composer.json
{
"require": {
"PHP": ">=5.3.3",
"zendframework/zendframework": "2.3.3",
"spoonx/sxmail": "1.4.*",
"slm/queue": "0.4.*",
"szmnmichalowski/zf2-nocaptcha": "dev-master"
}
}
return array(
'modules' => array(
'SxMail',
'SlmQueue',
'Application',
'NoCaptcha'
)
);
模块/应用/ Module.PHP
<?PHP
namespace Application;
class Module implements AutoloaderProviderInterface, ConfigProviderInterface, BootstrapListenerInterface
{
public function getFormElementConfig()
{
return array(
'factories' => array(
'Application\Form\ContactusForm' => function ($serviceManager) {
$form = new \Application\Form\ContactusForm();
return $form;
}
)
);
}
}
模块/应用/ src目录/应用/表格/ ContactusForm.PHP
<?PHP
namespace Application\Form;
use Zend\Form\Element;
use Zend\Form\Form;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\InputFilter\InputFilterawareInterface;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\Factory as InputFactory;
class ContactusForm extends Form implements InputFilterawareInterface, ServiceLocatorAwareInterface
{
protected $inputFilter;
public function __construct($name = null)
{
parent::__construct('ContactUs');
}
public function init()
{
$name = new \Zend\Form\Element\Text('name');
$name->setAttributes(array(
'class' => 'form-control',
'id' => 'name',
'placeholder' => 'Name',
'required' => 'Name required'
)
);
$this->add($name);
$config = $this->getServiceLocator()->getServiceLocator()->get('config');
$options = $config['application']['captcha'];
$captcha = new \NoCaptcha\Captcha\ReCaptcha($options);
$this->add(array(
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'attributes' => array(
'id' => 'recaptcha-response',
),
'options' => array(
'label' => 'Are you a bot?',
'captcha' => $captcha
)
));
}
public function getInputFilter()
{
if (!$this->inputFilter) {
$inputFilter = new InputFilter();
$factory = new InputFactory();
$inputFilter->add($factory->createInput([
'name' => 'name',
'required' => true,
'filters' => array(
array(
'name' => 'StripTags'
),
array(
'name' => 'StringTrim'
)
),
'validators' => array(
array(
'name' => 'not_empty',
"options" => array(
"messages" => array(
"isEmpty" => "Name is empty."
),
),
),
)
]));
$inputFilter->add($factory->createInput([
'name' => 'captcha',
'required' => true,
'validators' => array(
array(
'name' => 'Identical',
'options' => array(
'token' => 'g-recaptcha-response',
)
)
)
]));
$this->inputFilter = $inputFilter;
}
return $this->inputFilter;
}
}
模块/应用/ SRC /应用/控制器/ IndexController.PHP
<?PHP
namespace Application\Controller;
use Application\Form\ContactusForm;
class IndexController extends ApplicationController
{
public function __construct(
\Application\Service\EmailService $emailService
) {
$this->emailService = $emailService;
}
public function indexAction()
{
$contactUsForm = $this->getServiceLocator()->get('FormElementManager')->get('Application\Form\ContactusForm');
$contactUsForm->setTranslator($this->translator);
$request = $this->getRequest();
if ($request->isPost()) {
$postData = $request->getPost();
$contactUsForm->setData($postData);
if ($contactUsForm->isValid()) {
$emailJob = $this->getJobManager()->get('Application\Job\Email');
$emailJob->setContent(
//content
);
$this->getDoctrineQueue()->push($emailJob);
$successMessage = $this->translator->translate('Message successfully sent.');
$this->flashMessenger()->addSuccessMessage($successMessage);
$this->redirect()->toRoute('home');
} else {
$errorMessage = $this->translator->translate('Message Failed. Please try again');
$this->flashMessenger()->addErrorMessage($errorMessage);
}
}
$viewmodel = new viewmodel();
$viewmodel->setvariable('form', $contactUsForm);
return $viewmodel;
}
}
解决方法:
好像很多工作.试试这个:
https://github.com/Saeven/zf2-circlical-recaptcha
披露:我是作者,但至少可以保证它有效.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。