Symfony2 EWZ Recaptcha 字段未正确验证 [英] Symfony2 EWZ Recaptcha field not correctly validating

查看:81
本文介绍了Symfony2 EWZ Recaptcha 字段未正确验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在验证 Symfony2 中的表单中的 EWZ recaptcha 字段时遇到问题.

I am having an issue with Validating an EWZ recaptcha field in a form that I have in Symfony2.

这是我的验证码实体:

<?php

namespace Acme\FormBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints as Recaptcha;

/**
 * Captcha
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class Captcha
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var boolean
     *
     * @ORM\Column(name="captcha", type="boolean")
     */
    private $captcha;

    /**
     * @var integer
     *
     * @ORM\Column(name="uniqueid", type="integer")
     */
    private $uniqueid;

    /**
     * @Recaptcha\True
     */
    public $recaptcha;


    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set captcha
     *
     * @param boolean $captcha
     * @return Captcha
     */
    public function setCaptcha($captcha)
    {
        $this->captcha = $captcha;

        return $this;
    }

    /**
     * Get captcha
     *
     * @return boolean 
     */
    public function getCaptcha()
    {
        return $this->captcha;
    }

    /**
     * Set uniqueid
     *
     * @param integer $uniqueid
     * @return Captcha
     */
    public function setUniqueid($uniqueid)
    {
        $this->uniqueid = $uniqueid;

        return $this;
    }

    /**
     * Get uniqueid
     *
     * @return integer 
     */
    public function getUniqueid()
    {
        return $this->uniqueid;
    }
}

这是我的表格:

<?php

namespace Acme\FormBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\True;

class CaptchaType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('recaptcha', 'ewz_recaptcha')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Acme\FormBundle\Entity\Captcha'
        ));
    }

    public function getName()
    {
        return 'captchaType';
    }
}

这是我的控制器:

<?php

namespace Acme\FormBundle\Controller;

use Acme\FormBundle\Entity\User;
use Acme\FormBundle\Entity\Workstation;
use Acme\FormBundle\Entity\Captcha;
use Acme\FormBundle\Form\CaptchaType;
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\True;


use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Form\FormBuilderInterface;

class DefaultController extends Controller
{
    public function indexAction(Request $request)
    {
        $captcha = new Captcha();
        $form = $this->createForm(new CaptchaType(), $captcha);
        if ($request->isMethod('POST')) {
            echo "1";

            $form->bind($request);

            echo "2";


            if ($form->isValid()) {
                echo "3A";
                // perform some action, such as saving the task to the database
                $session = $this->getRequest()->getSession();
                $temptime = strtotime("now");
                $session->set('uniqueid', $temptime);

                return $this->redirect($this->generateUrl('customer_info'));
            }else{
                return $this->redirect($this->generateUrl('captcha'));
            }
            echo "3B";
        }



        return $this->render('AcmeFormBundle:Default:index.html.twig', array('form' => $form->createView()));



    }

当我检查表单是否有效时,它永远不等于 true.我确定我遗漏了一些很容易的东西,但在过去的 4 个小时里我一直在努力解决这个问题,但我被卡住了.

When I check to see if the form is valid, it never equals true. I'm sure that I'm missing something pretty easy, but I have been trying to figure this out for the past 4 hours, and I'm stuck.

非常感谢任何帮助.

谢谢

推荐答案

经过大量的谷歌搜索,我最终解决了我的问题.我最终将我的 buildform 更改为以下内容:

After a lot Googling, I ended up fixing my problem. I ended up changing my buildform to the following:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('recaptcha', 'ewz_recaptcha', array(
            'attr'          => array(
                'options' => array(
                    'theme' => 'clean'
                )
            ),
            'mapped' => false,
            'constraints'   => array(
                new True()
            ),
            'error_bubbling' => true
        ));

    }

并将我的 config.yml 更改为:

And changed my config.yml to this:

validation:      { enable_annotations: false }

那解决了我的问题.希望在安装说明中能更清楚一点.

That fixed my issue. Kind of wish this was a little more clear in the installation instructions.

哦,好吧.

这篇关于Symfony2 EWZ Recaptcha 字段未正确验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆