Community Documentation

Input validation

  1. drupal
    1. 4.6 common.inc
    2. 4.7 common.inc
    3. 5 common.inc
    4. 6 common.inc
    5. 7 common.inc
    6. 8 common.inc

Functions to validate user input.

Functions & methods

NameDescription
base_pathReturns the base URL path of the Drupal installation. At the very least, this will always default to /.
check_file
check_urlPrepare a URL for use in an HTML attribute. Strips harmful protocols.
drupal_add_cssAdds a CSS file to the stylesheet queue.
drupal_add_jsAdd a JavaScript file, setting or inline code to the page.
drupal_add_linkAdd a <link> tag to the page's HEAD.
drupal_attributesFormat an attribute string to insert in a tag.
drupal_build_css_cacheAggregate and optimize CSS files, putting them in the files directory.
drupal_clear_css_cacheDelete all cached CSS files.
drupal_cloneProvide a substitute clone() function for PHP4.
drupal_cron_cleanupShutdown function for cron cleanup.
drupal_cron_runExecutes a cron run when called
drupal_evalEvaluate a string of PHP code.
drupal_get_cssReturns a themed representation of all stylesheets that should be attached to the page. It loads the CSS in order, with 'core' CSS first, then 'module' CSS, then 'theme' CSS files. This ensures proper cascading of styles…
drupal_get_jsReturns a themed presentation of all JavaScript code for the current page. References to JavaScript files are placed in a certain order: first, all 'core' files, then all 'module' and finally all 'theme' JavaScript…
drupal_get_pathReturns the path to a system item (module, theme, etc.).
drupal_get_private_keyEnsure the private key variable used to generate tokens is set.
drupal_get_tokenGenerate a token based on $value, the current user session and private key.
drupal_mailSend an e-mail message, using Drupal variables and default settings. More information in the PHP function reference for mail()
drupal_map_assocForm an associative array from a linear array.
drupal_page_footerPerform end-of-request tasks.
drupal_renderRenders HTML given a structured array tree. Recursively iterates over each of the array elements, generating HTML code. This function is usually called from within a another function, like drupal_get_form() or node_view().
drupal_system_listingReturns an array of files objects of the given type from the site-wide directory (i.e. modules/), the all-sites directory (i.e. sites/all/modules/), the profiles directory, and site-specific directory (i.e. sites/somesite/modules/). The returned array…
drupal_to_jsConverts a PHP variable into its Javascript equivalent.
drupal_urlencodeWrapper around urlencode() which avoids Apache quirks.
drupal_valid_tokenValidate a token based on $value, the current user session and private key.
element_childCheck if the key is a child.
element_childrenGet keys of a structured array tree element that are not properties (i.e., do not begin with '#').
element_propertiesGet properties of a structured array element. Properties begin with '#'.
element_propertyCheck if the key is a property.
flood_is_allowedCheck if the current visitor (hostname/IP) is allowed to proceed with the specified event. The user is allowed to proceed if he did not trigger the specified event more than $threshold times per hour.
flood_register_eventRegister an event for the current visitor (hostname/IP) to the flood control mechanism.
format_dateFormat a date with the given configured format or a custom format string.
format_intervalFormat a time interval with the requested granularity.
format_pluralFormat a string containing a count of items.
format_rss_channelFormats an RSS channel.
format_rss_itemFormat a single RSS item.
format_sizeGenerate a string representation for the given byte count.
format_xml_elementsFormat XML elements.
lFormat an internal Drupal link.
page_set_cacheStore the current page in the cache.
parse_sizeParse a given byte count.
urlGenerate a URL from a Drupal menu path. Will also pass-through existing URLs.
valid_email_addressVerify the syntax of the given e-mail address.
valid_urlVerify the syntax of the given URL.
xmlrpcPerforms one or more XML-RPC request(s).
_drupal_add_jsHelper function for drupal_add_js().
_drupal_bootstrap_full
_element_sortFunction used by uasort in drupal_render() to sort structured arrays by weight.

Groups

NameDescription
FormattingFunctions to format numbers, strings, dates, etc.

File

includes/common.inc, line 893
Common functions that many Drupal modules will need to reference.

Comments

Additional validations

I spent some time mulling over all this stuff and wasn't able to find anything on customized validations. I presume this is because php functions like preg_match work so well. Based on the assumption that a drupal version of preg_match doesn't exist, here's how you would make sure that a given string contains ONLY, for example, numbers and letters:

//validate function called after submission of my form
function mymenu_validate($form, &$form_state) {

  /*
  suppose the user wrote something into the "userinput" field,
  and we want to ensure it only contains numbers and letters.
  To do this we use the expression /^[a-zA-Z0-9]+$/
  */
  if (!(preg_match("/^[a-zA-Z0-9]+$/", $form_state['values']['userinput']))) {
    //the user input contained characters other than letters and numbers
  }
  else {
    //the user input contained only letters and numbers
  }

}

If you're as confused as I was about the expressions, check this out: http://www.hotscripts.com/forums/php/21391-help-preg_match.html

Login or register to post comments