Reputation: 737
I'm trying to validate a json I'm getting via post, using the Dingo API library in Laravel. It seems that the validation is working incorrectly as I send a valid JSON according to the fields I am validating and it returns me the message saying:
The X field is required.
But I'm sending the X field in json, which I do not understand.
JSON:
[
{
"currency_id": 1,
"bills": [
{
"barcode": "99999.9999999.99999999.9999 9",
"due_date": "2018-09-14",
"value": 70.00
},
{
"barcode": "8888.888888.88888.8888 8",
"due_date": "2018-09-15",
"value": 32.00
}
]
}
]
I'm getting this error:
"message": "422 Unprocessable Entity",
"errors": {
"currency_id": [
"The currency id field is required."
],
"bills": [
"The bills field is required."
]
},
This is my custom FormRequest with validation rules, where I pass it as a parameter in the BillController store method.
namespace App\Http\Requests;
use App\Rules\Sum;
use Dingo\Api\Http\FormRequest;
class BillRequest extends FormRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'currency_id' => 'required|integer|exists:currency,id',
'bills' => ['required', 'array', 'min:1', 'max:3', new Sum],
'bills.*.barcode' => 'required|string|min:10|max:255',
'bills.*.due_date' => 'date',
'bills.*.value' => 'required|numeric|between:10,30000',
];
}
}
Upvotes: 1
Views: 452
Reputation: 737
To solve I had to put *. in front of each rule, as I'm getting an array the validator only understands this way.
public function rules()
{
return [
'*.currency_id' => 'required|integer|exists:currency,id',
'*.bills' => ['required', 'array', 'min:1', 'max:3', new Sum],
'*.bills.*.barcode' => 'required|string|min:10|max:255',
'*.bills.*.due_date' => 'date',
'*.bills.*.value' => 'required|numeric|between:10,30000',
];
}
Upvotes: 1