Laravel 表单输入整数长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28586568/
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 10:58:28  来源:igfitidea点击:

Laravel form input integer length

phpformsvalidationlaravel

提问by Alex McCabe

In Laravel form input validation, you can specify a minimum and a maximum length of a given input field.

在 Laravel 表单输入验证中,您可以指定给定输入字段的最小和最大长度。

$inputRules = [
    'postCode' => 'min:3|max:8'
];

Validation outcome

验证结果

  • 'BA45TZ' = true
  • 'FF' = false
  • 'GH22 55XYZ' = false
  • 'BA45TZ' = true
  • 'FF' = false
  • 'GH22 55XYZ' = false

However if I do the same for a number, it will validate whether the input is less than or greater than the input, then return on that.

但是,如果我对一个数字做同样的事情,它会验证输入是小于还是大于输入,然后返回。

$inputRules = [
    'cv2' => 'min:3|max:4'
];

Validation outcome

验证结果

  • '586' = false
  • '4' = true
  • '2' = false
  • '3' = true
  • '586' = false
  • '4' = true
  • '2' = false
  • '3' = true

I actually want to validate a numbers lengthnot it's numerical value. I can't figure out how to do this. Google has been no help, or I am not searching the right thing.

我实际上想验证数字长度而不是数值。我无法弄清楚如何做到这一点。谷歌一直没有帮助,或者我没有搜索正确的东西。

Anybody had any experience with this?

有没有人有这方面的经验?

EDIT: I have answered my own question. I had missed Laravels digits_between.

编辑:我已经回答了我自己的问题。我错过了 Laravel digits_between

回答by Alex McCabe

Like an idiot, I missed Laravels digits_betweenvalidator rule. I swear I had scoured those rules, but here we are.

像个白痴一样,我错过了 Laraveldigits_between验证器规则。我发誓我已经仔细研究了这些规则,但我们到了。

Thank you everyone for your help.

感谢大家的帮助。

$inputRules = [
    'cv2' => 'required|numeric|digits_between:3,4'
];

回答by lukasgeiter

That's the expected behavior of Laravel's size, minand maxvalidations. You can't change it.

这是 Laravel 的size,minmax验证的预期行为。你不能改变它。

For string data, value corresponds to the number of characters. For numeric data, value corresponds to a given integer value. For files, size corresponds to the file size in kilobytes.

对于字符串数据,值对应于字符数。对于数值数据,值对应于给定的整数值。对于文件,大小对应于以千字节为单位的文件大小。

To solve this you have to create a custom validation rule. Something like this:

要解决此问题,您必须创建自定义验证规则。像这样的东西:

Validator::extend('min_length', function($attribute, $value, $parameters){
    return strlen($value) >= $parameters[0];
});

Validator::extend('max_length', function($attribute, $value, $parameters){
    return strlen($value) <= $parameters[0];
});

And you can use them in the same way:

您可以以相同的方式使用它们:

'cv2' => 'min_length:3|max_length:4'

回答by Santiago Mendoza Ramirez

For length in number you've to use size, but you can't put ranges in it. So, the best thing to do is making a custom validation. Here is the documentation in laravel: http://laravel.com/docs/5.0/validation#custom-validation-rules

对于数量的长度,您必须使用大小,但不能在其中放置范围。因此,最好的做法是进行自定义验证。这是 laravel 中的文档:http://laravel.com/docs/5.0/validation#custom-validation-rules

And an example:

和一个例子:

Validator::extend('minsize', function($attribute, $value, $parameters)
{
    return  strlen($value) >= $parameters[0];
});

And do the same with maxsize.

对 maxsize 做同样的事情。