Laravel 将整数或浮点数验证为字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35519959/
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
Laravel validate int or float numbers as string
提问by
in my project. all mount maybe int or float or was even double.in database amount column type is varchar
and for each choose amount by user i have simple limitation, for example amount must be more than some value and less that some value. but i get error validation in laravel.
在我的项目中。所有挂载可能是 int 或 float 或者甚至是 double.in 数据库金额列类型是varchar
,对于每个用户选择的金额,我有一个简单的限制,例如金额必须大于某个值而小于某个值。但我在 Laravel 中得到了错误验证。
$ps = DB::table('merchant_web_service')->whereCustomerKey($request->input('customer_key'))->first();
/* $ps->minimum_range_buy is 5 and $ps->maximum_range_buy is 10*/
$validate_amount = Validator::make($request->all(),
['amount' => "required|min:$ps->minimum_range_buy|max:$ps->maximum_range_buy"]);
validator error is:
验证器错误是:
"The amount must be at least 10 characters."
my test amount values: 1000,100.1
我的测试金额值: 1000,100.1
采纳答案by Muhammet
Since you didn't specify any rule for the input data type, it validates it as a string. Try numeric
and between
rules.
由于您没有为输入数据类型指定任何规则,因此它会将其验证为字符串。尝试numeric
和between
规则。
$validate_amount = Validator::make($request->all(),
['amount'=>
"required|numeric|between:$ps->minimum_range_buy,$ps->maximum_range_buy"
]);
回答by utsav
try this
尝试这个
$rules = [
'your_field' => 'required|regex:/^\d*(\.\d{2})?$/'
]