I don't understand, can you give a concret example please ?
Oct 6, 2022
7
Level 1
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';
}
}
Please or to participate in this conversation.