我遇到了以Symfony2中的形式验证EWZ重访字段的问题。
这是我的Captcha实体:
<?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个小时内解决这个问题,而且我被卡住了。
非常感谢任何帮助。
由于
答案 0 :(得分:2)
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更改为:
validation: { enable_annotations: false }
这解决了我的问题。希望在安装说明中更清楚一点。
好吧,好吧。