Form API
https://api.drupal.org/api/examples/form_example!form_example.module/group/form_example/7
---------------------------------------- form_example.info -----------------------------------------
core = "7.x"
description = "An example module used to learn module development and forms creation."
name = "Form Example Module"
-------------------------------------- form_example.module --------------------------------
<?php
function form_example_menu() {
$items = array();
$items['examples/form-example'] = array( //this creates a URL that will call this form at "examples/form-example"
'title' => 'Example Form', //page title
'description' => 'A form to mess around with.',
'page callback' => 'drupal_get_form', //this is the function that will be called when the page is accessed. for a form, use drupal_get_form
'page arguments' => array('form_example_form'), //put the name of the form here
'access callback' => TRUE
);
return $items;
}
function form_example_form($form, &$form_state) {
$form['Overview'] = array(
'#markup' => t('This is just a markup'),
'#prefix' => '<p>',
'#suffix' => '</p>',
);
$form['check_box'] = array(
'#title' => t('Just a check box'),
'#description' => t('when enabled .....'),
'#type' => 'checkbox',
'#default_value' => 1,
);
$form['default_center'] = array(
'#title' => t('map center'),
'#description' => t('map setter'),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => FALSE,
);
$form['default_center']['inner_elements'] = array(
'#title' => t('latitude'),
'#description' => t('latitude value'),
'#type' => 'textfield',
'#default_value' => 45.225,
'#required' => TRUE,
);
$options = range(0,20,1);
$options[0] = t('0 - furthers');
$options[20] = t('20 - closest');
$form['default_zoomer'] = array(
'#title' => t('Gmap zoomer'),
'#description' => t('Zoome google maps'),
'#type' => 'select',
'#options' => $options,
'#default_value' => 8,
'#required' => TRUE,
);
$form['price'] = array(
'#type' => 'textfield', //you can find a list of available types in the form api
'#title' => 'What is Your Price?',
'#size' => 10,
'#maxlength' => 10,
'#required' => TRUE, //make this field required
);
$form['submit_button'] = array(
'#type' => 'submit',
'#value' => t('Click Here!'),
);
return $form;
}
function form_example_form_validate($form, &$form_state) {
if (!($form_state['values']['price'] > 0)){
form_set_error('price', t('Price must be a positive number.'));
}
}
function form_example_form_submit($form, &$form_state) {
}
No comments:
Post a Comment