Array
Laravel Validation Array
Categories:
Validate input array field with the same data
use Illuminate\Support\Facades\Validator;
$request_input = [
'user_id_list' = [
'u-1111',
'u-2222',
'u-3333',
],
];
$validator = Validator::make($request_input, [
'user_id_list' => [
'array',
],
'user_id_list.*' => [
'string',
'min:5',
'max:60',
],
]);
Validate input different array with the same fields
use Illuminate\Support\Facades\Validator;
$request_input = [
[
'name' => 'KJ',
'age' => 18,
],
[
'name' => 'Kay',
'age' => 18,
],
[
'name' => 'Jay',
'age' => 18,
],
];
$validator = Validator::make($request->all(), [
'*.name' => [
'string',
'min:2',
'max:60',
],
'*.age' => [
'integer',
'min:0',
'max:150',
],
]);
Validate input array field keys
use Illuminate\Support\Facades\Validator;
$request_input = [
'user' => [
'name' => 'KJ',
'age' => 18,
],
];
Validator::make($request_input, [
'user' => 'array:name,age',
]);
Validate input fields with its own array list
use Illuminate\Support\Facades\Validator;
$request_input = [
'user_list' => [
[
'name' => 'KJ',
'age' => 18,
],
[
'name' => 'Kay',
'age' => 18,
],
[
'name' => 'Jay',
'age' => 18,
],
],
];
Validator::make($request_input, [
'user_list.*.name' => [
'string',
'min:2',
'max:60',
],
'user_list.*.age' => [
'integer',
'min:0',
'max:150',
],
]);