Drupal: Returning an error and keeping field values when form submit fails

May 30, 2008

Handling Drupal forms submission is dead simple. You basically write a three functions: a form generator, a form validator and a form submitter.

The form generator is called everytime the form needs to be built and it can be used to populate fields default values or the values previously entered by the user.

In the form generator you can specify the functions that should be called to validate the form and to submit it (to actually do something with the form values, like adding a new row to a table for instance):

$form[‘#validate’][] = ‘car_edit_validate’;
$form[‘#submit’][] = ‘car_edit_submit’;

Where car_edit_validate is the name of the function that will be called to do the validation and car_edit_submit is the function that will be called after the form validates.

Every time you submit the form the validate function is called first and if the validation does not encounter any errors the submit function is called.

The problem I had is that I wanted to return an error and keep the form values ifΒ  an error occurred in my submit function. By default, after the submit function is called an empty form will be rendered and user entered values will be lost even if you add an error to the form with form_set_error. The error is displayed but this does not prevent the form values from being lost. If you want to keep the form values you must also add this line:

$form_state[‘redirect’] = FALSE;

Example:

function car_edit_submit($form, &$form_state) {
  global $user;
  $car = (object)$form_state['values'];
  $car->uid = $user->uid;
  if(car_save($car)) {
    drupal_set_message(t('car successfully saved')));
    drupal_goto('car/'.$car->cid);
  }Β else {
    form_set_error('form', t('The car could not be saved, please try again'));
    $form_state['redirect'] = FALSE; //To prevent Drupal from cleaning the form
  }
}

17 Responses to “Drupal: Returning an error and keeping field values when form submit fails”

  1. Drupalnoob Says:

    Dude, thanks for this! I banged my head on Drupal’s form error handling for a couple of hours before finding this!

  2. Ram Kumar Says:

    Thanks.. It helped me a lot πŸ™‚

  3. Judson Says:

    Why doesn’t it work with files? I have created a form that contains textfields and files, but When I select a file to upload and click a submit button that takes you to a validation that has ‘$form_state[‘redirect’] = FALSE;’ on it, all form values remain except those that were of type ‘file’.

    • Ramon Guiu Says:

      @Judson
      I think this is the behaviour you usually observed in forms so I don’t think this is a Drupal issue. I am not sure if there is a workaround for this. One way would be to use AJAX to submit the form, that way the contents of the page would not be updated until the form is fully submitted but I don’t know how you do this in Drupal


  4. My husband and i ended up being so relieved when Albert managed to round up his survey through the entire precious recommendations he obtained through the blog. It is now and again perplexing to just always be freely giving information most people have been making money from. We know we have got the website owner to appreciate for this. The main illustrations you have made, the simple web site menu, the friendships you can assist to promote – it’s many awesome, and it is facilitating our son and our family recognize that the theme is enjoyable, and that is pretty essential. Thanks for everything!

  5. Junaid Says:

    It helped me!

    Thanks

  6. Mathiraman Says:

    Great Man !! you saved my time. Wish you all the best.

  7. Jonathan Says:

    Thanks for it! And for the girl photo of the add πŸ˜‰

  8. Blackberry Says:

    The difference between the Blackberry Tab 10. With brand loyalty a vital component in securing large sales, the IP od
    Touch alternative, the blackberry Tab. Is it possible to cut some” fat” out of the window.
    The Samsung Nexus S comes with a $350 early termination fee.
    With this OS, you might be able to do this easily
    with just your fingers. The most critical difference between
    the two batteries is extremely low when it comes
    to real contest, the 1.


  9. Cheers from Moortown πŸ˜‰


  10. Appreciate it from Dungannon πŸ˜‰

  11. pvbob Says:

    I was looking for the below line all over the internet and the instructions found were putting it inn the wrong function.

    $form_state[‘redirect’] = FALSE; //To prevent Drupal from cleaning the form

    Thank you!


  12. If we had opted them in and then asked them 10 questions they would have if they had continued lighting up, according
    to the same shopping center, to 1. The hope is that it’ll catch on with smaller businesses in particular, which will start in June, and expand beyond the current locations. Using the” $20 Off Any Purchase” bed bath beyond discount some tire discount stores are now offering and you can end up buying it for less than half the price. We tend to think in terms of the traffic.

  13. bodyweight Says:

    If you are going for most excellent contents like myself, just go to see
    this web page every day since it offers quality contents, thanks

  14. LastVenus Says:

    I see you don’t monetize your site, don’t waste your traffic,
    you can earn extra bucks every month because you’ve got high quality content.
    If you want to know how to make extra money, search for: Mertiso’s
    tips best adsense alternative

  15. BestShona Says:

    I have noticed you don’t monetize your page, don’t waste your traffic, you can earn additional bucks every month.
    You can use the best adsense alternative for any type of website (they approve all websites), for
    more details simply search in gooogle: boorfe’s tips monetize your website


Leave a comment