Php 拉威尔验证浮点数
我正在尝试验证我的laravel应用程序中的浮动。 我正在使用laravel验证,我尝试的任何东西都是不正确的。 我试过类似的东西Php 拉威尔验证浮点数,php,laravel,Php,Laravel,我正在尝试验证我的laravel应用程序中的浮动。 我正在使用laravel验证,我尝试的任何东西都是不正确的。 我试过类似的东西 $validator = Validator::make($request->all(), [ 'packages.*.real_price' => 'required|regex:/^\d*(\.\d{2})?$/' ] 我尝试了数字、整数,基本上我在互联网上能找到的所有东西,但每次我都会收到如下错误: TypeE
$validator = Validator::make($request->all(), [
'packages.*.real_price' => 'required|regex:/^\d*(\.\d{2})?$/'
]
我尝试了数字、整数,基本上我在互联网上能找到的所有东西,但每次我都会收到如下错误:
TypeError: Argument 6 passed to App\Http\Controllers\ProductController::createPackageInfo() must be of the type float, string given
正如你们看到的,我给了字符串来测试它。有什么建议吗?
我发出的请求就像
{
"sku":"asd",
"slug":"admin@admin.com",
"category":"2",
"type":"accessory",
"product_info":[
{
"country":184,
"name":"asd",
"slug":"asd",
"description":"asd"
}
],
"packages":[
{
"country":98,
"name":"asd",
"slug":"asd",
"qty":"12",
"real_price":"123",
"sale_price":"asd",
"delivery_price":"123"
}
]
}
我怎么称呼这个方法
try{
$createdPackageInfo = self::createPackageInfo($createdPackage, $country, $name, $slug, $real_price, $sale_price, $delivery_price, $image);
}catch (\Exception $e) {
Log::log('Something bad happened', 'POST', $e);
return response()->json(['response' => false, 'message' => 'Something went wrong with creating package info'], 400);
}
以及我正在调用的函数
private static function createPackageInfo(int $package_id, int $country_id, string $name, string $slug, float $real_price, float $sale_price, float $delivery_price, string $image)
{
$packageInfo = PackageInfo::create(['package_id' => $package_id, 'country_id' => $country_id, 'name' => $name, 'slug' => $slug, 'real_price' => $real_price, 'sale_price' => $sale_price, 'delivery_price' => $delivery_price, 'image' => $image]);
return $packageInfo->id;
}
错误很明显,当您调用函数
createPackageInfo
时,您传递(我猜)显示的字符串的结果
我猜您在createPackageInfo()函数中键入了参数,在JSON中,我们可以得到:
"real_price":"123",
"sale_price":"asd",
双引号表示值是字符串类型的
您可以简单地使用:
$createdPackageInfo = self::createPackageInfo($createdPackage, $country, $name, $slug, (float) $real_price, (float) $sale_price, (float) $delivery_price, $image);
如果字符串是数字的,那么自定义验证如何?每次将其传递给函数时,都对其进行类型转换
$validator = Validator::make($request->all(), [
'packages.*.real_price' => 'required|numeric'
]
当你调用这个方法时,键入它
$typecasted_real_price = $real_price+ 0.0;
$createdPackageInfo = self::createPackageInfo($createdPackage, $country, $name, $slug, $typecasted_real_price, $sale_price, $delivery_price, $image);
// or
$createdPackageInfo = self::createPackageInfo($createdPackage, $country, $name, $slug, ($real_price+ 0.0), $sale_price, $delivery_price, $image);
添加0.0背后的逻辑
调用的createPackageInfo()
方法是什么?它是我存储数据的一个函数。你能显示你调用该方法的代码吗?是的,我已经更新了问题“销售价格”:“asd”
,这不会产生好的浮动。我不认为这是最好的方法。首先,它不会给出类型不正确的错误。唯一的好处是,如果它不是一个float,它将返回0到数据库。虽然我个人不知道(从未尝试过)numeric验证1-
或+11+
或0xf
,您可以测试它,如果numeric验证在内部使用php的is\u numeric()函数,那么它将传递这些字符串,但如果它使用ctype\u digit()
php函数,则不会传递上述值
$num = "1000";
// Performing mathematical operation to implicitly type conversion
echo $num + 0.0, "\n"; // value = 1000
echo gettype($num + 0.0), "\n"; // type = double