Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 验证来自表单的整数输入_Validation_Symfony - Fatal编程技术网

Validation 验证来自表单的整数输入

Validation 验证来自表单的整数输入,validation,symfony,Validation,Symfony,我有一个实体,其中有几个字段。其中一个在提交表格后进行验证,如下所示: /** * @var integer $anzahl * * @ORM\Column(name="anzahl", type="integer") * @Assert\NotBlank(message="Bitte geben Sie eine Kistenanzahl an.") * @Assert\Type(type="numeric", message="Die Kistenanzahl muss eine

我有一个实体,其中有几个字段。其中一个在提交表格后进行验证,如下所示:

/**
 * @var integer $anzahl
 *
 * @ORM\Column(name="anzahl", type="integer")
 * @Assert\NotBlank(message="Bitte geben Sie eine Kistenanzahl an.")
 * @Assert\Type(type="numeric", message="Die Kistenanzahl muss eine Zahl sein.")
 * @Assert\Min(limit="1", message="Sie müssen mindestens eine Kiste suchen oder anbieten.")
 */
private $anzahl;
我对此解决方案有两个问题:

只接受大于零的整数值。然而,该验证也接受浮点数/双精度浮点数。但是,如果我将
@Assert\Type(Type=“numeric”)
更改为
@Assert\Type(Type=“integer”)
则不会将任何输入验证为true。如何验证输入是否为整数值

另一个问题是,在输入一个明显无效的值(如一串字母)后,我不仅收到了用于类型验证的德语错误消息,还收到了英语消息“此值应为有效数字”。此消息来自何处?我如何摆脱它?

您可以使用

/**
 * @Assert\Regex(pattern="/\d+/")
 */
或者使用
ctype\u digit
创建一个验证器,您应该使用:

@Assert\Type(type="integer")
但是要小心,您应该将其与
整数类型
一起使用,而不是
数字类型
文本类型

Symfony\Component\Form\Extension\Core\Type\IntegerType 

IntegerType
NumberType
相同,只是它集成了正确的字段类型。

如果字段类型应为字符串,则可以使用此字段:

/**
 * @Assert\Type(type="digit")
 */
虽然没有提到,
TypeValidator
也使用
ctype.*
函数

见:

这对我很有用:

 ->add('field_name', 'integer', array(
     'label' => 'Your label here', 
     'data' => 0, // default value
     'precision' => 0, // disallow floats
     'constraints' => array(
         new Assert\NotBlank(), 
         new Assert\Type('integer'), 
         new Assert\Regex(array(
             'pattern' => '/^[0-9]\d*$/',
             'message' => 'Please use only positive numbers.'
             )
         ),
         new Assert\Length(array('max' => 2))
     )
 ))

我不得不在表单字段类型中使用
number
,但它在输入标签旁边显示了一个星号
*
,尽管它不是强制性的。因此,我也不得不使用
“required”=>false
。表单字段类型
integer
不起作用。基础字段数据类型为
smallint

->add('storey', 'number', array('required' => false))
YML中的
Regex
约束在类型为
integer
或未提供任何内容时也不起作用。我不知道为什么

storey:
    - Regex: '/^[0-9]+$/'

我的Symfony版本是2.7。

从Symfony v2.3版本开始,您应该使用:

/**
 * @Assert\Type(type="integer")
 * @Assert\GreaterThan(0)
 */

但是请记住表单字段类型应该是整数(
IntegerType::class
),否则我将得到否定的验证。

@Assert\type(type=“integer”)
不起作用。这是我尝试的第一件事。你看到答案的第二部分了吗?此属性使用的表单类型是什么?Assert\Min()已被弃用。此答案已在2年前发布。由于此解决方案即使使用
text
作为字段类型也接受数字,因此它的缺点是接受“123ABC”等值。也许这个模式更有用,因为它也不接受以前导零开头的值:
*@Assert\Regex(pattern=“/^([1-9][0-9]*)$/”
较短的版本不以0开头,那么只接受数字
@Assert\Regex(pattern=“/^ 0)\d+$/”
太好了,谢谢!我已经创建了一个pull请求来将它添加到文档中。在我的例子中,regex工作得很好,但是类型函数integer不工作。这对我来说很奇怪,因为我已经检查了我已经在代码中添加了use-Type的所有代码。这让我感到奇怪,因为我已经检查了我已经在代码中添加的所有代码。
/**
 * @Assert\Type(type="integer")
 * @Assert\GreaterThan(0)
 */