Vous êtes sur la page 1sur 15

Drupal Module Development

Creating Forms in Drupal


The primary method of submitting content
over the Web is through HTML forms. While the ubiquity of forms makes life easy for the web user, the dearth of good forms processing tools usually makes form development a joyless chore for programmers. However, forms processing is one area in which Drupal excels

Creating Forms in Drupal


The Forms API (FAPI) provides a
programming interface that takes the pain out of form development (or at least significantly reduces the pain). Using the FAPI, developers can provide a single form definition and let Drupal build and display the form, collect the results, and even validate and escape form data.

Handling Forms with the Forms API (FAPI)

Perhaps the most common but time


consuming aspect of web development is form handling. Typically, a form must be defined and prepared for display. Then, when form data is submitted to the server, various form processing tasks must be conducted to validate, manipulate, and store form data. Drupal has a special API for handling data.

Handling Forms with the Forms API (FAPI)

Forms API (FAPI). FAPI automatically


handles the mundane details of form handling, making it much easier for developers to quickly write forms. Using the Forms API, developers can create a single form definition that is then used by Drupal to perform various aspects of form handling. The same data structure that is used to generate the form is also used to validate the form.

And with a few simple functions, complex


forms can be handled effectively. FAPI stands out as one of the gems in Drupal's crown because of these features. While we will return to some aspects of the API later in the book, if you are looking for an in-depth discussion beyond what this chapter has to offer, the Drupal website has some very good articles.

:The "quickstart" guide to the Forms API:


http://api.drupal.org/api/ file/developer/topics/forms_api.html/6

The Forms API Quick Reference:


http://api.drupal.org/api/file/ developer/topics/forms_api_reference.html/6

Form API changes between Drupal 5 and Drupal 6:


http://drupal.org/ node/144132

The Form Generation API overview:


http://api.drupal.org/api/group/ form_api/6

Sample Code

Sample Code

In its simplest form, the drupal_get_form() function takes a callback function and uses the results of that function to create a form structure and then manage handling of the resulting form. In the above example, it will return an HTMrmatted form. While the drupal_get_form() function only makes use of one argumentthe name of the callback functionit can take more arguments. Any additional arguments will simply be passed on to the functions that it calls

Activity 1- Create Entry Form

Sample Code

); function student($arg1){ $form=array(); $genderarray=array('Male'=>'Male','Female'=>'Female'); $form['studentform']= array( '#type' => 'fieldset', '#title' => t('Adding of Student'), '#collapsible' => TRUE, '#collapsed' => FALSE, ); $form['studentform']['studentid'] = array( '#title' => t('Student ID'), '#size' => 9, '#maxlengh' => 25, '#required'=>TRUE, '#default_value'=>$studid, '#type' =>'textfield',

$form['studentform']['lastname'] = array( '#title'=> t('Lastname'), '#type' =>'textfield', ); return $form; }

Cont.
Objective is to:
Add fields for School Year,Semester,Major, Year Level and College Add student record in the database.

Activity 2-Create Search Form


Objective is to :
Create a form that allows user to filter students per school year and sem Add additional filter by:
Gender Student Major Year Level

Activity 3-Edit Form


Objective is to: Create Edit Form for student record. Allows user to change the student
information.

Drupal Module Development

ICT Center
We are making IT happen

Integrity

Commitment

Teamwork

Vous aimerez peut-être aussi