You are here

function webform_conditionals_form_validate in Webform 7.4

Validate handler for webform_conditionals_form().

Prohibit the source and target of a conditional rule from being the same.

1 string reference to 'webform_conditionals_form_validate'
webform_conditionals_form in includes/webform.conditionals.inc
Form builder; Provide the form for adding conditionals to a webform node.

File

includes/webform.conditionals.inc, line 196
Form elements and menu callbacks to provide conditional handling in Webform.

Code

function webform_conditionals_form_validate($form, &$form_state) {

  // Skip validation unless this is saving the form.
  $button_key = end($form_state['triggering_element']['#array_parents']);
  if ($button_key !== 'submit') {
    return;
  }
  $node = $form['#node'];
  $components = $node->webform['components'];
  $component_options = webform_component_options();
  foreach ($form_state['complete form']['conditionals'] as $conditional_key => $element) {
    if (substr($conditional_key, 0, 1) !== '#' && $conditional_key !== 'new') {
      $conditional = $element['conditional'];
      $targets = array();
      foreach ($conditional['actions'] as $action_key => $action) {
        if (is_numeric($action_key)) {
          $operation = $action['action']['#value'];
          $target_id = $action['target']['#value'];
          if (isset($targets[$target_id][$operation])) {
            form_set_error('conditionals][' . $conditional_key . '][actions][' . $action_key . '][target', t('A operation %op cannot be made for a component more than once. (%target).', array(
              '%op' => $action['action']['#options'][$operation],
              '%target' => $components[$action['target']['#value']]['name'],
            )));
          }
          $component_type = $node->webform['components'][$action['target']['#value']]['type'];
          if (!webform_conditional_action_able($component_type, $action['action']['#value'])) {
            form_set_error('conditionals][' . $conditional_key . '][actions][' . $action_key . '][action', t("A component of type %type can't be %action. (%target)", array(
              '%action' => $action['action']['#options'][$action['action']['#value']],
              '%type' => $component_options[$component_type],
              '%target' => $components[$action['target']['#value']]['name'],
            )));
          }
          $targets[$target_id][$operation] = $target_id;
        }
      }
      foreach ($conditional['rules'] as $rule_key => $rule) {
        if (!is_numeric($rule_key)) {
          continue;
        }
        $source_cid = isset($rule['source']['#value']) ? $rule['source']['#value'] : NULL;

        // Validate component rules, but not conditional_start/end rules.
        if ($source_cid && $rule['source_type']['#value'] == 'component' && isset($targets[$source_cid])) {
          form_set_error('conditionals][' . $conditional_key . '][rules][' . $rule_key . '][source', t('The subject of the conditional cannot be the same as the component that is changed (%target).', array(
            '%target' => $components[$source_cid]['name'],
          )));
        }
        if ($source_cid && $components[$source_cid]['type'] === 'date' && strtotime($rule['value']['#value']) === FALSE) {
          form_set_error('conditionals][' . $conditional_key . '][rules][' . $rule_key . '][value', t('The conditional comparison value must be a valid date.'));
        }
      }
    }
  }

  // Form validation will not rebuild the form, so we need to ensure
  // necessary JavaScript will still exist.
  _webform_conditional_expand_value_forms($node);
}