You are here

function clientside_validation_settings_form in Clientside Validation 7

Same name and namespace in other branches
  1. 7.2 clientside_validation.admin.inc \clientside_validation_settings_form()
1 string reference to 'clientside_validation_settings_form'
clientside_validation_menu in ./clientside_validation.module
Implements hook_menu().

File

./clientside_validation.admin.inc, line 162
Admin settings for Clientside Validation

Code

function clientside_validation_settings_form($form, $form_state, $cvs_formid = NULL, $cvs_type = 'default') {
  $settings = clientside_validation_settings_load($cvs_type, $cvs_formid, TRUE);
  $form['new'] = array(
    '#type' => 'value',
    '#value' => FALSE,
  );
  if ($cvs_type == 'default') {
    drupal_set_title(clientside_validation_settings_title($cvs_type, $cvs_formid, TRUE), PASS_THROUGH);
  }
  else {
    if (!$settings || $settings['settings'] === FALSE || empty($settings['form_id'])) {
      $settings = clientside_validation_settings_load_defaults();
      drupal_set_title(clientside_validation_settings_title($cvs_type, $cvs_formid, FALSE), PASS_THROUGH);
      $form['new'] = array(
        '#type' => 'value',
        '#value' => TRUE,
      );
    }
    else {
      drupal_set_title(clientside_validation_settings_title($cvs_type, $cvs_formid, TRUE), PASS_THROUGH);
    }
  }

  // Make sure settings aren't nested.
  if (isset($settings['settings']) && is_array($settings['settings'])) {
    $settings = $settings['settings'];
  }
  $form['#tree'] = TRUE;
  $form['cvs_formid'] = array(
    '#type' => 'value',
    '#value' => $cvs_formid,
  );
  $form['cvs_type'] = array(
    '#type' => 'value',
    '#value' => $cvs_type,
  );

  //Validate options
  $form['validate_options'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Validate options'),
    '#weight' => 10,
  );
  if (module_exists('clientside_validation_html5')) {
    $form['validate_options']['disable_html5'] = array(
      '#type' => 'radios',
      '#options' => array(
        '1' => t('Yes'),
        '0' => t('No'),
      ),
      '#title' => t('Replace HTML5 validation with Clientside Validation'),
      '#default_value' => $settings['validate_options']['disable_html5'],
      '#description' => t('Replace HTML5 validation with Clientside Validation on forms that have Clientside Validation enabled'),
    );
  }
  else {
    $form['validate_options']['disable_html5'] = array(
      '#type' => 'value',
      '#value' => $settings['validate_options']['disable_html5'],
    );
  }
  if (module_exists('clientside_validation_form')) {
    $form['validate_options']['captcha'] = array(
      '#type' => 'radios',
      '#options' => array(
        '1' => t('Yes'),
        '0' => t('No'),
      ),
      '#title' => t('Validate CAPTCHA'),
      '#default_value' => $settings['validate_options']['captcha'],
      '#description' => t('Enable or disable Clientside Validation for CAPTCHA elements'),
    );
  }
  else {
    $form['validate_options']['captcha'] = array(
      '#type' => 'value',
      '#value' => $settings['validate_options']['captcha'],
    );
  }
  $form['validate_options']['validate_onsubmit'] = array(
    '#type' => 'radios',
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No'),
    ),
    '#title' => t('Validate fields on submit'),
    '#description' => t('"No" disables onsubmit validation, allowing the user to submit whatever he wants, while still validating on keyup/blur/click events (if not specified otherwise).'),
    '#default_value' => $settings['validate_options']['validate_onsubmit'],
  );
  $form['validate_options']['validate_onblur'] = array(
    '#type' => 'radios',
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No'),
    ),
    '#title' => t('Validate fields on blur'),
    '#description' => t('Validate elements (except checkboxes/radio buttons) on blur. <strong>If nothing is entered, all rules are skipped, except when the field was already marked as invalid.</strong>'),
    '#default_value' => $settings['validate_options']['validate_onblur'],
  );
  $form['validate_options']['validate_onblur_always'] = array(
    '#type' => 'radios',
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No'),
    ),
    '#title' => t('Always validate fields on blur'),
    '#description' => t('Validate elements (except checkboxes/radio buttons) on blur, <strong>even nothing is entered before or if the fields hasn\'t been marked as invalid before.</strong>'),
    '#default_value' => $settings['validate_options']['validate_onblur_always'],
    '#states' => array(
      'visible' => array(
        ':input[name="validate_options[validate_onblur]"]' => array(
          'value' => 1,
        ),
      ),
    ),
  );
  $form['validate_options']['validate_onkeyup'] = array(
    '#type' => 'radios',
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No'),
    ),
    '#title' => t('Validate fields on key up'),
    '#description' => t('Validate elements on keyup. <strong>As long as the field is not marked as invalid, nothing happens</strong>. Otherwise, all rules are checked on each key up event.'),
    '#default_value' => $settings['validate_options']['validate_onkeyup'],
  );
  $form['validate_options']['validate_before_ajax'] = array(
    '#type' => 'radios',
    '#options' => array(
      '1' => t('Yes'),
      '0' => t('No'),
    ),
    '#title' => t('Validate before AJAX calls'),
    '#description' => t('Validate elements before ajax calls. Use with caution, this behavior has been known to !cause_issues.', array(
      '!cause_issues' => l(t('cause issues'), 'https://drupal.org/project/issues/clientside_validation?text=4bf5b2d&status=All'),
    )),
    '#default_value' => $settings['validate_options']['validate_before_ajax'],
  );
  $form['validate_options']['show_messages'] = array(
    '#type' => 'radios',
    '#options' => array(
      '0' => t('Show all error messages'),
      '1' => t('Show only first error message'),
      '2' => t('Show only last error message'),
    ),
    '#title' => t('Show these error messages on validation'),
    '#default_value' => $settings['validate_options']['show_messages'],
    '#description' => t('Warning: Showing only the first or last message only works if the error messages are displayed on top of the form. When they are displayed inline this setting will not be applied.'),
    '#states' => array(
      'visible' => array(
        ':input[name="error_placement[error_placement_default]"]' => array(
          'value' => CLIENTSIDE_VALIDATION_TOP_OF_FORM,
        ),
      ),
    ),
  );

  //Error message settings
  $form['error'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Error message settings'),
    '#weight' => 20,
  );
  $form['error']['prefix'] = array(
    '#title' => t('Field name prefix'),
    '#description' => t('The prefix of the field name in the error messages.'),
    '#type' => 'textfield',
    '#default_value' => $settings['error']['prefix'],
  );
  $form['error']['suffix'] = array(
    '#title' => t('Field name suffix'),
    '#description' => t('The suffix of the field name in the error messages.'),
    '#type' => 'textfield',
    '#default_value' => $settings['error']['suffix'],
  );
  $form['error']['example_image'] = array(
    '#type' => 'item',
    '#title' => t('Example'),
    '#markup' => '<img id="example_image" src="' . base_path() . drupal_get_path('module', 'clientside_validation') . '/errormsg.png" alt="' . t('Error message example') . '" />',
    '#description' => t('Filling in double quotes in both the above fields will give this result.'),
  );
  $form['error']['scrollto_errormessage'] = array(
    '#title' => t('Scroll to error message'),
    '#description' => t('If checked, the page will automatically scroll to the error messages when validation fails.'),
    '#type' => 'checkbox',
    '#default_value' => $settings['error']['scrollto_errormessage'],
  );
  $form['error']['scroll_speed'] = array(
    '#title' => t('Scroll speed'),
    '#description' => t('The scroll speed in milliseconds'),
    '#type' => 'textfield',
    '#default_value' => $settings['error']['scroll_speed'],
    '#states' => array(
      'visible' => array(
        ':input[name="error[scrollto_errormessage]"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );
  $form['error']['error_element'] = array(
    '#title' => t('Error Element'),
    '#description' => t('The HTML element to wrap the errors in. Defaults to label.'),
    '#type' => 'select',
    '#options' => drupal_map_assoc(array(
      'label',
      'span',
      'div',
    )),
    '#default_value' => $settings['error']['error_element'],
  );
  if (module_exists('fapi_validation') && module_exists('fapi')) {
    $form['error']['scroll_speed'] += array(
      '#rules' => array(
        'numeric',
      ),
    );
  }

  //Error Placement
  $form['error_placement'] = array(
    '#type' => 'fieldset',
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#title' => t('Error message placement'),
    '#weight' => 30,
  );
  $form['error_placement']['error_placement_default'] = array(
    '#type' => 'select',
    '#title' => t('Default location'),
    '#description' => t('Default location to show the error messages.
      If you choose "jQuery selector", provide a valid jQuery selector in the appropriate textfield.
      If you choose "Custom function", provide the name of the custom javascript function.
      The function needs to be declared on the Drupal.clientsideValidation object prototype. You can find
      further details in the clientside_validation.api.js file.
      This function will be given to !errorPlacement.', array(
      '!errorPlacement' => l(t('the errorPlacement option of jQuery.validate()'), 'http://docs.jquery.com/Plugins/Validation/validate#options'),
    )),
    '#options' => array(
      CLIENTSIDE_VALIDATION_JQUERY_SELECTOR => t('jQuery selector'),
      CLIENTSIDE_VALIDATION_TOP_OF_FORM => t('Top of form'),
      CLIENTSIDE_VALIDATION_BEFORE_LABEL => t('Before label'),
      CLIENTSIDE_VALIDATION_AFTER_LABEL => t('After label'),
      CLIENTSIDE_VALIDATION_BEFORE_INPUT => t('Before input'),
      CLIENTSIDE_VALIDATION_AFTER_INPUT => t('After input'),
      CLIENTSIDE_VALIDATION_TOP_OF_FIRST_FORM => t('Top of first form'),
      CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION => t('Custom function'),
    ),
    '#default_value' => $settings['error_placement']['error_placement_default'],
  );
  $form['error_placement']['jquery_selector'] = array(
    '#type' => 'textfield',
    '#title' => t('jQuery selector'),
    '#description' => t('Enter a jQuery selector here if you selected "jQuery selector" in the previous step.
                      The error messages will be shown in this div if it exists.
                      If it doesn\'t, error messages will be shown above the first form on the page.'),
    '#default_value' => $settings['error_placement']['jquery_selector'],
    '#states' => array(
      'visible' => array(
        ':input[name="error_placement[error_placement_default]"]' => array(
          'value' => CLIENTSIDE_VALIDATION_JQUERY_SELECTOR,
        ),
      ),
    ),
  );
  $form['error_placement']['custom_error_function'] = array(
    '#type' => 'textfield',
    '#title' => t('Custom function name'),
    '#description' => t('If you selected "Custom function" in the previous step, provide the name of the custom javascript function.
      This function will be given to !errorPlacement', array(
      '!errorPlacement' => l(t('the errorPlacement option of jQuery.validate()'), 'http://docs.jquery.com/Plugins/Validation/validate#options'),
    )),
    '#default_value' => $settings['error_placement']['custom_error_function'],
    '#states' => array(
      'visible' => array(
        ':input[name="error_placement[error_placement_default]"]' => array(
          'value' => CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION,
        ),
      ),
    ),
  );

  //Hidden fields and tabs
  $form['include_hidden'] = array(
    '#type' => 'fieldset',
    '#title' => t('Hidden fields and tabs'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
    '#weight' => 40,
  );
  $form['include_hidden']['include_hidden_fields'] = array(
    '#type' => 'checkbox',
    '#default_value' => $settings['include_hidden']['include_hidden_fields'],
    '#title' => t('Validate hidden fields'),
    '#description' => t('Validate hidden fields as well.'),
    '#weight' => 2,
  );
  $form['include_hidden']['validate_tabs'] = array(
    '#type' => 'checkbox',
    '#default_value' => $settings['include_hidden']['validate_tabs'],
    '#title' => t('Validate all tabs'),
    '#description' => t('If you check this, all tabs will be validated (both vertical and horizontal) on submit
                      (including the hidden ones). Warning: if there are other hidden
                      fields (e.g. conditional fields), those will be validated too.'),
    '#weight' => 3,
  );
  if ($cvs_type == 'default') {
    $form['include_hidden']['include_hidden'] = array(
      '#type' => 'textarea',
      '#default_value' => $settings['include_hidden']['include_hidden'],
      '#title' => t("Don't ignore hidden fields on the following forms"),
      '#description' => t('You can specify form IDs (one per line) of forms that should have hidden elements validated.'),
      '#weight' => 1,
    );
  }
  else {
    $fieldsets = array(
      'validate_options' => 'Validate options',
      'error' => 'Error message settings',
      'error_placement' => 'Error message placement',
      'include_hidden' => 'Hidden fields and vertical tabs',
    );
    foreach ($fieldsets as $key => $fieldset) {
      $statefield = ':input[name="' . $key . '_override_default"]';
      $form[$key]['#states'] = array(
        'visible' => array(
          $statefield => array(
            'checked' => TRUE,
          ),
        ),
      );
      $form[$key . '_override_default'] = array(
        '#type' => 'checkbox',
        '#title' => t('Override default options for %fieldset', array(
          '%fieldset' => $fieldset,
        )),
        '#description' => t('Check this to override the default validate options'),
        '#default_value' => isset($settings[$key . '_override_default']) && $settings[$key . '_override_default'],
        '#weight' => $form[$key]['#weight'] - 1,
      );
    }
  }
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
    '#weight' => 50,
  );
  return $form;
}