Professional Documents
Culture Documents
drupalform.module
<?php
/**
*@file
*Thisisthemainmodulefile.
*/
/**
*Implementshook_help().
*/
functiondrupalform_help($path,$arg){
if($path=='admin/help#rupalform'){
$output='<h3>'.t('About').'</h3>';
$output.='<p>'.t('Thedrupalformmoduleshowshowtocreateformsusin
return$output;
}
}
With the above code you should be able to see your module in the available module list and should be able to
enable it as shown below. If these steps seem mysterious or complicated, please see my last tutorial for a
goodDrupal Module introduction.
Once we have done this we will add a menu callback usinghook_menuand create a form on the page. The code
for that is as follows
/**
*Implementationofhook_menu().
*/
functiondrupalform_menu(){
$items['drupalform/form1']=array(
'type'=>MENU_CALLBACK,
'accessarguments'=>array('accesscontent'),
'pagecallback'=>'drupal_get_form',
'pagearguments'=>array('drupalform_form1'));
return$items;
}
functiondrupalform_form1(){
$form=array();
$form['name']=array(
'#type'=>'textfield',
'#title'=>t('Enteryourname'),
'#description'=>t('Yourfirstnamegoeshere')
);
$form['last_name']=array(
'#type'=>'textfield',
'#title'=>t('EnteryourLastname'),
'#description'=>t('YourLastnamegoeshere')
);
$form['email']=array(
'#type'=>'textfield',
'#title'=>t('Enteryouremail'),
'#description'=>t('Youremailgoeshere')
);
$form['country']=array(
'#type'=>'select',
'#title'=>t('Selectyourcountry'),
'#options'=>array('USA','UK','France','Japan')
);
$form['submit']=array(
'#type'=>'submit',
'#value'=>t('Submit')
);
return$form;
}
functiondrupalform_form1_validate($form,$form_state){
if(empty($form_state['values']['name']))
form_set_error('name','Namecannotbeempty');
elseif(empty($form_state['values']['last_name']))
form_set_error('last_name','Lastnamecannotbeempty');
elseif(filter_var($form_state['values']['email'],FILTER_VALIDATE_EMAIL)==
form_set_error('email','Emailisnotvalid');
}
In the above function we check if the name and last_name are not empty and that the email is valid. If that is not
the case we set a form error using the Drupal functionform_set_error. This will display the error to the
user and the form will not be submitted.
If you submit the form by entering an empty name you should see the following error on the screen:
//Dependingonthetypeofformyoucanaddthelogic
//tostorethedetailsoftheform
//byaddingitinaDrupaltable.
//orsendingamailtotheadmin
//Storinginafile
//orpassittosomeotherservice
drupal_set_message("Formhasbeensubmitted");
}
Now if we pass all the valid elds you will get the following message on screen.
functiondrupalform_form1(){
$form=array();
$form['basicdetails']=array(
'#type'=>'fieldset',
'#title'=>t('EnteryourBasicdetailsbelow'),
'#description'=>t('Theseareallmadatory')
);
$form['basicdetails']['name']=array(
'#type'=>'textfield',
'#title'=>t('Enteryourname'),
'#description'=>t('Yourfirstnamegoeshere')
);
$form['basicdetails']['last_name']=array(
'#type'=>'textfield',
'#title'=>t('EnteryourLastname'),
'#description'=>t('YourLastnamegoeshere')
);
$form['basicdetails']['email']=array(
'#type'=>'textfield',
'#title'=>t('Enteryouremail'),
'#description'=>t('Youremailgoeshere')
);
$form['submit']=array(
'#type'=>'submit',
'#value'=>t('Submit')
);
return$form;
}
functiondrupalform_form1(){
$form=array();
$form['basicdetails']=array(
'#type'=>'fieldset',
'#title'=>t('EnteryourBasicdetailsbelow'),
'#description'=>t('Theseareallmadatory')
);
$form['basicdetails']['name']=array(
'#type'=>'textfield',
'#title'=>t('Enteryourname'),
'#description'=>t('Yourfirstnamegoeshere')
);
$form['basicdetails']['last_name']=array(
'#type'=>'textfield',
'#title'=>t('EnteryourLastname'),
'#description'=>t('YourLastnamegoeshere')
);
$form['basicdetails']['email']=array(
'#type'=>'textfield',
'#title'=>t('Enteryouremail'),
'#description'=>t('Youremailgoeshere')
);
$form['addressdetails']=array(
'#type'=>'fieldset',
'#title'=>t('EnteryourAddressdetailsbelow'),
'#description'=>t('Theseareallmadatory')
);
$form['addressdetails']['country']=array(
'#type'=>'select',
'#title'=>t('Selectyourcountry'),
'#options'=>array('USA','UK','France','Japan')
);
$form['addressdetails']['city']=array(
'#type'=>'textfield',
'#title'=>t('Enteryourcity'),
'#description'=>t('Yourcitynamegoeshere')
);
$form['addressdetails']['localaddress']=array(
'#type'=>'textarea',
'#title'=>t('Enteraddress'),
'#description'=>t('YourAddressnamegoeshere')
);
$form['additionaldetails']=array(
'#type'=>'fieldset',
'#title'=>t('Enteryourotherdetailsbelow'),
'#description'=>t('Thesearealloptional')
$form['additionaldetails']['gender']=array(
'#type'=>'radios',
'#title'=>t('Gender'),
'#options'=>array('Male','Female')
);
$form['additionaldetails']['suscribtion']=array(
'#type'=>'checkboxes',
'#title'=>t('Iwanttosubscribefor'),
'#options'=>array('Emailnewsletter','Offervouchers')
);
$form['additionaldetails']['birthdate']=array(
'#type'=>'date',
'#title'=>t('Birthdate'),
);
$form['#attributes']['enctype']='multipart/formdata';
$form['additionaldetails']['picture']=array(
'#type'=>'file',
'#title'=>t('Uploadyourpicture'),
);
$form['submit']=array(
'#type'=>'submit',
'#value'=>t('Submit')
);
return$form;
}
Conclusion
Drupal helps you create and process forms right inside your module. The APIs Drupal offers make it very simple
to make an addition or modication of your form as the forms are arrays and even the validation and submission
happens in a different function. This modular approach keeps the form code in your module clean and easy to
maintain. You also do not need to bother with the HTML details of the form if you use the Drupal form API all
the HTML is auto-generated. Have fun creating your next form in your Drupal module!
If you'd like us to cover a more specic use case or go into more details, or just have feedback, let us know in the