You are here

private function wf_crm_webform_postprocess::validateThisPage in Webform CiviCRM Integration 7.5

Same name and namespace in other branches
  1. 7.4 includes/wf_crm_webform_postprocess.inc \wf_crm_webform_postprocess::validateThisPage()

Recursive validation callback for webform page submission

Parameters

array $elements: FAPI form array

1 call to wf_crm_webform_postprocess::validateThisPage()
wf_crm_webform_postprocess::validate in includes/wf_crm_webform_postprocess.inc
Called after a webform is submitted Or, for a multipage form, called after each page

File

includes/wf_crm_webform_postprocess.inc, line 321

Class

wf_crm_webform_postprocess

Code

private function validateThisPage($elements) {

  // Recurse through form elements.
  foreach (element_children($elements) as $key) {
    if (is_array($elements[$key]) && ($element = $elements[$key])) {
      $this
        ->validateThisPage($elements[$key]);
      if (!empty($element['#civicrm_data_type']) && substr(wf_crm_aval($element, '#type', ''), 0, 4) === 'text' && isset($element['#value']) && $element['#value'] !== '') {
        $dt = $element['#civicrm_data_type'];

        // Validate state/prov abbreviation
        if ($dt == 'state_province_abbr') {
          if (!empty($this->crmValues[$key])) {
            $ckey = str_replace('state_province', 'country', $key);
            if (!empty($this->crmValues[$ckey]) && is_numeric($this->crmValues[$ckey])) {
              $country_id = $this->crmValues[$ckey];
            }
            else {
              $country_id = wf_crm_get_civi_setting('defaultContactCountry', 1228);
            }
            $states = wf_crm_get_states($country_id);
            if ($states && !array_key_exists(strtoupper($element['#value']), $states)) {
              $countries = wf_crm_apivalues('address', 'getoptions', [
                'field' => 'country_id',
              ]);
              form_error($element, t('Mismatch: "@state" is not a state/province of %country. Please enter a valid state/province abbreviation for %field.', [
                '@state' => $element['#value'],
                '%country' => $countries[$country_id],
                '%field' => $element['#title'],
              ]));
            }
          }
        }
        elseif ($dt !== 'String' && $dt !== 'Memo' && $dt !== 'File' && CRM_Utils_Type::escape($element['#value'], $dt, FALSE) === NULL) {

          // Allow data type names to be translated
          switch ($dt) {
            case 'Int':
              $dt = t('an integer');
              break;
            case 'Float':
              $dt = t('a number');
              break;
            case 'Link':
              $dt = t('a web address starting with http://');
              break;
            case 'Money':
              $dt = t('a currency value');
              break;
          }
          form_error($element, t('Please enter @type for %field.', [
            '@type' => $dt,
            '%field' => $element['#title'],
          ]));
        }
      }
    }
  }
}