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

chechogrom's avatar

Validation array

Hello everyone, I have a request where I am sent an array of phone numbers, and I would like that for each element could have a special validation for example: if the country of the phone is 1, then the maximum and minimum phone number must be one in specific, if it is different from 1 the maximum and minimum phone number must be another one.

thanks in advance to those who can help me.

I tried traversing the array and adding the rules dynamically but the following form does not work in my FormRequest.

if ($this->listContactPhone) {
            $rule['listContactPhone'] = 'array|min:2|max:2';
            foreach ($this->listContactPhone as $key => $value) {
                if ($value['phoneTypeContact'] == 1) {
                    $rule["listContactPhone.{$key}.phoneContact"] = 'nullable|max:15|min:5|regex:/^[0-9]+$/';
                }

                if ($value['phoneTypeContact'] == 2 && $value['countryContact'] == "1") {
                    $rule["listContactPhone.{$key}.phoneContact"] = 'nullable|max:10|min:10|regex:/^[0-9]+$/';
                }

                if ($value['phoneTypeContact'] == 2 && $value['countryContact'] != "1") {
                    $rule["listContactPhone.{$key}.phoneContact"] = 'nullable|max:20|min:8|regex:/^[0-9]+$/';
                }

                $rule["listContactPhone.{$key}.countryContact"]    = 'required';
                $rule["listContactPhone.{$key}.phoneTypeContact"]  = 'required';
                $rule["listContactPhone.{$key}.extensionContact"]  = 'nullable|max:3';
                $rule["listContactPhone.{$key}.departmentContact"]  = 'nullable|integer';
                $rule["listContactPhone.{$key}.tipo"]  = 'required|in:0,1';
            }
}
0 likes
7 replies
vincent15000's avatar

I don't understand, can you give a concret example please ?

chechogrom's avatar

@vincent15000 I have an array of phone numbers, each item has, the country id and the type of phone, if the type of phone is 1 I need the max or min rule for the phone number to be max:10, min:5, if the type of phone is 2, the validation rule for that phone max or min for the phone number to be max:10, min:10.

example

the first phone is type 1 so the validation rules for that phone should be as follows: max:10, min:5.

in the second phone is type 2 the validation rules in that phone should be as follows: max:10, min:10.

but they belong to the same array

1 like
chechogrom's avatar

@vincent15000 I have not tried it, but I think it is the solution, I just have one question, in my FormRequest the variable $validator which is it ?

PD: I am currently in version 6 of laravel

Michael88's avatar

Hi! You should use Laravel validator I think https://laravel.com/docs/9.x/validation#creating-form-requests and add your custom validation rule CheckPhoneContactRule https://laravel.com/docs/9.x/validation#custom-validation-rules

return [
	'listContactPhone' => 'array|min:2|max:2',
	'listContactPhone.*.countryContact' => 'required',
	'listContactPhone.*.phoneTypeContact' =>  'required',
	'listContactPhone.*.extensionContact' => 'nullable|max:3',
	'listContactPhone.*.departmentContact' => 'nullable|numeric',
	'listContactPhone.*.tipo' => 'required|in:0,1',
	'listContactPhone.*.phoneContact' => new CheckPhoneContactRule($this-> phoneTypeContact, $this-> countryContact)
];
1 like
Michael88's avatar

@vincent15000 he also needs to pass countryContact there, all this logic can be in custom validator and also I don't understand why are {$key} and foreach needed

Please or to participate in this conversation.