ahah_example_simple_validation.inc

  1. examples
    1. 6 ahah_example/ahah_example_simple_validation.inc

Demonstrate validation of a textfield using AHAH. This approach allows 'live' validation of a field which degrades gracefully when JavaScript is not available.

Functions & methods

NameDescription
ahah_example_simple_validation@file Demonstrate validation of a textfield using AHAH. This approach allows 'live' validation of a field which degrades gracefully when JavaScript is not available.
ahah_example_simple_validation_callbackCallback for autocheckboxes. Process the form with the number of checkboxes we want to provide.
ahah_example_simple_validation_submitSubmit 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.
ahah_example_simple_validation_validateValidation function for the form. Requires that two words be entered.

File

ahah_example/ahah_example_simple_validation.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Demonstrate validation of a textfield using AHAH.
  5. * This approach allows 'live' validation of a field which degrades gracefully
  6. * when JavaScript is not available.
  7. */
  8. function ahah_example_simple_validation(&$form_state) {
  9. // Wrap the dynamic element in a fieldset, as some themes render the throbber
  10. // wrong and things appear to jump around on the screen. This can also be
  11. // fixed with some CSS.
  12. $form['wrapper_fieldset'] = array(
  13. '#type' => 'fieldset',
  14. );
  15. $form['wrapper_fieldset']['two_words_required'] = array(
  16. '#title' => t('At least two words are required in this textfield'),
  17. '#type' => 'textfield',
  18. '#default_value' => !empty($form_state['values']['two_words_required']) ? $form_state['values']['two_words_required'] : '',
  19. '#ahah' => array(
  20. 'path' => 'examples/ahah_example/simple_validation/callback',
  21. 'wrapper' => 'two_words_required_wrapper',
  22. ),
  23. '#prefix' => '<div id="two_words_required_wrapper">',
  24. '#suffix' => '</div>',
  25. );
  26. $form['submit'] = array(
  27. '#type' => 'submit',
  28. '#value' => t('Click Me'),
  29. );
  30. return $form;
  31. }
  32. /**
  33. * Validation function for the form. Requires that two words be entered.
  34. */
  35. function ahah_example_simple_validation_validate(&$form, &$form_state) {
  36. $words = explode(' ', $form_state['values']['two_words_required']);
  37. if (count($words) < 2) {
  38. form_set_error('two_words_required', t('You have to enter at least two words'));
  39. }
  40. }
  41. /**
  42. * Submit handler for autocheckboxes.
  43. * Gets called even when our select is active, so we use the
  44. * $form_state to determine whether the submit handler should actually do
  45. * anything.
  46. */
  47. function ahah_example_simple_validation_submit($form, &$form_state) {
  48. if (!empty($form_state['ahah_submission'])) {
  49. return;
  50. }
  51. // Continue to handle submit processing.
  52. }
  53. /**
  54. * Callback for autocheckboxes. Process the form with the number of checkboxes
  55. * we want to provide.
  56. */
  57. function ahah_example_simple_validation_callback() {
  58. $form = ahah_example_callback_helper();
  59. // This section prepares the actual output that will be returned to the
  60. // browser.
  61. $selected_portion = $form['wrapper_fieldset']['two_words_required'];
  62. // To avoid doubling-up the wrapper, we have to remove it here.
  63. unset($selected_portion['#prefix'], $selected_portion['#suffix']);
  64. // Now render and output.
  65. $output = drupal_render($selected_portion);
  66. // Include (optionally) the results of any drupal_set_message() calls that
  67. // may have occurred.
  68. $output .= theme('status_messages');
  69. // Output the results and exit.
  70. drupal_json(array('status' => TRUE, 'data' => $output));
  71. exit();
  72. }
Login or register to post comments