4

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 ?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. I've never pulled something from github, so I have no idea how to do that. Can you give me some directions ? I have git (bash) installed.
@Kockar It's not something that you do. At some point, if it is acceptable, Taylor will merge the pull request(s) into Laravel, tag a new release, and then you'll need to run a composer update to get the latest Laravel version. You just need to keep an eye on the pull requests to see when they're merged in, so you know when to update.

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.