Q:

Validator for varying amounts of data

I have a form with a varying number of fields. It is necessary to make a validator for this form, which verifies that all the fields between them have different values.
<form method="POST" action="url/action">
 <input type="hidden" name="_token" value="{{ csrf_token() }}">
     @for ($i = 0; $i < $num; $i++)
         <div class='form-group'>
             <label>
                 Property №{{ $i+1 }}:
                 <select name="property{{ $i }}">
                     @foreach($properties as $property)
                         <option value="{{ $property->id }}">{{ $property->name }}</option>
                     @endforeach
                 </select>
             </label>
         </div>
     @endfor
 <input type="submit" class="btn btn-primary" value="Добавить">
</form>

The result of the form submission is to create a table that has these properties, so they must all be different.
Maybe the validator would look as follows.
Validator::make($data, [
    'property1' => 'unique',
    'property2' => 'unique',
    ...
]);

Help write a validator, please.

A:

Below is a solution to this problem.
protected function validatorForProperties(array $data)
{
    $rules = [];
    foreach ($data as $key1 => $value1) {
        $rule = 'different:';
        foreach ($data as $key2 => $value2) {
            if ($key1 != $key2)
                $rule = $rule . $key2 . ',';
        }
        $rules = array_merge($rules, [$key1 => $rule]);
    }
    return Validator::make($data, $rules);
}

