Pages

Thursday, February 17, 2011

Drupal Module - Form State

The info file - form_explore.info
name = Form Module Learning
description = This form provide a way for learning
package = Exploration
version = 1.0
core = 7.x


The module file - form_explore.module
<?php

/**
 * Implements hook_menu()
 */
 
function form_explore_menu(){
    $items = array();
   
    $items['explore/main'] = array(
        'title' => 'Form State',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('get_states'),
        'access callback' => TRUE,
    );
   
    return $items;
}

/**
 * form implementation
 */
function get_states($form, &$form_state){
    $form['gender'] = array(
        '#type' => 'radios',
        '#title' => t('What is your gender'),
        '#options' => drupal_map_assoc(array( t('Male'), t('Female') )),
    );
   
    $form['wife'] = array(
        '#type' => 'textfield',
        '#title' => t('Your Wife'),
        // states
        '#states' => array(
            'visible' => array(
                ':input[name="gender"]' => array('value' => t('Male')),
            ),
        ),   
    );
   
    $form['husband'] = array(
        '#type' => 'textfield',
        '#title' => t('Your Husband'),
        // states
        '#states' => array(
            'visible' => array(
                ':input[name="gender"]' => array('value' => t('Female')),
            ),
        ),
    );
   
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => t('Send'),
    );
   
    return $form;
}

No comments:

Post a Comment