Form API

Using State API in Drupal Forms (Custom Conditional Fields)

The following is a snippet on how to enable fields to appear and/or disappear based on the state of other fields.

<?php

/**
* Modifications for including State API functionalities. The idea is to hide the fields field_referme_industry
* and field_referme_major_skills, based on the value of another field.
* The functionality can be implemented during the after_build phase
*/
function jc_form_alter(&$form, &$form_state, $form_id) {
//drupal_set_message("Form Id is: $form_id ");
if($form_id == 'referme_node_form') {

How to enable Ajax for CCK Fields in Drupal 7

The idea is to modify another CCK field based on selection of an item via a select list in another field.

Let us consider a content type called article containing 2 additional fields: article_test_entity - which is an entity reference that displays node titles of all nodes and article_test_entity_type - which is a select list that contains Content Type names list with its key as the actual machine name of the content type. The idea is to restrict the list of node titles displayed on article_test_entity based on the content type selected via article_test_entity_type.

<?php

/*

Saving Form Selection using Form API

During custom form submission we would want to remember the previous selection. There are many ways to do that, which includes using Sessions and variable_set to set the default value but this is an overkill if you wanted to simply preserve/remember the selection in the next page or after submission. The best way is to use $form_state['storage'] in your submit handler to hoard values between steps.

Consider an example:
<?php
function custom_form_submit($form, &$form_state) {
$start_date = $form_state['values']['startdate'];
$end_date = $form_state['values']['enddate'];