Programmatically Validate Form

Specs

Version
Drupal 7
Tools
Form API
Created
28 Feb 2016

Summary

Drupal no proporciona un método para la validación de la presentación de un formulario mediante programación. Fuera de la caja, la única forma de ejecutar la validación de un formulario es para enviar el formulario real en la interfaz de usuario. Este fragmento simula la validación de un formulario y las capturas de sus errores. Esto se hace mediante:

  1. La construcción de la forma $ y $ form_state matriz
  2. Ejecución de estos a través de funciones de validación personalizados
  3. La captura de los errores utilizando form_get_errors ()
  4. Restablecimiento de errores utilizando 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.