User login
Navigation
Recent blog posts
- Ubuntu mirrors up and improved!
- Google-jstemplate: Iterate object with unknown properties
- nginx https proxypass for php apps
- sugarcrm & memcache: Doing it Wrong
- subversion and https in Ubuntu Karmic
- Drupal: Views block delta converted to md5 hash
- Ubuntu Server install requires PAE
- Installing Drupal from command line
- Drupal: Handling form field weight through CCK
- minify javascript using Google Closure Compiler
angch's location
angch twitter
- angch: Heading to Brunei. Business class for a change.
- angch: tc qdisc htb activated on the mirror, and mirror reactivated. Hope nothing melts now.
- angch: Hmmm, tbf doesn't work as advertised. htb does. #qos #linux
- angch: @yoonkit me thinks #lucid is a much better tag than #lynx
- angch: Great, you broke the dc's net and they disconnected us. Running to tpm from shah alam
Drupal Form API button handler

kamal — Sat, 02/08/2008 - 11:57
Drupal Form API, which first appeared on 4.7 and now on the 3rd revision is always an overlooked features when someone considering Drupal as framework to build web applications. One thing that I really like about Form API is it abstract the logic of form handling, with us the developer only need to implement certain hooks in order to process the form submission.
The typical flow of most Form library is something like this:-
:-
// create new form object
$form = new Form('some parameter');
//the logic
if $form->is_valid() {
do some processing
}
In Drupal Form API, the logic of handling the form is handled by the API it self. For example, to validate the form we just implement the hook function, something like
function form_api_test_validate($form, $form_state) {
//validation code here
}
function form_api_test_submit($form, $form_state) {
//processing code here
}
Another thing about Drupal API is we never care where to submit the form. In fact, there's no option to specify the form action attribute ! Form is only identified using the form_id and that's all we need to care about. And for those who care about security, Drupal automatically add a token to all forms build through the API to protect it from some kind of cross site request forgery (CSRF) so it save up sometimes on our part to implement that measures.
With Drupal 6, Form API comes with another improvent that I really like it a lot. It's now possible to specify a custom validate and submit handler for each submit button in our form. So we can have Save button handled by let say function form_api_test_save() while a Cancel button would be handled by function form_api_test_cancel(). Awesome !
There's a lot more cool features but I guess that enough for some introductory blog post. Expect more writing about Drupal Form API in the upcoming days ;) 