You are here

function token_element_validate_token_context in Token 5

Same name and namespace in other branches
  1. 6 token.module \token_element_validate_token_context()
  2. 7 token.module \token_element_validate_token_context()

Validate a form element that should have tokens in it.

Form elements that want to add this validation should have the #token_types parameter defined.

For example:

$form['my_node_text_element'] = array(
  '#type' => 'textfield',
  '#title' => t('Some text to token-ize that has a node context.'),
  '#default_value' => 'The title of this node is [node:title].',
  '#validate' => array(
    'token_element_validate_token_context' => array(),
  ),
  '#token_types' => array(
    'node',
  ),
);

File

./token.module, line 598
The Token API module.

Code

function token_element_validate_token_context(&$element) {
  $value = isset($element['#value']) ? $element['#value'] : $element['#default_value'];
  $invalid_tokens = token_get_invalid_tokens_by_context($value, $element['#token_types']);
  if ($invalid_tokens) {
    form_error($element, t('The %element-title is using the following invalid tokens: @invalid-tokens.', array(
      '%element-title' => $element['#title'],
      '@invalid-tokens' => implode(', ', $invalid_tokens),
    )));
  }
  return $element;
}