'. t('Validation API offers the ability to create a validation (as a PHP script or a regular-expression script) to validate any field on the site.') .'
'; $output .= ''. t('Validators are the scripts that are run to test if a given field is valid. Fields are representatives of any form field on the site. You will want to assign validators to fields to make the field be validated before submission of the form.') .'
'; return $output; case 'admin/build/validation_api': $output = ''. t('Validators are the scripts that are run to test if a given field is valid. Fields are representatives of any form field on the site. You will want to assign validators to fields to make the field be validated before submission of the form.') .'
'; return $output; case 'admin/build/validation_api/validators': $output = ''. t('This page contains all of the validators in the database. You can add/edit your own validators, import validators from other modules, import validators with the code from an export, export code for a validator for importing elsewhere, and clone validators. The update link under Ops (You may or may not see this link) is for imported validators from other modules that are now different from the one in the database.'); return $output; case 'admin/build/validation_api/validators/add': $output = '
'. t('The rule is the heart and soul of validating. It is the script that is run to validate the value passed to this validator. This script can either be PHP or RegEx (determined by the \'Type\' select box).') .'
'; $output .= ''. t('If you choose PHP, you need to use $value as the value being validated and (if applicable) use $arguments[x] as the argument in the rule with x representing the delta of the argument. If you choose RegEx, the value will automatically be used and (if applicable) you would need to use %arguments[x] as the argument with the x representing the delta of the argument. (e.g. Rule: "/^((.)|(\s)){%arguments[0],%arguments[1]}$/").') .'
'; $output .= ''. t('Messages may use placeholders to further describe the error. You need to use %field as the placeholder for the name of the field and use %arguments[x] as the placeholder for the argument with x representing the delta of the argument.') .'
'; return $output; case 'admin/build/validation_api/validators/edit/%': $output = ''. t('The rule is the heart and soul of validating. It is the script that is run to validate the value passed to this validator. This script can either be PHP or RegEx (determined by the \'Type\' select box).') .'
'; $output .= ''. t('If you choose PHP, you need to use $value as the value being validated and (if applicable) use $arguments[x] as the argument in the rule with x representing the delta of the argument. If you choose RegEx, the value will automatically be used and (if applicable) you would need to use %arguments[x] as the argument with the x representing the delta of the argument. (e.g. Rule: "/^((.)|(\s)){%arguments[0],%arguments[1]}$/").') .'
'; $output .= ''. t('Messages may use placeholders to further describe the error. You need to use %field as the placeholder for the name of the field and use %arguments[x] as the placeholder for the argument with x representing the delta of the argument.') .'
'; return $output; case 'admin/build/validation_api/validators/import': $output = ''. t('You can check any validator(s) you want imported from modules, and/or you can put code from an code export into the text-area.') .'
'; return $output; case 'admin/build/validation_api/validators/export': $output = ''. t('Check which validators you would like to export. After exporting, copy the code in the text-area and paste into the import text-area to import these validators on other set-ups.') .'
'; return $output; case 'admin/build/validation_api/validators/clone': $output = ''. t('Check which validators you would like to clone. This will create a copy of that validator and place it in the database with a number added to the end of the name for uniqueness.') .'
'; return $output; case 'admin/build/validation_api/fields': $output = ''. t('This page contains all of the fields in the database that have been associated with a validator. You can add/edit your own fields, but I recommend you go to the field you want to add a validator to and click the \'add a validator\' link below it for adding fields.'); return $output; case 'admin/build/validation_api/fields/add': $output = '
'. t('On the first step: For Form ID, use the form_id where the field resides. The Field Name is the name of the field you want to validate. I recommend getting this data by going to the page the form resides on and using the \'add a validator\' link directly below the field in question.') .'
'; $output .= ''. t('On the second step: There may be arguments available for you to fill in, just follow the guidelines as these were set by the validator you selected. You can override the message form the validator, as well.') .'
'; return $output; case 'admin/build/validation_api/fields/edit/%': $output = ''. t('There may be arguments available for you to fill in, just follow the guidelines as these were set by the validator you selected. You can override the message form the validator, as well.') .'
'; return $output; } } /** * Implementation of hook_form_alter(). */ function validation_api_form_alter(&$form, $form_state, $form_id) { $form = _validation_api_form_fields($form, $form_id, _validation_api_fields($form_id), variable_get('excluded_validation_fields', array('item', 'value', 'fieldset', 'hidden', 'button', 'submit', 'image_button', 'markup', 'token'))); } /** * Implementation of hook_menu(). */ function validation_api_menu() { $items['admin/build/validation_api'] = array( 'title' => 'Validation API', 'description' => 'Create and manage validators and a form field\'s relationship with a validator.', 'page callback' => 'validation_api_admin', 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', ); $items['admin/build/validation_api/settings'] = array( 'title' => 'Settings', 'description' => 'Modify settings for the Validation API system.', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_settings_form'), 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', 'weight' => 2, ); $items['admin/build/validation_api/validators'] = array( 'title' => 'Validators', 'description' => 'Create and manage validator rules, messages, etc.', 'page callback' => 'validation_api_admin_validators', 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', ); $items['admin/build/validation_api/validators/list'] = array( 'title' => 'List', 'type' => MENU_DEFAULT_LOCAL_TASK ); $items['admin/build/validation_api/validators/add'] = array( 'title' => 'Add', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_validators_form'), 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', 'type' => MENU_LOCAL_TASK, 'weight' => 1, ); $items['admin/build/validation_api/validators/import'] = array( 'title' => 'Import', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_import_form'), 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', 'type' => MENU_LOCAL_TASK, 'weight' => 4, ); $items['admin/build/validation_api/validators/export'] = array( 'title' => 'Export', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_export_form'), 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', 'type' => MENU_LOCAL_TASK, 'weight' => 5, ); $items['admin/build/validation_api/validators/clone'] = array( 'title' => 'Clone', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_clone_form'), 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', 'type' => MENU_LOCAL_TASK, 'weight' => 6, ); $items['admin/build/validation_api/validators/edit/%'] = array( 'title' => 'Edit', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_validators_form', 5), 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', 'type' => MENU_CALLBACK, ); $items['admin/build/validation_api/validators/update/%'] = array( 'title' => 'Update', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_update_form', 5), 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', 'type' => MENU_CALLBACK, ); $items['admin/build/validation_api/validators/disconnect/%'] = array( 'title' => 'Disconnect', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_disconnect_form', 5), 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', 'type' => MENU_CALLBACK, ); $items['admin/build/validation_api/validators/delete/%'] = array( 'title' => 'Delete', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_validators_delete', 5), 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', 'type' => MENU_CALLBACK, ); $items['admin/build/validation_api/fields'] = array( 'title' => 'Fields', 'description' => 'Create and manage a form field\'s validator, argument, etc.', 'page callback' => 'validation_api_admin_fields', 'access callback' => 'user_access', 'access arguments' => array('administer validation fields'), 'file' => 'validation_api.admin.inc', 'weight' => 1, ); $items['admin/build/validation_api/fields/list'] = array( 'title' => 'List', 'type' => MENU_DEFAULT_LOCAL_TASK ); $items['admin/build/validation_api/fields/add'] = array( 'title' => 'Add', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_fields_form'), 'access callback' => 'user_access', 'access arguments' => array('administer validation fields'), 'file' => 'validation_api.admin.inc', 'type' => MENU_LOCAL_TASK, ); $items['admin/build/validation_api/fields/edit/%'] = array( 'title' => 'Edit', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_fields_form', 5), 'access callback' => 'user_access', 'access arguments' => array('administer validation fields'), 'file' => 'validation_api.admin.inc', 'type' => MENU_CALLBACK, ); $items['admin/build/validation_api/fields/delete/%'] = array( 'title' => 'Delete', 'page callback' => 'drupal_get_form', 'page arguments' => array('validation_api_admin_fields_delete', 5), 'access callback' => 'user_access', 'access arguments' => array('administer validators'), 'file' => 'validation_api.admin.inc', 'type' => MENU_CALLBACK, ); return $items; } /** * Implementation of hook_perm(). */ function validation_api_perm() { return array('administer validation fields', 'administer validators', 'administer php validators'); } /** * Implementation of hook_theme(). */ function validation_api_theme($existing, $type, $theme, $path) { return array( 'validation_api_admin_import' => array( 'arguments' => array('form' => array()), 'file' => 'validation_api.admin.inc', ), ); } /** * Helper function to incorporate the elements that need validation into the structured form array. * Also, adds a 'Add a validator to...' link to the #suffix of core text-like elements. * * @param $elements * Structured section of a form array. * @param $form_id * Form ID. * @param $validation_api_fields * Structured array for fields from this form_id * @param $excluded_fields * An array of fields that are not allowed to be validated on * @param $tree * String for the current place in an FAPI tree * * @return * Returns the element with possible changes. */ function _validation_api_form_fields($elements, $form_id, $validation_api_fields, $excluded_fields, $tree = '') { foreach (element_children($elements) as $key) { if (is_array($elements[$key])) { // new_tree sets a new place in a tree with the current key. $new_tree = $tree; // If there is anything in new_tree then add a brackets around the key. if (drupal_strlen($new_tree) > 0) { $new_tree .= '['. $key .']'; } // Else, if #tree is TRUE then set the key to the tree. elseif (isset($elements[$key]['#tree']) && $elements[$key]['#tree']) { $new_tree = $key; } // Check if there is a #type assigned to the key and make sure it is not an excluded field. if (isset($elements[$key]['#type']) && !in_array($elements[$key]['#type'], $excluded_fields)) { $current_tree = (drupal_strlen($new_tree) > 0 ? $new_tree : $key); // Links to add to the suffix of this element. $links = array(l(t('Add a validator to !name', array('!name' => $current_tree)), 'admin/build/validation_api/fields/add', array('query' => array('form_id' => $form_id, 'form_field' => $current_tree)))); // If the current tree has a validator associated, then attach the validator function to validate it. if (isset($validation_api_fields[$current_tree])) { $elements[$key]['#element_validate'][] = '_validation_api_validator'; } // If there are brackets in the current tree, then we need to add more links and check for validators for each placeholder. if (preg_match('/\[([a-zA-Z0-9_\-]*)\]/', $current_tree)) { $base = drupal_substr($current_tree, 0, strpos($current_tree, '[')); $current_tree_array = array(); preg_match_all('/\[([a-zA-Z0-9_\-]*)\]/', $current_tree, $current_tree_array); $current_tree_array = $current_tree_array[1]; $count = count($current_tree_array); // Need to run through every single placeholder option for the tree. for ($i = 0; $i < $count; $i++) { for ($ii = 1; $ii <= $count - $i; $ii++) { $adjusted_array = $current_tree_array; for ($iii = 1; $iii <= $i; $iii++) { $adjusted_array[$count - $iii] = '%'; } $adjusted_array[$count - $ii - $i] = '%'; $full_name = $base .'['. implode('][', $adjusted_array) .']'; $links[] = l(t('Add a validator to !name', array('!name' => $full_name)), 'admin/build/validation_api/fields/add', array('query' => array('form_id' => $form_id, 'form_field' => $full_name))); if (isset($validation_api_fields[$full_name]) && (!isset($elements[$key]['#element_validate']) || !in_array('_validation_api_validator', $elements[$key]['#element_validate']))) { $elements[$key]['#element_validate'][] = '_validation_api_validator'; } } } } // Only add the links to the suffix, if they are enabled and the user has access to administer them. if (variable_get('validation_api_links', 1) && user_access('administer validation fields')) { drupal_add_css(drupal_get_path('module', 'validation_api') .'/validation_api.css'); if (count($links) > 1) { $elements[$key]['#suffix'] = (isset($elements[$key]['#suffix']) ? $elements[$key]['#suffix'] : '') .'