So I have a table with dynamic fields communication_type_id and communication_value. If user selects communication_type_id = 4 then communication_value should be phone number which is required and numeric if user select Communication_type_id =7 then communication_value should be required and email.How do I do this? I have done following till now but it does not seem to be checking the condition as I get "communication_value must be a valid email address" for both conditions:
$validator = Validator::make($request->all(), [
'name' => 'required',
'client_addresses.*.address'=>'required',
'client_addresses.*.city'=>'required',
'client_addresses.*.state_province'=>'required',
'client_addresses.*.country'=>'required',
//Communication|
'client_communications.*.communication_type_id'=>'required|numeric',
//tag
'tags.*.tag_id'=>'required',
]);
foreach ($request->client_communications as $key=>$com) {
// dd($com);
if($com['communication_type_id']== "5")
{
$validator = Validator::make($request->all(), [
'client_communications.'.$key.'.communication_value' => 'required|numeric',
]);
}
if ($com['communication_type_id'] == "7"){
$validator = Validator::make($request->all(), [
'client_communications.'.$key.'.communication_value' => 'required|email',
]);
}
}
if ($validator->fails()) {
return response()->json(['errors'=>$validator->errors()]);
}