Posts Tagged ‘Form’
From Programming in Drupal Way
Display the Form
drupal_get_form($form_id)
Form Callbacks
There has 3 callbacks: form callback, validate callback, submit callback.
xxx_form($form_state)
xxx_form_validate($form, &$form_state)
xxx_form_submit($form, &$form_state)
Form theme: theme_xxx_form($form)
Functions can be used in form theme:
drupal_render(&$elements)
Example:
$output .= drupal_render($form['name']);
……
$output .= drupal_render($form);
Hooks
hook_forms()
This hook is for map form_ids to builder functions.
hook_form_alter(&$form, $form_state, $form_id)
This hook allow any form (even those in core) can be altered in almost any way imaginable–elements can be removed, added, and rearranged
PHP Form Handing
1. Use $_SERVER['PHP_SELF'] as a form action
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>”>
2. Test for form submission with a hidden element
if (array_key_exists('_submit_check', $_POST)) {
/* ... do something with the form parameters ... */
}
...
<input type="hidden" name="_submit_check" value="1"/>
3. Divide your form handling into three parts: showing, validating, and processing
if (array_key_exists('_submit_check',$_POST)) {
// If validate_form() returns errors, pass them to show_form()
if ($form_errors = validate_form()) {
show_form($form_errors);
} else {
// The submitted data is valid, so process it
process_form();
}
} else {
// The form wasn't submitted, so display
show_form();
}