Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function spower_menu() {
- $items = array();
- $items['spower'] = array(
- 'title' => t('My form'),
- 'page callback' => 'spower_form',
- 'access arguments' => array('access content'),
- 'description' => t('My form'),
- 'type' => MENU_CALLBACK,
- );
- return $items;
- }
- function spower_form() {
- return drupal_get_form('spower_my_form');
- }
- function spower_my_form($form, &$form_state, $no_js_use = FALSE) {
- $form['name'] = array(
- '#type' => 'fieldset',
- '#title' => t('Name'),
- '#collapsible' => FALSE,
- '#collapsed' => FALSE,
- );
- $form['#tree'] = TRUE;
- $form['names_fieldset'] = array(
- '#type' => 'fieldset',
- '#title' => t('People coming to the picnic'),
- // Set up the wrapper so that AJAX will be able to replace the fieldset.
- '#prefix' => '<div id="names-fieldset-wrapper">',
- '#suffix' => '</div>',
- );
- $form['name']['first'] = array(
- '#type' => 'textfield',
- '#title' => t('First name'),
- '#required' => TRUE,
- '#default_value' => "First name",
- '#description' => "Please enter your first name.",
- '#size' => 20,
- '#maxlength' => 20,
- );
- $form['name']['last'] = array(
- '#type' => 'textfield',
- '#title' => t('Last name'),
- '#required' => TRUE,
- );
- $form['name']['year_of_birth'] = array(
- '#type' => 'textfield',
- '#title' => "Year of birth",
- '#description' => 'Format is "YYYY"',
- );
- // Build the fieldset with the proper number of names. We'll use
- // $form_state['num_names'] to determine the number of textfields to build.
- if (empty($form_state['num_names'])) {
- $form_state['num_names'] = 1;
- }
- for ($i = 0; $i < $form_state['num_names']; $i++) {
- $form['names_fieldset']['name'][$i] = array(
- '#type' => 'textfield',
- '#title' => t('Name'),
- );
- }
- $form['names_fieldset']['add_name'] = array(
- '#type' => 'submit',
- '#value' => t('Add one more'),
- '#submit' => array('spower_add_more_add_one'),
- // See the examples in ajax_example.module for more details on the
- // properties of #ajax.
- '#ajax' => array(
- 'callback' => 'spower_add_more_callback',
- 'wrapper' => 'names-fieldset-wrapper',
- ),
- );
- if ($form_state['num_names'] > 1) {
- $form['names_fieldset']['remove_name'] = array(
- '#type' => 'submit',
- '#value' => t('Remove one'),
- '#submit' => array('spower_add_more_remove_one'),
- '#ajax' => array(
- 'callback' => 'spower_add_more_callback',
- 'wrapper' => 'names-fieldset-wrapper',
- ),
- );
- }
- $form['submit'] = array(
- '#type' => 'submit',
- '#value' => 'Submit',
- '#ajax' => array(
- 'callback' => 'spower_ajax_callback',
- 'wrapper' => 'tabs-3',
- ),
- );
- return $form;
- }
- function spower_theme($existing, $type, $theme, $path) {
- $base = array(
- 'render element' => 'form',
- );
- return array(
- 'spower_my_form' => $base + array(
- 'template' => 'spower',
- ),
- );
- }
- function spower_my_form_validate($form, &$form_state) {
- $year_of_birth = $form_state['values']['year_of_birth'];
- if ($year_of_birth && ($year_of_birth < 1900 || $year_of_birth > 2000)) {
- form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
- }
- }
- // Adds a submit handler/function to our form to send a successful
- // completion message to the screen.
- function spower_my_form_submit($form, &$form_state) {
- drupal_set_message(t('The form has been submitted.'));
- };
- function spower_ajax_callback($form, $form_state){
- return "<div id=\"tabs-3\">hello world</div>";
- }
- function spower_add_more_callback($form, $form_state) {
- return $form['names_fieldset'];
- }
- /**
- * Submit handler for the "add-one-more" button.
- *
- * Increments the max counter and causes a rebuild.
- */
- function spower_add_more_add_one($form, &$form_state) {
- $form_state['num_names']++;
- $form_state['rebuild'] = TRUE;
- }
- /**
- * Submit handler for the "remove one" button.
- *
- * Decrements the max counter and causes a form rebuild.
- */
- function spower_add_more_remove_one($form, &$form_state) {
- if ($form_state['num_names'] > 1) {
- $form_state['num_names']--;
- }
- $form_state['rebuild'] = TRUE;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement