使用 Assert 注释的 Symfony2 验证不起作用 [英] Symfony2 validation using Assert annotation does not work

查看:26
本文介绍了使用 Assert 注释的 Symfony2 验证不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:因为我没有得到任何答案,所以我用一个更简单的例子重写了整篇文章.希望这有助于暴露问题.

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.

我在表单验证方面遇到问题.我可以让 NotBlank() 断言起作用,但 Type() 对我不起作用.首先,这是代码:

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(),
        ));
    }
}

当我将输入留空时,我收到一条错误消息,正如预期的那样.但是当我在输入中输入adsf"时,我看到输出Form is valid".我已经使用 YAML 和 PHP 验证尝试过同样的事情.谁能看到我做错了什么?

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?

谢谢,- 伊恩

推荐答案

这不能按预期工作的原因是 Symfony 的 NumberFormatter 存根实现中的一个错误.如果您没有安装 PHP intl 扩展,将使用存根实现.

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.

基本上,数字格式化程序会尝试解析值和 当发现它以非数字字符开头时返回false.它应该设置一个错误代码,然后在转换器中触发异常,但是,因为它没有,所以使用布尔假值和 将类型转换为整数(这本身也是一个错误).因此,您的adsf"输入最终为整数 0 并通过类型断言.

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.

您可以使用这些补丁来解决问题,或者您现在可以通过添加一个Min断言,限制设置为1.

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.

这篇关于使用 Assert 注释的 Symfony2 验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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