I'm sure there's a standard Drupal way of doing this, but I've tried a couple of the statements that I've found on blogs and stuff, and I can't get a form to restore the values submitted by the users.
For example:
- form default value: "enter a long word"
- user submits: "foo"
- validation sets: "word not long enough"
- form displayed on page with value: "enter a long word"
- expected value: "foo"
So far, in the hook_nameform_validate
function, I've tried setting form_state['rebuild']
and form_state['redirect']
to FALSE
(I've seen both listed in blogs as the right answer).
Perhaps unsurprisingly to anyone who knows the correct answer, neither works individually, nor do they both work together.
EDIT as requested, here is some actual code to show what I'm doing (wrong). You're correct that the question was probably too general to be of value without any actual code on display. This sample is still "abstracted" - there's closer to a dozen fields, but the effect is the same.
In this case, with these functions (and the others, like theme etc...) when a user submits their email address, if the validation returns an error, the form tells them "bad user" then clears the email field (and all others). Ideally the form should repopulate all fields with any typing they've already done.
function sampleform_nameform($form, &$form_state) {
$d = sampleform_defaults();
$form = array(
'#method' => 'post',
'#prefix' => $d['form_prefix'].'<div class="sample">',
'#suffix' => '</div>'.$d['form_suffix'],
'your_email' => array(
'#value' => $d['your_email'],
'#type' => 'textfield',
'#field_suffix' => '<span class="required">*</span>',
'#required' => TRUE,
'#attributes' => array('class' => array('standardInput'),
'default' => $d['your_email'])
),
'sampleform_submit' => array(
'#prefix' => $d['clear'],
'#type' => 'submit',
'#value' => $d['submit'],
'#attributes' => array('class' => array('buttonInput', 'submitInput')),
'#executes_submit_callback' => true
)
);
return $form;
}
function sampleform_nameform_validate($form, &$form_state){
$d = sampleform_defaults();
if (!valid_email_address($form_state['input']['your_email'])) {
form_set_error('your_email', $d['e_email_v']);
}
}
It's very possible that one of the two answers below has fixed the issue, and I've not had the chance to read them yet, so with a few more minutes, maybe we'l find the answer is already here.
:)
EDIT 2
kiamlaluno has shown us that while I was using #value
in my form-builder function, the correct nondestructive setting in creation of a form is #default_value
If I look back to my original reference point, Pro Drupal 7 Development the form API examples in chapter 11 give the instruction to use #value
on page 247 (where you build your first form).
They also use MODULENAME_nameform
as the hook, while it appears to be more correct to use MODULENAME_form
(according to the way drupal works, it's easier to find further documentation if you search for _form
rather than nameform
).
Anyways, hopefully this saves someone else some time. Someone should probably tell Todd Tomlinson and John K VanDyk that they've got an error in their book.
:)