Laravel validate float

Viewed 2103

I'm trying to validate float in my laravel app. I'm using laravel validation and anything I try is not correct. I tried something like

$validator = Validator::make($request->all(), [
            'packages.*.real_price' => 'required|regex:/^\d*(\.\d{2})?$/'
]
     

I tried with numeric, integer, and basically everything that I can find on the internet, but every time I receive error like:

TypeError: Argument 6 passed to App\Http\Controllers\ProductController::createPackageInfo() must be of the type float, string given

As you can see I'm giving string to test it. Any suggestions? I'm sending request like

{
   "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"
      }
   ]
}

How am I calling this method?

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);
        }

and function that I'm calling

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;
    }
2 Answers

The error is pretty clear, when you call your function createPackageInfo, you pass (I guess) the result of the string you showed.

I guess you typed your params in createPackageInfo() function, and in your JSON, we can you have :

 "real_price":"123",
 "sale_price":"asd", 

Double quotes mean the values are STRING typed.

You can simply use :

$createdPackageInfo = self::createPackageInfo($createdPackage, $country, $name, $slug, (float) $real_price, (float) $sale_price, (float) $delivery_price, $image);

How about custom validating if the string is numeric, then do a type conversion of it everytime you pass it to the function

$validator = Validator::make($request->all(), [
            'packages.*.real_price' => 'required|numeric'
]

And when you are calling this method, typecast it,

$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);

Logic behind adding 0.0

$num = "1000"; 
  
// Performing mathematical operation to implicitly type conversion 
echo $num + 0.0, "\n";  // value = 1000
echo gettype($num + 0.0), "\n";  // type = double
Related