且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 Assert 注释的 Symfony2 验证不起作用

更新时间:2023-11-19 20:50:10

这不能按预期工作的原因是 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.