我在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个小时里,我一直在努力解决这个问题,但我被卡住了。
非常感谢您的帮助。
感谢
经过大量的谷歌搜索,我终于解决了我的问题。我最终将我的构建形式更改为以下内容:
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
));
}
并将我的配置yml更改为:
validation: { enable_annotations: false }
这解决了我的问题。有点希望在安装说明中能更清楚一点。
哦,好吧。