You are here

function _wf_crm_validate in Webform CiviCRM Integration 7.3

Recursive validation callback for webform submissions.

Parameters

$form: FAPI form array

$form_state: FAPI form_state array

$submitted: Webform submission array

1 call to _wf_crm_validate()
wf_crm_validate in ./webform_civicrm.module
Validation callback for webform submissions.

File

./webform_civicrm_forms.inc, line 1262

Code

function _wf_crm_validate($form, &$form_state, $submitted) {

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

        // Validate state/prov abbreviation
        if ($dt == 'state_province_abbr') {
          $ckey = str_replace('state_province', 'country', $key);
          if (!empty($submitted[$ckey]) && is_numeric($submitted[$ckey])) {
            $country_id = $submitted[$ckey];
          }
          else {
            $config = CRM_Core_Config::singleton();
            $country_id = $config->defaultContactCountry;
          }
          $states = wf_crm_get_options('state_province', $country_id);
          if ($states && !array_key_exists(strtoupper($element['#value']), $states)) {
            $countries = wf_crm_get_options('country');
            form_error($element, t('Mismatch: "@state" is not a state/province of %country. Please enter a valid state/province abbreviation for %field.', array(
              '@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.', array(
            '@type' => $dt,
            '%field' => $element['#title'],
          )));
        }
      }
    }
  }
}