Creating and managing forms
In this section, let's learn how to create new forms and manage them.
Creating a new form
In Drupal 7, a form was a function that returned an array containing nested elements from the Form API. Then, you would add appropriate _validate and _submit functions to handle verifying the submission and to handle the completed form in a simple case that would look as follows:
function my_module_my_form($form, &$form_state)
$form['first'] = array(
'#type' => 'textfield',
'#title' => t('First name'),
'#required' => TRUE,
);
$form['last'] = array(
'#type' => 'textfield',
'#title' => t('Last name'),
'#required' => TRUE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
return $form;
}
function my_module_my_form_validate($form, &$form_state) {
// Handle form validation
}
function my_module_my_form_submit($form, &$form_state) {
// Handle form...