Answers.Body: <p>I suspect u will need to use</p>
  
  <p>CRM_Utils_Type::validate (which is what CRM_Utils_Request::retrieve actually calls for validation) directly</p>
  
  <p>lobo</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-04-10T15:28:09.090Z
Answers.LastActivityDate: 2015-04-10T15:28:09.090Z
Answers.UserDisplayName: Donald Lobo
Body: <p>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?</p>
  
  <pre><code>  foreach ($_POST['student_details'] AS $student_record){
  
      $student_details = array(
            'sequential'  =&gt; 1,
            'debug' =&gt; 1,
            'contact_type' =&gt; 'Individual',
            'contact_sub_type' =&gt; 'Student',
            'first_name'  =&gt;  $student_record['student_first_name'],
            'last_name'   =&gt; $student_record['student_last_name'],
            'gender_id'   =&gt;  $student_record['gender_id'],
            'birth_date'  =&gt; $student_record['birth_date'],
            'custom_8'   =&gt; $student_record['shirt_size'],
            'custom_9'   =&gt; $student_record['allergies'],
            'api.Relationship.create' =&gt; array (
                    'sequential' =&gt; 1,
                    'contact_id_a' =&gt; '$value.id',
                    'contact_id_b' =&gt; $parent_id,
                    'relationship_type_id' =&gt; 1,
                    'is_permission_a_b' =&gt; 0,
                    'is_permission_b_a' =&gt; 1,
                  ),
            );
  
      $student_event_list = [];
  
      foreach($student_record['event_id'] AS $student_event) {
        $student_event_list[] = array(
                      'sequential' =&gt; 1,
                      'event_id' =&gt; $student_event,
                      'contact_id' =&gt; '$value.id',
                      'status_id' =&gt; "Pending from pay later",
                      'role_id' =&gt; "Attendee",
                      'register_date' =&gt; date("m/d/Y g:i a"),
                      'is_pay_later' =&gt; 1,
                      'fee_level' =&gt; "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-&gt;getMessage();
            $errorCode = $e-&gt;getErrorCode();
            $errorData = $e-&gt;getExtraParams();
        return $errorMessage . ' (Code ' . $errorCode . ')';
          }
  
    }
  </code></pre>
  
Tags: <api>
Title: How do I use CRM_Utils_Request::retrieve when the POSTed field is an array?
ThreadText: # How do I use CRM_Utils_Request::retrieve when the POSTed field is an array?
  
  (Asked by: username_0 on 2015-04-10)
  
  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 . ')';
          }
  
    }
  
  ```
  
  **Tags:** api
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-04-10. Score: 3 | Accepted answer
  
  I suspect u will need to use
  
  CRM\_Utils\_Type::validate (which is what CRM\_Utils\_Request::retrieve actually calls for validation) directly
  
  username_1
  
  
  ---
  
  