4

I have a controller which receives a following POST request:

{
  "_token": "csrf token omitted",
  "order": [1,2,3,4,5,6,7,8]
}

How can I use validators to ensure that elements in order are unique, and between 1 and 7? I have tried the following:

$this->validate($request, [
    'order' => 'required|array',
    'order.*' => 'unique|integer|between:1,7'
]);

The first clause is checked, the secound one passes even when the input is invalid.

2 Answers 2

12

Using distinct rule:

distinct

When working with arrays, the field under validation must not have any duplicate values.

In your case, it could look like this:

$this->validate($request, [
    'order' => 'required|array',
    'order.*' => 'distinct|integer|between:1,7'
]);
Sign up to request clarification or add additional context in comments.

Comments

2

The unique validator keyword is for checking a value's duplicates in database.

You should use custom validator for such situations.

See: https://laravel.com/docs/5.1/validation#custom-validation-rules

Comments

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.