You are here

function clientside_validation_webform_after_build_recurse in Clientside Validation 6

Same name and namespace in other branches
  1. 7 clientside_validation_webform/clientside_validation_webform.module \clientside_validation_webform_after_build_recurse()
1 call to clientside_validation_webform_after_build_recurse()
clientside_validation_webform_clientside_validation_webform_alter in clientside_validation_webform/clientside_validation_webform.module
@file Adds clientside validation support for the webform module

File

clientside_validation_webform/clientside_validation_webform.module, line 20
Adds clientside validation support for the webform module

Code

function clientside_validation_webform_after_build_recurse($form_id, &$form, &$form_state, &$js_rules) {
  if ($children = array_values(element_children($form))) {
    foreach ($children as $index => $item) {
      $element = $form[$item];
      if (isset($element['#title'])) {
        if (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'time' && isset($element['hour']['#name'])) {
          $message = t('Hour in !title field is required.', array(
            '!title' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', ''),
          ));
          _clientside_validation_set_required($element['hour']['#name'], $element['#title'], $element['#required'], $js_rules, $message);
          $message = t('Minute in !title field is required.', array(
            '!title' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', ''),
          ));
          _clientside_validation_set_required($element['minute']['#name'], $element['#title'], $element['#required'], $js_rules, $message);
        }
        elseif (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'date') {
          $message = t('Month in !title field is required.', array(
            '!title' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', ''),
          ));
          _clientside_validation_set_required($element['#name'] . '[month]', $element['#title'], $element['#required'], $js_rules, $message);
          $message = t('Day in !title field is required.', array(
            '!title' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', ''),
          ));
          _clientside_validation_set_required($element['#name'] . '[day]', $element['#title'], $element['#required'], $js_rules, $message);
          $message = t('Year in !title field is required.', array(
            '!title' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', ''),
          ));
          _clientside_validation_set_required($element['#name'] . '[year]', $element['#title'], $element['#required'], $js_rules, $message);
          if (isset($element['#year_start']) && isset($element['#year_end'])) {
            if (is_numeric($element['#year_start']) && is_numeric($element['#year_end'])) {
              $message = t('The entered date needs to be between the years @start and @end.', array(
                '@start' => $element['#year_start'],
                '@end' => $element['#year_end'],
              ));
              _clientside_validation_set_minmax($element['#name'] . '[year]', $element['#title'], $element['#year_start'], $element['#year_end'], $js_rules, $message);
            }
          }
          elseif (isset($element['#start_date']) || isset($element['#end_date'])) {
            if (isset($element['#start_date']) && isset($element['#end_date'])) {
              $message = t('The entered date needs to be between the dates @start and @end.', array(
                '@start' => $element['#start_date'],
                '@end' => $element['#end_date'],
              ));
              $start_date = explode('-', $element['#start_date']);
              $end_date = explode('-', $element['#end_date']);
            }
            elseif (isset($element['#start_date'])) {
              $message = t('The entered date needs to be before @start', array(
                '@start' => $element['#start_date'],
              ));
              $start_date = explode('-', $element['#start_date']);
              $end_date = '';
            }
            else {
              $message = t('The entered date needs to be before @end', array(
                '@end' => $element['#end_date'],
              ));
              $start_date = '';
              $end_date = explode('-', $element['#end_date']);
            }
            $id = 'webform-component-' . str_replace('_', '-', implode('--', array_slice($element['#parents'], 1)));
            _clientside_validation_set_minmax_date($id, $element['#title'], $start_date, $end_date, $js_rules, $message);
          }
        }
        elseif ($element['#type'] == 'checkboxes') {
          $id = '#' . $element['#id'] . '-wrapper';
          _clientside_validation_set_checkboxgroup_minmax($element['#name'], $element['#title'], $id, $js_rules, intval($element['#required']));
        }
        elseif ($element['#type'] == 'select' && $element['#multiple']) {
          _clientside_validation_set_required($element['#name'] . '[]', $element['#title'], intval($element['#required']), $js_rules);
        }
        elseif (isset($element['#type']) && $element['#required']) {
          _clientside_validation_set_required($element['#name'], $element['#title'], intval($element['#required']), $js_rules);
        }
        if (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'file') {
          $file_children = element_children($element);
          if (count($file_children) > 0) {
            foreach ($file_children as $child) {
              $file_child = $element[$child];
              if (isset($file_child['#name']) && $file_child['#type'] == 'file') {
                $name = $file_child['#name'];
                _clientside_validation_set_required($name, $element['#title'], (bool) $element['#webform_component']['mandatory'], $js_rules);
                if (isset($element['#webform_component']['extra']['filtering']['types'])) {
                  $extensions = $element['#webform_component']['extra']['filtering']['types'];
                  _clientside_validation_set_extensions($name, $extensions, $js_rules);
                }
              }
            }
          }
        }
        if (isset($element['#maxlength']) && $element['#maxlength']) {
          $message = t('!title field has a max length of !maxl characters.', array(
            '!title' => variable_get('clientside_validation_prefix', '') . $element['#title'] . variable_get('clientside_validation_suffix', ''),
            '!maxl' => $element['#maxlength'],
          ));
          _clientside_validation_set_minmaxlength($element['#name'], $element['#title'], '', $element['#maxlength'], $js_rules, $message);
        }
        if (isset($element['#webform_component']) && $element['#webform_component']['type'] == 'email') {
          _clientside_validation_set_email($element['#name'], $element['#title'], $js_rules);
        }
      }
      clientside_validation_webform_after_build_recurse($form_id, $element, $form_state, $js_rules);
    }
  }
}