Programmatically Validate Form

Specs

Version
Drupal 7
Tools
Form API
Created
28 Feb 2016

Summary

Drupal bietet keine Methode zur Validierung der Vorlage eines programmatisch Form. Out of the box, die einzige Möglichkeit, die Validierung auf einem Formular auszuführen ist, die tatsächliche Form in der Benutzeroberfläche zu unterbreiten. Dieser Code-Schnipsel simuliert die Validierung eines Formulars und fängt seine Fehler. Es tut dies durch:

  1. Bau der $ form und form_state $ array
  2. Laufen diese durch benutzerdefinierte Validierungsfunktionen 
  3. Fangen Fehler mit form_get_errors ()
  4. Rücksetzen von Störungen mit form_set_error (NULL, NULL, TRUE);

Code

1. Programmatically Validate a Form

MODULE_NAME.module
Put this in one of your module files. View MODULE_NAME_example_use_callback() for example usage.
          
/*
 * Simulates validation on a form using defined validation callbacks. It does this by running the validation functions
 * on the $form and $form_state, catching the errors using form_get_errors(), and flushing out the errors using form_set_error()
 * again.
 * @param array $form
 *  The form structure
 * @param array $form_state
 *   The form data
 * @param array $validation_callbacks
 *   An array of validation callbacks to call on the form
 * @return array $errors
 *   An array of errors, or NULL if no errors.
 */
function MODULE_NAME_simulate_form_validation(&$form, &$form_state, $validation_callbacks) {

  foreach ($validation_callbacks as $validation_callback) {
    $validation_callback($form, $form_state);
  }
  
  // Get errors from the form validation. form_get_errors() retrives them from a static variable
  // set in form_set_error(). form_set_error() would have been called in the validation callbacks
  $errors = form_get_errors();
  
  // reset the static cache of form_set_error() so the errors won't be picked up on the next call of this function
  $reset_errors = form_set_error(NULL, NULL, TRUE);
  
  // flush out the messages that were set by form_set_error()
  $messages = drupal_get_messages();
  
  return $errors;
}

/*
 * This example will call validation function MODULE_NAME_article_node_form_validate() on the article node form and catch its errors
 */
function MODULE_NAME_example_use_callback() {
  
  module_load_include('inc', 'node', 'node.pages');
  $node = node_load(1);
  
  $form = drupal_get_form('article_node_form', $node);
  $form_state['values'] = (array) $node; // cast the node object to an array so it can be used in $form_state['values']
  
  $validation_callbacks = array('MODULE_NAME_article_node_form_validate'); // your custom validation functions
  
  $errors = MODULE_NAME_simulate_form_validation($form, $form_state, $validation_callbacks);

  if (!empty($errors)) {
    return FALSE;
  }
  
  return TRUE;
}

/*
 * This is just an example validation function. Use your own validation function based on required logic.
 * @param array $form
 *  The form structure
 * @param array $form_state
 *   The form data
 */
function MODULE_NAME_article_node_form_validate(&$form, &$form_state) {
  
  // This is just an example form validation function.
  if (is_numeric($form_state['values']['title'])) {
    form_set_error('title', t('Please do not enter a number for a title.'));
  }
  
}
          
          

Comments

Placeholder: I'll extend node comments with ajax and other functionality here.