Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

haakym's avatar

Form Validation, multiple "different" rules

Hey everyone,

I've got a form that will have multiple email addresses and they should all be unique. I'm aware I have access to the "different" rule that comes with Laravel:

different:*field*

The field under validation must have a different value than *field*.

http://laravel.com/docs/5.1/validation#rule-different

My issue is that I'm unsure how I should write the rule for each email field using the different rule to apply to all the other email fields. My first guess is a double for loop? So let's say I have 8 email fields, all named sequentially email[1], email[2] etc, I'd do something like this:

for ($i = 1; $i < 8; $i++) {

    for ($j = 1; $j < 8; $i++) {

        if ($j !== $i) {
            $rules['email.' . $i] .= '|different:email.' . $j;
        }

    }

}

So, if anyone could provide some guidance on the best way to do this I'd highly appreciate it.

Another issue I'm confused about is should I use the actual value of the input or can I simply reference the input name? So instead of:

$rules['email.' . $i] .= '|different:email.' . $j;

I would put:

$rules['email.' . $i] .= '|different:' . \Input::get('email.' . $j;
0 likes
2 replies
pmall's avatar

You must only reference input name in validation rules.

It is hard to create this rule. You can create a custom rule.

Also, maybe you can have a workaround. Do you really need the emails to be unique ? You could just not return an error to the user and deal with the list of unique email on the server side.

haakym's avatar

You must only reference input name in validation rules.

Thank you. Makes sense that in the docs the rule parameter is named field and not something like value.

It is hard to create this rule. You can create a custom rule.

Gotcha, might not be worth it in the scope of the current project I'm in but thanks for the suggestion.

Also, maybe you can have a workaround. Do you really need the emails to be unique? You could just not return an error to the user and deal with the list of unique email on the server side.

Yes, they should be unique.

I'd usually get round this by firstly using dropdown boxes instead of text input and disabling the value from all dropdown text boxes when it has been selected from one. However I've had to use text input, so can't implement this solution!

Please or to participate in this conversation.