使用 Assert 注释的 Symfony2 验证不起作用
更新:由于我没有得到任何答案,我使用一个更简单的示例重写了整个帖子。希望这有助于揭露问题。
我在表单验证方面遇到问题。我可以让 NotBlank() 断言起作用,但 Type() 对我不起作用。首先,这是代码:
/* ...\Entity\LineItem.php */
<?php
namespace Rialto\ExperimentBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class LineItem
{
/**
* @var integer
* @Assert\NotBlank()
* @Assert\Type(type="integer")
*/
private $quantity = 0;
public function getQuantity()
{
return $this->quantity;
}
public function setQuantity($quantity)
{
$this->quantity = $quantity;
}
}
/* ...\Controller\DefaultController.php */
<?php
namespace Rialto\ExperimentBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Rialto\ExperimentBundle\Entity\LineItem;
class DefaultController extends Controller
{
public function indexAction()
{
return $this->testValidation();
}
private function testValidation()
{
$item = new LineItem();
$form = $this->createFormBuilder($item)
->add('quantity', 'integer')
->getForm();
$request = $this->getRequest();
if ( $request->getMethod() == 'POST') {
$form->bindRequest($request);
if ( $form->isValid() ) {
return new Response('Form is valid.');
}
}
return $this->render('RialtoCoreBundle:Form:basicForm.html.twig', array(
'form' => $form->createView(),
));
}
}
当我将输入留空时,我会收到一条错误消息,如预期的那样。但是当我在输入中输入“adsf”时,我看到输出“表单有效”。我已经使用 YAML 和 PHP 验证尝试了同样的操作。谁能看到我做错了什么吗?
谢谢, - 伊恩
Update: since I'm not getting any answers, I've rewritten the entire post using a much simpler example. Hopefully this helps expose the problem.
I'm having trouble with form validation. I can get the NotBlank() assertion to work, but Type() does not work for me. First, here's the code:
/* ...\Entity\LineItem.php */
<?php
namespace Rialto\ExperimentBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
class LineItem
{
/**
* @var integer
* @Assert\NotBlank()
* @Assert\Type(type="integer")
*/
private $quantity = 0;
public function getQuantity()
{
return $this->quantity;
}
public function setQuantity($quantity)
{
$this->quantity = $quantity;
}
}
/* ...\Controller\DefaultController.php */
<?php
namespace Rialto\ExperimentBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Rialto\ExperimentBundle\Entity\LineItem;
class DefaultController extends Controller
{
public function indexAction()
{
return $this->testValidation();
}
private function testValidation()
{
$item = new LineItem();
$form = $this->createFormBuilder($item)
->add('quantity', 'integer')
->getForm();
$request = $this->getRequest();
if ( $request->getMethod() == 'POST') {
$form->bindRequest($request);
if ( $form->isValid() ) {
return new Response('Form is valid.');
}
}
return $this->render('RialtoCoreBundle:Form:basicForm.html.twig', array(
'form' => $form->createView(),
));
}
}
When I leave the input blank, I get an error message, as expected. But when I type "adsf" into the input, I see the output "Form is valid". I've tried the same thing using YAML and PHP validation. Can anyone see what I've done wrong?
Thanks,
- Ian
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
之所以不能按预期工作,是因为 Symfony 的 NumberFormatter 存根实现中存在错误。如果您没有安装 PHP intl 扩展,则将使用存根实现。
基本上,数字格式化程序尝试解析该值并返回当发现以非数字字符开头时为 false。它应该设置一个错误代码,然后在变压器中触发异常,但由于它没有,所以使用布尔 false 值并且 将类型转换为整数(这本身也是一个错误)。因此,您的“adsf”输入最终为整数 0 并传递类型断言。
我发现有关此问题的问题报告,并且我已针对这两个错误发送了拉取请求。
您可以使用这些补丁来解决该问题,也可以通过添加 Min 断言 将限制设置为 1。
The reason this isn't working as expected is because of a bug in Symfony's stub implementation of NumberFormatter. The stub implementation will be used if you do not have the PHP intl extension installed.
Basically, the number formatter tries to parse the value and returns false when it finds that it starts with a non-numeric character. It should set an error code which would then trigger an exception in the transformer but, since it doesn't, the boolean false value is used and gets typecast to an integer (which is itself a bug, too). So, your "adsf" input ends up as an integer 0 and passes the type assertion.
I found an issue report about this and I've sent pull requests against it for the two bugs.
You can use those patches to fix the issue or you could work around it for now by adding a Min assertion with the limit set to 1.
也许你应该改变这个:
为此:
这只是一个猜测..但我希望它有帮助!
May be you should change this:
For this:
It's just a guess..But I hope it helps!