Reputation: 131
How to validate with custom request, my request with an array key
$request = [
'link_inc_characteristic_id' => $inc_char_id[$i],
'value' => $value[$i],
'created_by' => $created_by,
'last_updated_by' => $last_updated_by,
];
$this->validate($request, [
'value['.$i.']' => 'max:30'
]);
$linkIncCharacteristicValue = LinkIncCharacteristicValue::create($request);
return Response::json($linkIncCharacteristicValue);
[EDIT] [CODE UPDATED] display error:
Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given,
Upvotes: 0
Views: 2581
Reputation: 131
Its because I am putting the validation inside loop and not use *
character to validate an array
...
for ($i=0; $i < $count ; $i++) {
...
$this->validate($request, [
'value['.$i.']' => 'max:30'
]);
...
// save etc
}
right code, put validation before loop, and use * character for array validation :
$this->validate($request, [
'value.*' => 'max:30'
]);
...
for ($i=0; $i < $count ; $i++) {
// save etc
}
Upvotes: 0
Reputation: 2681
The Controller::validate()
is not a generic validation method, it validates requests. For this use case, simply use the validator directly:
Validator::make($data, ['value' => 'max:30']);
Upvotes: 2
Reputation: 39389
The error message is telling you what’s wrong:
Argument 1 passed to App\Http\Controllers\Controller::validate() must be an instance of Illuminate\Http\Request, array given
So pass the Request
instance to the validate()
method:
public function store(Request $request)
{
$this->validate($request, [
'value.*' => 'max:30',
]);
}
Check out the following resources:
Upvotes: 1