You are here

function webform_validation_manage_rule_validate in Webform Validation 7

Same name and namespace in other branches
  1. 6 webform_validation.admin.inc \webform_validation_manage_rule_validate()

Validation handler to add / edit a rule.

File

./webform_validation.admin.inc, line 299
Manages validation rules administration UI.

Code

function webform_validation_manage_rule_validate($form, &$form_state) {
  $values = $form_state['values'];
  if ($values['action'] == 'edit') {
    if (!is_numeric($values['ruleid']) || $values['ruleid'] == 0) {
      form_set_error(NULL, t('A problem occurred while editing this rule. Please try again.'));
    }
  }
  $rule_validator = webform_validation_get_validator_info($values['validator']);

  // Validate custom data.
  if (!empty($values['data']) && !empty($rule_validator['custom_data']['validate_regex']) && !preg_match($rule_validator['custom_data']['validate_regex'], $values['data'])) {
    form_set_error('data', $rule_validator['custom_data']['label'] . ' ' . t('is invalid.'));
  }
  $selected_components = count(array_filter($values['rule_components']));

  // Check validator min_components and min_components property when they are
  // equal.
  if (isset($rule_validator['min_components']) && isset($rule_validator['max_components']) && $rule_validator['min_components'] === $rule_validator['max_components'] && $selected_components !== $rule_validator['min_components']) {
    form_set_error('rule_components', format_plural($rule_validator['min_components'], 'You need to select exactly @count component.', 'You need to select exactly @count components.'));
  }
  elseif (isset($rule_validator['min_components']) && $selected_components < $rule_validator['min_components']) {
    form_set_error('rule_components', format_plural($rule_validator['min_components'], 'You need to select at least @count component.', 'You need to select at least @count components.'));
  }
  elseif (isset($rule_validator['max_components']) && $selected_components > $rule_validator['max_components']) {
    form_set_error('rule_components', format_plural($rule_validator['max_components'], 'You can select @count component at most.', 'You can select @count components at most.'));
  }
}