drupal_form_submit
- Versions
- 7
drupal_form_submit($form_id, &$form_state)
Retrieves a form using a form_id, populates it with $form_state['values'], processes it, and returns any validation errors encountered. This function is the programmatic counterpart to drupal_get_form().
<?php
// register a new user
$form_state = array();
$form_state['values']['name'] = 'robo-user';
$form_state['values']['mail'] = 'robouser@example.com';
$form_state['values']['pass']['pass1'] = 'password';
$form_state['values']['pass']['pass2'] = 'password';
$form_state['values']['op'] = t('Create new account');
drupal_form_submit('user_register_form', $form_state);
// Create a new node
$form_state = array();
module_load_include('inc', 'node', 'node.pages');
$node = array('type' => 'story');
$form_state['values']['title'] = 'My node';
$form_state['values']['body'] = 'This is the body text!';
$form_state['values']['name'] = 'robo-user';
$form_state['values']['op'] = t('Save');
drupal_form_submit('story_node_form', $form_state, (object)$node);
?>
Parameters
$form_id The unique string identifying the desired form. If a function with that name exists, it is called to build the form array. Modules that need to generate the same form (or very similar forms) using different $form_ids can implement hook_forms(), which maps different $form_id values to the proper form constructor function. Examples may be found in node_forms(), search_forms(), and user_forms().
$form_state A keyed array containing the current state of the form. Most important is the $form_state['values'] collection, a tree of data used to simulate the incoming $_POST information from a user's form submission.
... Any additional arguments are passed on to the functions called by drupal_form_submit(), including the unique form constructor function. For example, the node_edit form requires that a node object be passed in here when it is called. For example:
Related topics
Code
includes/form.inc, line 445
<?php
function drupal_form_submit($form_id, &$form_state) {
if (!isset($form_state['build_info']['args'])) {
$args = func_get_args();
array_shift($args);
array_shift($args);
$form_state['build_info']['args'] = $args;
}
$form = drupal_retrieve_form($form_id, $form_state);
$form_state['input'] = $form_state['values'];
$form_state['programmed'] = TRUE;
// Programmed forms are always submitted.
$form_state['submitted'] = TRUE;
// Merge in default values.
$form_state += form_state_defaults();
drupal_prepare_form($form_id, $form, $form_state);
drupal_process_form($form_id, $form, $form_state);
}
?>Login or register to post comments 