home | career | drupal | java | mac | mysql | perl | php | scala | uml | unix

Drupal example source code file (ahah_example_simple_validation.inc)

This example Drupal source code file (ahah_example_simple_validation.inc) is included in the DevDaily.com "Drupal Source Code Warehouse" project. The intent of this project is to help you "Learn Drupal by Example".

PHP - Drupal tags/keywords

array, form, form_state, function, if, least, output, php, selected_portion, suffix, two, two_words_required_wrapper, type, words

The ahah_example_simple_validation.inc Drupal example source code

<?php

/**
 * @file
 * Demonstrate validation of a textfield using AHAH.
 * This approach allows 'live' validation of a field which degrades gracefully
 * when JavaScript is not available.
 */
function ahah_example_simple_validation(&$form_state) {

	// Wrap the dynamic element in a fieldset, as some themes render the throbber
	// wrong and things appear to jump around on the screen. This can also be
	// fixed with some CSS.
	$form['wrapper_fieldset'] = array(
	 '#type' => 'fieldset',
	);
  $form['wrapper_fieldset']['two_words_required'] = array(
    '#title' => t('At least two words are required in this textfield'),
    '#type' => 'textfield',
    '#default_value' => !empty($form_state['values']['two_words_required']) ? $form_state['values']['two_words_required'] : '',
    '#ahah' => array(
      'path' => 'examples/ahah_example/simple_validation/callback',
      'wrapper' => 'two_words_required_wrapper',
    ),
    '#prefix' => '<div id="two_words_required_wrapper">',
    '#suffix' => '</div>',
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Click Me'),
  );

  return $form;
}

/**
 * Validation function for the form. Requires that two words be entered.
 */
function ahah_example_simple_validation_validate(&$form, &$form_state) {
	$words = explode(' ', $form_state['values']['two_words_required']);
	if (count($words) < 2) {
		form_set_error('two_words_required', t('You have to enter at least two words'));
	}
}

/**
 * Submit handler for autocheckboxes.
 * Gets called even when our select is active, so we use the
 * $form_state to determine whether the submit handler should actually do
 * anything.
 */
function ahah_example_simple_validation_submit($form, &$form_state) {
  if (!empty($form_state['ahah_submission'])) {
    return;
  }

  // Continue to handle submit processing.
}

/**
 * Callback for autocheckboxes. Process the form with the number of checkboxes
 * we want to provide.
 */
function ahah_example_simple_validation_callback() {
  $form = ahah_example_callback_helper();

  // This section prepares the actual output that will be returned to the
  // browser.
  $selected_portion = $form['wrapper_fieldset']['two_words_required'];

  // To avoid doubling-up the wrapper, we have to remove it here.
  unset($selected_portion['#prefix'], $selected_portion['#suffix']);

  // Now render and output.
  $output = drupal_render($selected_portion);

  // Include (optionally) the results of any drupal_set_message() calls that
  // may have occurred.
  $output .= theme('status_messages');

  // Output the results and exit.
  print drupal_json(array('status' => TRUE, 'data' => $output));
  exit();
}

Other Drupal examples (source code examples)

Here is a short list of links related to this Drupal ahah_example_simple_validation.inc source code file:

new blog posts

"Drupal" is a registered trademark of Dries Buytaert.

my drupal tutorials and examples  

Copyright 1998-2016 Alvin Alexander, alvinalexander.com
All Rights Reserved.

Beginning in 2016, a portion of the proceeds from pages under the '/drupal-code-examples/' URI will be donated to charity.