2

In my application, have a form that could (theoretically) have infinite task and I want to define a validation rule in my Request that checks that none of them is equals to each others:

From:

<select name="task[1][id][]" class="form-control">

..

<select name="task[2][id][]" class="form-control">

..
..

<select name="task[n][id][]" class="form-control">

Since I am still not familiar enough with Laravel validations, I was naiv and tried things like:

'task[1][id][]' => 'different:task[2][id][]', 'task[2][id][]' => 'different:task[1][id][]'

or:

'task[1][id][*]' => 'different:task[2][id][*]', 'task[2][id][*]' => 'different:task[1][id][*]'

or:

'task[*][id][*]' => 'different:task[*][id][*]'

I do not get any error, but the validation always passes. I am pretty sure, that there is a good way to get this thing running, but I think that I am on the wrong way..

edit: (additional information as requested)

<select name="task[1][id][]" class="form-control">
      <option value="">&nbsp;</option>
      @foreach ($users as $user)
          <option value="{{ $user->id }}">{{ $user->email }}</option>
      @endforeach
</select>

<select name="task[2][id][]" class="form-control">
      <option value="">&nbsp;</option>
      @foreach($users as $user)
         <option value="{{ $user->id }}">{{ $user->email }}</option>
       @endforeach
</select>

For now, I am using "hardcoded" <select>. Nothing special...

1 Answer 1

1

use distinct

$rules = [
  'task.*.id.*'=>'distinct'
]
Sign up to request clarification or add additional context in comments.

5 Comments

'task.*'=>'distinct' but also 'task.id.*' => 'distinct'is not working.
show your view code, your select form code, this should work fine for arrays
show your real view code, something like generating select field with foreach may be,, or you could dd($request) and show us
Sorry to bother you again, but there is one last thing: how could I ignore "empty" ìd`?
add this nullable|distinct

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.