In the snippet below, I build api calls by looping through each student, and then each event within that. However, the field data is coming straight from the request with no server-side validation. Is it possible to use the Request class for array variables? If so, how would I implement it in my code below? What's the proper syntax?
foreach ($_POST['student_details'] AS $student_record){
$student_details = array(
'sequential' => 1,
'debug' => 1,
'contact_type' => 'Individual',
'contact_sub_type' => 'Student',
'first_name' => $student_record['student_first_name'],
'last_name' => $student_record['student_last_name'],
'gender_id' => $student_record['gender_id'],
'birth_date' => $student_record['birth_date'],
'custom_8' => $student_record['shirt_size'],
'custom_9' => $student_record['allergies'],
'api.Relationship.create' => array (
'sequential' => 1,
'contact_id_a' => '$value.id',
'contact_id_b' => $parent_id,
'relationship_type_id' => 1,
'is_permission_a_b' => 0,
'is_permission_b_a' => 1,
),
);
$student_event_list = [];
foreach($student_record['event_id'] AS $student_event) {
$student_event_list[] = array(
'sequential' => 1,
'event_id' => $student_event,
'contact_id' => '$value.id',
'status_id' => "Pending from pay later",
'role_id' => "Attendee",
'register_date' => date("m/d/Y g:i a"),
'is_pay_later' => 1,
'fee_level' => "Single",
);
}
$student_details['api.Participant.create'] = $student_event_list;
try {
$student_api_result = civicrm_api3('Contact', 'create', $student_details);
}
catch (CiviCRM_API3_Exception $e) {
$errorMessage = $e->getMessage();
$errorCode = $e->getErrorCode();
$errorData = $e->getExtraParams();
return $errorMessage . ' (Code ' . $errorCode . ')';
}
}