2

I have created a simple form that displays a textfield and a submit button. The text field is defined:

$form['upload_file'] = array(
    '#type' => 'textfield',
    '#title' => t('File to read')
  );

When I enter "test" in the textfield and select the submit button, the form moves on to the Validate function which has the following code:

$answer = $form_state['values']['upload_file'];
  if (!$answer) {
    form_set_error('upload_file', "Please enter a file name: ");
  }

And I get the error message indicting that $answer is empty. Do I have the wrong array location for the stored textfield text? Or is there something else I need to do to place the values in the form ito the values array?

  • how are you defining/declaring your validate function? – Jimajamma Dec 15 '11 at 18:52
3

The easiest way to debug form related issues I have found is with the devel module

drush dl devel && drush en devel -y

This module provides a whole host of helpful functions. E.g. in your case you could use this in your validation function:

dsm($form_state['values']);

This would print a lovely formatted array of every value in the form. From there you can drill down to the required value.

I believe you should be checking for:

if (empty(trim($form_state['values']['field_upload_file']))) {
  form_set_error('upload_file', "Please enter a file name: ");
}

Or alternatively, if you are just checking to see if the textfield simply has a value in it, then the better approach would be to use the FAPI and declare:

$form['upload_file'] = array(
  '#type' => 'textfield',
  '#title' => t('File to read'),
  '#required' => TRUE,
);
  • 1
    I am forever throwing dsm()s and/or the equivalent drupal_set_message()s and print_r()s into things to solve my "off by one" errors like this, too. – Jimajamma Dec 15 '11 at 19:00
  • The value of a textfield is contained in $form_state['values']['field_upload_file']. – kiamlaluno Dec 15 '11 at 19:01
  • The documentation examples at Drupal do not show the 'field_' prefix. Is this new in v7? – Ashlar Dec 15 '11 at 19:57
  • I tried using ['values']['field_upload_file'] and it failed. However, ['values']['upload_file'] does have a value and I confirmed the name using DSM. – Ashlar Dec 15 '11 at 20:26
  • 1
    wiifm: ++++1 for describing dsm. I Have Devel, but did not know about dsm, its just what I needed! – Ashlar Dec 15 '11 at 20:27
1

If you compare the code of search_form() with the code of search_form_validate(), you will notice the following code:

  $form['basic'] = array(
    '#type' => 'container',
    '#attributes' => array('class' => array('container-inline')),
  );
  $form['basic']['keys'] = array(
    '#type' => 'textfield', 
    '#title' => $prompt, 
    '#default_value' => $keys, 
    '#size' => $prompt ? 40 : 20, 
    '#maxlength' => 255,
  );
function search_form_validate($form, &$form_state) {
  form_set_value($form['basic']['processed_keys'], trim($form_state['values']['keys']), $form_state);
}

In your case, the validation handler access the submitted value with $form_state['values']['upload_file'].
If the code of your validation handler is not executed, then Drupal is not invoking it. It could be the validation handler doesn't have the right name, or another module is overwriting the validation handler with another one.

To notice the the default validation handler, FORM_ID_validate() (replace "FORM_ID" with the form ID), is used when other validation handlers are not set. The code of drupal_prepare_form() used to set the validation handler is the following one:

  if (!isset($form['#validate'])) {
    // Ensure that modules can rely on #validate being set.
    $form['#validate'] = array();
    // Check for a handler specific to $form_id.
    if (function_exists($form_id . '_validate')) {
      $form['#validate'][] = $form_id . '_validate';
    }
    // …
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.