2015-12-03 81 views
4

我正在使用szmnmichalowski/ZF2-NoCaptcha進行驗證碼驗證,我有以下設置。我如何使用相同的驗證器來驗證'驗證碼'表單元素'g-recaptcha-response'生成並嵌入到表單中以供即時生成?即使輸入正確的驗證碼,我也會得到'給定的兩個令牌不匹配'錯誤消息。但我可以使用自定義驗證器進行驗證。ZF2,ZF2-NoCaptcha,FormValidation

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" 
    } 
} 

配置/ application.config.php

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 /應用/ Form/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; 
    } 
} 

回答

2

好像很多工作。試試這個: https://github.com/Saeven/zf2-circlical-recaptcha

披露:我是作者,但至少可以保證它的作品。

+0

謝謝。 ZF2-NoCaptcha也在工作。我正在尋找相同的驗證器。 –

+0

您無需完全相同即可驗證驗證碼。上面提供的captcha類具有可用的驗證碼專用驗證程序。讓我知道你是否有困難的時間來運行它。 – Saeven

0

你可以看看我的自由ZF2模塊mxreCaptcha,那就是:

  1. 更容易使用
  2. 具有更好的單元測試覆蓋率
  3. 在CI/CD
  4. 成熟和版本
  5. 關注SemVer
  6. 廣泛應用於我公司的商業項目