0

I have a Drupal form wherein someone inputs information, and I need to do a database query to check if it is valid before submitting. I would like to either have a button the user can click to check the validity (or have it be done automatically after the user leaves that field), and then display some information about his selection.
I know I can use hook_form_submit to review a form when it is submitted and then stop the process if there are any errors, but I would like the user to be able to confirm they have selected the correct thing before submitting the form.

0

2 Answers 2

2

I haven't personally tried this module, but it might be what you're looking for:

http://drupal.org/project/ajax

If you're just looking for a way to do real-time lookups (e.g. entering the book barcode and getting the title), you can also use Drupal's autocomplete feature, but it will require you to write your own autocomplete function to handle the database lookups.

Sign up to request clarification or add additional context in comments.

Comments

0

Take a look at: Basic form with validate handler. You really just need to add a function similar to mymodule_myform_validate($form, &$form_state) { ... }. From the linked page:

"This adds a new form field and a way to validate it with a validate function, also referred to as a validate handler."

<?php
function my_module_menu() {
  $items = array();
  $items['my_module/form'] = array(
    'title' => t('My form'),
    'page callback' => 'my_module_form',
    'access arguments' => array('access content'),
    'description' => t('My form'),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

function my_module_form() {
  return drupal_get_form('my_module_my_form');
}

function my_module_my_form($form_state) {
  $form['name'] = array(
    '#type' => 'fieldset',
    '#title' => t('Name'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $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,
  );

  // New form field added to permit entry of year of birth.
  // The data entered into this field will be validated with
  // the default validation function.
  $form['year_of_birth'] = array(
    '#type' => 'textfield',
    '#title' => "Year of birth",
    '#description' => 'Format is "YYYY"',
  ); 

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

// This adds a handler/function to validate the data entered into the 
// "year of birth" field to make sure it's between the values of 1900 
// and 2000. If not, it displays an error. The value report is // $form_state['values'] (see http&#58;//drupal.org/node/144132#form-state).
//
// Notice the name of the function. It is simply the name of the form 
// followed by '_validate'. This is the default validation function.
function my_module_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.');
  }
}
?>

1 Comment

So if I am reading this correctly, the user still has to click submit before their input gets validated. I am looking for a way for them to check if the input is valid before they click submit (eg. they input a books barcode, and then it will tell them the name of the book, before they submit). That way they don't accidentally input the wrong thing, even though it is a "valid" entry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.