You are here

function webform_validation_manage_rule in Webform Validation 7

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

Callback function to add or edit a validation rule.

1 string reference to 'webform_validation_manage_rule'
webform_validation_menu in ./webform_validation.module
Implements hook_menu().

File

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

Code

function webform_validation_manage_rule($form, $form_state, $node, $action, $validator, $rule = NULL) {
  $form = array();
  $rule_validator = webform_validation_get_validator_info($validator);
  if ($action === 'edit') {
    $title = t('Edit validation rule %title', array(
      '%title' => $rule['rulename'],
    ));
  }
  else {
    $title = t('Add validation rule');
  }
  drupal_set_title($title, PASS_THROUGH);
  $form['rule']['validator'] = array(
    '#type' => 'hidden',
    '#value' => $validator,
  );
  $form['rule']['action'] = array(
    '#type' => 'hidden',
    '#value' => $action,
  );
  if ($action == 'edit' && $rule) {
    $form['rule']['ruleid'] = array(
      '#type' => 'hidden',
      '#value' => $rule['ruleid'],
    );
    $form['rule']['nid'] = array(
      '#type' => 'hidden',
      '#value' => $rule['nid'],
    );
  }
  else {
    $form['rule']['nid'] = array(
      '#type' => 'hidden',
      '#value' => $node->nid,
    );
  }
  $form['rule']['type'] = array(
    '#type' => 'item',
    '#title' => t('Rule type'),
    '#markup' => $rule_validator['name'],
  );
  $form['rule']['rulename'] = array(
    '#type' => 'textfield',
    '#title' => t('Rule name'),
    '#default_value' => isset($rule['rulename']) ? $rule['rulename'] : NULL,
    '#required' => TRUE,
    '#size' => 60,
    '#maxlength' => 255,
    '#weight' => 1,
  );
  $form['rule']['rule_components'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Components'),
    '#weight' => 3,
    '#description' => t('Select the components to be validated by this validation rule'),
    '#options' => webform_validation_get_webform_components($node, $validator),
    '#default_value' => isset($rule['components']) ? array_keys($rule['components']) : array(),
  );
  if (!empty($rule_validator['custom_data']) && is_array($rule_validator['custom_data'])) {
    $valid_types = array(
      'textfield',
      'textarea',
    );
    $type = isset($rule_validator['custom_data']['type']) ? $rule_validator['custom_data']['type'] : 'textfield';
    if (!in_array($type, $valid_types)) {
      $type = 'textfield';
    }
    $required = isset($rule_validator['custom_data']['required']) ? $rule_validator['custom_data']['required'] : TRUE;
    $form['rule']['data'] = array(
      '#type' => $type,
      '#title' => $rule_validator['custom_data']['label'],
      '#description' => $rule_validator['custom_data']['description'],
      '#required' => (bool) $required,
      '#size' => 60,
      '#maxlength' => NULL,
      '#default_value' => isset($rule['data']) ? $rule['data'] : NULL,
      '#weight' => 4,
    );
  }
  if (!empty($rule_validator['negatable'])) {
    $form['rule']['negate'] = array(
      '#type' => 'checkbox',
      '#title' => t('Negate rule'),
      '#description' => t('Validate the inverse of the rule.'),
      '#default_value' => isset($rule['negate']) ? $rule['negate'] : NULL,
      '#weight' => 5,
    );
  }
  if (!empty($rule_validator['custom_error'])) {
    $form['rule']['error_message'] = array(
      '#type' => 'textfield',
      '#title' => t('Custom error message'),
      '#description' => t("Specify an error message that should be displayed when user input doesn't pass validation"),
      '#required' => TRUE,
      '#size' => 60,
      '#maxlength' => 2048,
      '#default_value' => isset($rule['error_message']) ? $rule['error_message'] : NULL,
      '#weight' => 5,
    );
  }
  $form['rule']['submit'] = array(
    '#type' => 'submit',
    '#value' => isset($rule['ruleid']) ? t('Save rule') : t('Add rule'),
    '#weight' => 25,
  );
  $destination = drupal_get_destination();
  $form['rule']['cancel'] = array(
    '#markup' => l(t('Cancel'), $destination['destination']),
    '#weight' => 26,
  );
  return $form;
}