0

I have a trouble validating an array members in validation for Laravel. For array itself it passes, but for elements themselves, it fails.

Open json in console, shows structure of data being sent. It's clearly a integer. Since it's associative array, not sure how it even finds it's value as show in error handling red rectangle on the bottom of the page. Open json show structure of data being sent

Here is my validation logic:

$validation = Validator::make(
            $request->all(),
            [
                'load_place' => 'required|max:255',
                "unload_place" => 'required|max:255',
                "comment" => 'required|max:255',
                'time_in' => 'required',
                'time_out' => 'required',
                "vehicles" => 'required|array',
                "vehicles.*" => "required|integer",
                'operator_id' => 'required|integer',
                'sec_id' => 'required|integer'
            ]
        );

And here is how it's returned:

    if($validation->fails()){
    
                    $response = array(
                        "message" => "Failed",
                        "errors" => $errors,
                        "test" => $request->vehicles[0]
                    );
                    return response()->json($response);
    
                }

That request[0], is that number at the bottom.

Edit1:

Keys in that marked array, are set on frontend, they are not not predefined. They can be whatever.

1 Answer 1

1

Change "vehicles.*" => "required|integer", to "vehicles.*.vehicle_id" => "required|integer", and it should work.

Keys is set on frontend, they are not not predefined. That what i am trying to say.

Try to replace "vehicles.*" => "required|integer", with new Rule::forEach rule for nested array data - guess it should work.

"vehicles.*" => Rule::forEach(function($value, $attribute) {
    return [
        Rule::integer()->min(1)   //min(1) as eg if it's for an id field which cannot be 0
    ];
}),
Sign up to request clarification or add additional context in comments.

6 Comments

I know that. What if placeholder name for a value, in this case "vehicle_id" is different? e.g. something else as placeholder name?
Whatever key you are using inside individual objects of vehicles array you need to use that in the validation rules. If you are using foo as the key like [{foo:1},{foo:5}] then the validation rule should be "vehicles.*.foo" => "required|integer"
Keys is set on frontend, they are not not predefined. That what i am trying to say.
You can try "vehicles.*.*" => "required|integer" if it works. Otherwise you may have to write your own custom validation rule.
@VeljkoStefanovic Have updated the answer to make use of new Rule::forEach() for validating arrays with nested data. Check if that works
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.