Laravel 价格验证只接受正数,而不仅仅是 0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49212265/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:30:02  来源:igfitidea点击:

Laravel price validation only accept positive number and not only 0

laravelvalidation

提问by user2722667

I want to validate a "price" field in Laravel.

我想验证 Laravel 中的“价格”字段。

The price should only contain numbers without (.) or (,) and cant start with 0 as well.

价格应该只包含没有 (.) 或 (,) 的数字,并且也不能以 0 开头。

Eg

例如

2500 // Pass

2500 // 通过

02500 // Fails

02500 // 失败

12.12 // Fails

12.12 // 失败

12,12 / Fails

12,12 / 失败

The rule I have now looks like this:

我现在的规则是这样的:

'price' => 'required|integer|not_in:0',

It seems to work with the example above, however I dont understand it. Why does not integerallow something like 0123with a starting zero. I just want to make sure that my rule works as excpected and that I dont miss something

它似乎适用于上面的示例,但是我不明白。为什么integer不允许带有起始零的0123 之类的东西。我只是想确保我的规则按预期工作并且我不会错过任何东西

Thanks

谢谢

采纳答案by kevinniel

if you're not sure about the rule "integer", you can use the regex expression validation as following :

如果您不确定规则“整数”,您可以使用正则表达式验证如下:

'price' => 'required|regex:^[1-9][0-9]+|not_in:0',

I had some issue same way as you, and since then i always used the regex validation for this kind of requirements. Furthermore it allow you to take back the same regex if you want to make a front validation with js.

我遇到了和你一样的问题,从那以后我总是使用正则表达式验证来满足这种要求。此外,如果您想使用 js 进行前端验证,它允许您收回相同的正则表达式。

Here is the laravel function allowing to validate integers :

这是允许验证整数的 laravel 函数:

public function validateInteger($attribute, $value)
{
    return filter_var($value, FILTER_VALIDATE_INT) !== false;
}

SO it's PHP itself that is concerned by this behavior. PHP tells us this thing about FILTER_VALIDATE_INT :

因此,这种行为是由 PHP 本身引起的。PHP 告诉我们关于 FILTER_VALIDATE_INT 的事情:

Validates value as integer, optionally from the specified range, and converts to int on success.

将 value 验证为 integer,可选地来自指定的范围,并在成功时转换为 int。

No other informations are set by PHP about the "0" value, but it's known that this function doesn't consider numbers starting by "0".

PHP 没有设置有关“0”值的其他信息,但众所周知,此函数不考虑以“0”开头的数字。

回答by Adnan Mumtaz

You can format the input before sending it to the validator in your Requestclass.

您可以在将输入发送到Request类中的验证器之前格式化输入。

   public function formatInput()
{
  $input = array_map('trim', $this->all());

  $input['price'] = (int)($input['price']);
  $this->replace($input);
  return $this->all();
}

Then you can use

然后你可以使用

'price' => 'required|integer|min:0',

Hope this helps

希望这可以帮助

回答by Mahdi Younesi

Use regexrule to not allow for integer with starting 0

使用正则表达式规则不允许以 0 开头的整数

'price' => 'required|integer|not_in:0|regex:^[1-9][0-9]+',