Validate a price

Take your time, follow the code and check your understanding.

The codeLaravel routing and validation
 $data = $request->validate([
    "title" => ["required", "string", "max:120"],
    "price" => ["required", "numeric", "gt:0"],
 ]);
 // JSON request with price: -1
Your answer4 min · no timer
What needs fixing?

Show solution and explanation

The gt:0 rule requires a positive value. JSON clients should send Accept: application/json to receive JSON validation responses.

Validation rejects the negative price.