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 } }
February 5, 2009 at 10:10 am
Dude, thanks for this! I banged my head on Drupal’s form error handling for a couple of hours before finding this!
May 19, 2009 at 7:31 am
Thanks.. It helped me a lot π
July 3, 2009 at 3:59 pm
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’.
July 4, 2009 at 1:29 pm
@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
August 13, 2011 at 6:18 am
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!
January 3, 2012 at 12:36 pm
It helped me!
Thanks
November 3, 2012 at 8:59 am
Great Man !! you saved my time. Wish you all the best.
December 13, 2012 at 5:11 pm
Thanks for it! And for the girl photo of the add π
April 17, 2013 at 1:20 am
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.
April 18, 2013 at 4:01 am
Cheers from Moortown π
May 22, 2013 at 9:14 am
Appreciate it from Dungannon π
May 28, 2013 at 9:15 pm
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!
August 3, 2013 at 12:12 am
http://www.anyhow-shirt.comγγγ γ»γ―γ·γΌ
August 9, 2013 at 4:55 pm
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.
January 18, 2014 at 7:44 pm
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
January 7, 2018 at 10:12 pm
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
August 18, 2018 at 10:10 pm
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