private function WebformCivicrmPostProcess::validateThisPage in Webform CiviCRM Integration 8.5
Recursive validation callback for webform page submission
@todo this shouldn't be needed as each element should handle this.
Parameters
array $elements: FAPI form array
1 call to WebformCivicrmPostProcess::validateThisPage()
- WebformCivicrmPostProcess::validate in src/
WebformCivicrmPostProcess.php - Called after a webform is submitted Or, for a multipage form, called after each page
File
- src/
WebformCivicrmPostProcess.php, line 384 - Front-end form validation and post-processing.
Class
Namespace
Drupal\webform_civicrmCode
private function validateThisPage($elements) {
// Recurse through form elements.
$utils = \Drupal::service('webform_civicrm.utils');
foreach (Element::children($elements) as $key) {
if (is_array($elements[$key]) && ($element = $elements[$key])) {
$this
->validateThisPage($elements[$key]);
if (empty($element['#civicrm_data_type'])) {
continue;
}
if (!isset($element['#value']) || $element['#value'] === '') {
continue;
}
$element_type = $element['#type'] ?? '';
if (strpos($element_type, 'text') !== 0) {
continue;
}
$dt = $element['#civicrm_data_type'];
// Validate state/prov abbreviation
if ($dt === 'state_province_abbr') {
$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 = (int) $utils
->wf_crm_get_civi_setting('defaultContactCountry', 1228);
}
$isBilling = strpos($element['#name'], 'billing_address_') !== false ?? FALSE;
$states = $utils
->wf_crm_get_states($country_id, $isBilling);
if ($states && !array_key_exists(strtoupper($element['#value']), $states)) {
$countries = $utils
->wf_crm_apivalues('address', 'getoptions', [
'field' => 'country_id',
]);
$this->form_state
->setError($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;
}
$this->form_state
->setError($element, t('Please enter @type for %field.', [
'@type' => $dt,
'%field' => $element['#title'],
]));
}
}
}
}