Form API Tip: Use form_set_value() to change form values during validation

|

Using Drupal's form api can often times make form validation and submission handling simple and effective. Today's tip is about using the form_set_value($form, $value) function to change a form item's value during the validation phase. This is convenient if you've done any manipulation to the submitted data during the validation process and you don't want to have to redo it in the submit process.

For those of you with the Pro Drupal Development book like myself there is an example of how to use this feature of the form api on page 163-165, however some small but important details were left out.

The key to getting this handy feature to work is to define your hook_validate function with a third parameter, I used $form.

The following is obviously not a complete implementation of a form, but the necessary elements can be seen. Notice that I have two fields, one of which is going to be our place holder ('myitem') and the other which actually takes the user input. During validation I process the user input variable and store it in the place holder. This allows me to access the manipulated value in our submit process through the place holder variable.

function myform() {
  $form['input'] = array(
    '#type' => 'textfield',
    '#title' => t('User Input'),
  );
  $form['myitem'] = array(
    '#type' => 'value',
    '#value' => array(),
  );
  return $form;
}

function myform_validate($form_id, $form_values, $form) {
  form_set_value($form['myitem'], strtolower($form_values['input']);
}

function myform_submit($form_id, $form_values) {
  // access our manipulated input at $form_values['myitem']
}

I hope this helps someone else who has ripped out less hair on their head over this than I have. More information can be found somewhere along the way in the lengthy discussion on this topic here: http://drupal.org/node/160160#comment-279107.

Thanks. I had a battle with

Thanks. I had a battle with this just a couple weeks ago. BTW -> I like the example.

Glad to hear. I think I

Glad to hear. I think I also forgot to mention that this is for Drupal 5.x, just in case anyone was wondering.

What is an example for

What is an example for Drupal 6?

In Drupal 6 there are a

In Drupal 6 there are a number of changes that have been made to the form API. Here is a link that covers all of the changes and should hopefully get you in the right direction.

http://api.drupal.org/api/file/developer/topics/forms_api.html/6

http://drupal.org/node/114774

I personally have yet to work with Drupal 6 form API, but will be soon as views 2 is nearing a release candidate.

Great! Thanks for the tip

Great! Thanks for the tip and the pointers.

It's worth mentioning to

It's worth mentioning to anyone doing this for the first time that if they print_r within the validation function itself they will NOT see their form_values change. Put another way - one needs to check to see if your drupal_set_form is working in the submit function NOT the validation function.

Great tip.

Great tip.

more than a great tip for

more than a great tip for me...thanks

I think this blog is using

I think this blog is using version 6...

code is look like hard

code is look like hard

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You may use [inline:xx] tags to display uploaded files or images inline.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.

Upcoming events

  • No upcoming events available