1
我遇到了在Symfony2中的表單中驗證EWZ recaptcha字段的問題。Symfony2 EWZ Recaptcha字段未正確驗證
這裏是我的驗證碼實體:
<?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()));
}
當我檢查,看看是否形式有效,它永遠不會等於真。我敢肯定,我想的東西很容易,但我一直在試圖弄清楚這一點在過去的4個小時,和我被困。
任何幫助,非常感謝。
感謝