How do I fill a custom form with the values entered from the users before the form was submitted? I have saved the $form_state
values, but when I put them back into $form_state
on loading, the form fields aren't filled.
-
Are you talking about remembering the user values in the next form load?– AgACommented Dec 29, 2011 at 6:53
4 Answers
If you are not redirecting and want to preserve the values in the Form API fields add $form_state['rebuild'] = TRUE;
on the submit handler.
/**
* FORM submit handler.
*/
function FORM_form_submit($node, &$form_state) {
// Rebuild the form and keep the filter values.
$form_state['rebuild'] = TRUE;
}
In Drupal 8 you would use $form_state-> setRebuild();
See public function FormState::setRebuild for more detail.
Custom submit handlers in Drupal 8 are dealt with differently — here is some useful discussion on Drupal 8 submit handlers.
-
This should be the accepted answer (for Drupal 7 at least). Commented Mar 15, 2019 at 12:01
I think you want to put them in $form['element_name']['#default_value']
not $form_state
. What you're describing sounds odd, though. Why are you adding a user's information back into a form after they already entered it?
-
This is for scenario where user entered data was invalid and he has to fix his mistakes. Drupal default behaviour is to render empty form again when it would be more userfriendly to fill form with submitted values. #default_value works, but right now it means i have to manually save data and then put it back into form elements. I hoped there was some sort drupal way for doing it for me or at least making it easier.– ValdarsCommented Oct 24, 2011 at 6:44
-
if you call form_set_error() correctly, user-entered values will remain- even the invalid field input.– AKSCommented Sep 8, 2012 at 4:19
Values entered in a form should be validated in a form validation handler. When the validation handler finds values that are invalid, it should call form_error() or form_set_error(); in that case, the form would be presented back to the users with the values they entered. The only form field that is reset is the password form field, for which presenting the previously entered value would not have any sense, since the users would just see a string made of "•" (or the character that replaces the input used by the browser).
The purpose of form submission handlers is not to validate the entered values, but to save them, or using them to create the required output.
When form submit fails you must set $form_state['redirect']
to false
. By default after submit form is reloaded and form fields cleared out. But when redirect is set to false form will not be redirected and form fields will remain filled.
-
This does not work if I add another line with this in the submit handler: $form_state['storage']['confirm'] = true;– AgACommented Dec 29, 2011 at 6:38
-
If I can set storage along with redirect then it'd resolve my thread: drupal.stackexchange.com/questions/18333/…– AgACommented Dec 29, 2011 at 6:47
-
In short how do I remember the fact in the next form reload that the reload is due to redirect being set to false in submit handler last time?– AgACommented Dec 29, 2011 at 6:52