Dimitar Spasovski
Dimitar Spasovski

Reputation: 2132

Array validation in Laravel 5.2

I am developing a quiz application in Laravel and I have some problems with the array validation. I use AngularJS on the front end and I use ajax in order to send objects to the Laravel API. This is a sample JSON object :

{"name":"TestName","category":"TestCategory","questions":[{"answers":[{"type":"radio","information":"Test answer two","is_correct":false,"$$hashKey":"object:28"},{"type":"radio","information":"Test answer One","is_correct":false,"$$hashKey":"object:22"}],"$$hashKey":"object:13","question_text":"Test Question One"}]} 

The quiz has name, category and questions. Each question must have question_text and answers. Each answer has type, information and is_correct.

Here is the validation I wrote :

   $this->validate($request, [
            'name' => 'required|min:3',
            'category' => 'required|min:2',
            'questions' => 'required',
            'questions.*.question_text' => 'required|min:5',
            'questions.*.answers' => 'required'

        ]);

The name and category validations work fine. The third validation ('questions => 'required') works fine as well. The rest of the validations do nothing. For example,

{"name":"SomeName","category":"SomeCategory","questions":[{}]}

passes the validation although the questions array has an element that doesn't have answers or question_text field. How does the array validation work ?

Upvotes: 4

Views: 547

Answers (1)

patricus
patricus

Reputation: 62228

This is a known issue.

There is an open pull request that addresses the "required" validation. You can follow this pull request here.

There is also a second pull request that addresses the issue with the "required_*" validations (required_with, etc.). You can follow that pull request here.

Upvotes: 3

Related Questions