【问题标题】:ZF2, ZF2-NoCaptcha, FormValidationZF2、ZF2-NoCaptcha、FormValidation
【发布时间】:2015-12-03 11:43:56
【问题描述】:

我正在使用szmnmichalowski/ZF2-NoCaptcha 进行验证码验证,并且我有以下设置。如何使用 Identical 验证器来验证 '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"
    }
}

config/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;
                }

            )
        );
    }
}

module/Application/src/Application/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;
    }
}

module/Application/src/Application/Controller/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;
    }
}

【问题讨论】:

    标签: php validation zend-framework2


    【解决方案1】:

    看起来工作量很大。试试这个: https://github.com/Saeven/zf2-circlical-recaptcha

    披露:我是作者,但至少可以保证它有效。

    【讨论】:

    • 谢谢。 ZF2-NoCaptcha 也可以工作。我正在寻找相同的验证器。
    • 验证验证码不需要完全相同。上面提供的验证码类有一个特定于验证码的验证器可用。如果您在运行它时遇到困难,请告诉我。
    【解决方案2】:

    大家可以看看我的免费ZF2模块mxreCaptcha,即:

    1. 更容易使用
    2. 具有更好的单元测试覆盖率
    3. 在 CI/CD 下
    4. 成熟和版本化
    5. 关注SemVer
    6. 在我公司的商业项目中广泛使用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-12
      • 1970-01-01
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多