E
E
Evgeny Khripunov2018-07-14 14:17:45
Laravel
Evgeny Khripunov, 2018-07-14 14:17:45

How to validate an array of objects in laravel?

There is a post request to the controller that passes the filenames to myfiles(generatedName,originalName) and the article id

axios.post('/api/articlesUploads', {myfiles: this.files, article_id:this.article_id})

This is what dd($request) looks like from the controller 5b49da3bb79be312529594.png
Trying to validate all originalName and generatedName
$this->validate($request, [
                'article_id' => 'required',
                'myfiles.*.generatedName' => 'required|string|max:255',
                'myfiles.*.originalName' => 'required|string|max:255',
            ]);

And throws an error
"message": "Trying to get property 'originalName' of non-object",

If you try to validate
'myfiles.*.generatedName123' => 'required|string|max:255',

That gives an error
{"message":"The given data was invalid.","errors":{"myfiles.0.123generatedName":["The myfiles.0.123generatedName field is required."],"myfiles.1.123generatedName":["The myfiles.1.123generatedName field is required."]}

It can be seen that the validator correctly iterates over [0] and [1] array objects, therefore, in theory, the correct validation path is ' myfiles.*.generatedName'
What could be the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladislav Nagorny, 2018-07-15
@fannyfan414

fun
i did it
In my controller I have

$request->validate([
            'article_id' => 'required',
            'myfiles.*.generatedName' => 'required|string|max:255',
            'myfiles.*.originalName' => 'required|string|max:255',
        ]);

Threw a normal error
PS Laravel 5.6

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question