You are here

function field_validation_manage_rule in Field Validation 7

Callback function to add or edit a validation rule

2 string references to 'field_validation_manage_rule'
field_validation_callback_dispatch in ./field_validation.admin.inc
Menu callback function using to dispatch.
field_validation_menu in ./field_validation.module
Implements hook_menu().

File

./field_validation.admin.inc, line 115
Manages validation rules administration UI

Code

function field_validation_manage_rule($form, $form_state, $instance, $action = 'add', $validator = 'regex', $ruleid = NULL) {
  $form = array();
  $rule_validator = field_validation_get_validator_info($validator);
  if ($action == 'overwrite') {
    $rule = field_validation_rule_load($ruleid);
    $rule = (array) $rule;
  }
  else {
    $rule = field_validation_get_rule($ruleid);
  }
  $form['rule'] = array(
    '#type' => 'fieldset',
    '#title' => $action == 'edit' ? t('Edit rule') : t('Add rule'),
    '#collapsible' => FALSE,
    '#collapsed' => FALSE,
  );
  $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']['field_name'] = array(
      '#type' => 'hidden',
      '#value' => $rule['field_name'],
    );
    $form['rule']['entity_type'] = array(
      '#type' => 'hidden',
      '#value' => $rule['entity_type'],
    );
    $form['rule']['bundle'] = array(
      '#type' => 'hidden',
      '#value' => $rule['bundle'],
    );
  }
  else {
    $form['rule']['field_name'] = array(
      '#type' => 'hidden',
      '#value' => $instance['field_name'],
    );
    $form['rule']['entity_type'] = array(
      '#type' => 'hidden',
      '#value' => $instance['entity_type'],
    );
    $form['rule']['bundle'] = array(
      '#type' => 'hidden',
      '#value' => $instance['bundle'],
    );
  }
  $form['rule']['rulename'] = array(
    '#type' => 'textfield',
    '#title' => t('Rule name'),
    '#default_value' => isset($rule['rulename']) ? $rule['rulename'] : '',
    '#required' => TRUE,
    '#size' => 60,
    '#maxlength' => 255,
    '#weight' => 1,
  );
  $form['rule']['name'] = array(
    '#type' => 'machine_name',
    '#default_value' => isset($rule['name']) ? $rule['name'] : '',
    '#machine_name' => array(
      'exists' => 'field_validation_rule_load',
      'source' => array(
        'rulename',
      ),
    ),
    '#weight' => 2,
    '#maxlength' => 32,
  );
  $field = field_info_field($instance['field_name']);
  $col_options = array(
    '' => t('Choose a column'),
  );
  $columns = !empty($field['columns']) ? $field['columns'] : array();
  foreach ($columns as $key => $column) {
    $col_options[$key] = $key;
  }
  $form['rule']['col'] = array(
    '#type' => 'select',
    '#options' => $col_options,
    '#title' => t('Column'),
    '#description' => t('A column defined in the hook_field_schema() of this field.'),
    '#default_value' => isset($rule['col']) ? $rule['col'] : '',
    '#required' => TRUE,
    '#weight' => 3,
  );
  if (isset($rule_validator['custom_data']) && is_array($rule_validator['custom_data'])) {
    $required = isset($rule_validator['custom_data']['required']) ? $rule_validator['custom_data']['required'] : TRUE;
    $form['rule']['data'] = array(
      '#type' => 'textfield',
      '#title' => $rule_validator['custom_data']['label'],
      '#description' => $rule_validator['custom_data']['description'],
      '#required' => $required !== FALSE ? TRUE : FALSE,
      '#size' => 60,
      '#maxlength' => 255,
      '#default_value' => $rule['data'],
      '#weight' => 4,
    );
  }
  if (isset($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' => 255,
      '#default_value' => $rule['error_message'],
      '#weight' => 5,
    );
  }
  $form['rule']['submit'] = array(
    '#type' => 'submit',
    '#value' => isset($rule['ruleid']) ? t('Save rule') : t('Add rule'),
    '#weight' => 25,
  );
  if ($action == 'overwrite') {
    $form['rule']['submit']['#value'] = t('Overwrite rule');
  }
  return $form;
}