Posted by smiletrl on November 21, 2012 at 11:15am
hi
I want to show a hidden form element if one check fails in validation function.
This use case is user has submited sensitive data and that hidden element(a checkbox) will unhide to make sure he really wants to submit the data.
After user checks that checkbox, this validation will
pass. Pretty straightforward, but the challenge is how to make form knows this form has been validated
and failed. So it knows it needs to unhide that checkbox.
I tried adding variable in $form_state, but all elements inside $form_state will be ingored when
validation fails.
Any help will be appreciated. Thank you!
Comments
Problem solved. In validation
Problem solved.
In validation handler, add this:
<?php$form_state['rebuild'] = TRUE;
$form_state['flag'] = 1;
?>
Doesn't work
This doesn't work if you set a form_error on the validation step. Your code only works if the submit handler also triggers.
--author="jax "
Well, the idea is when the
Well, the idea is when the validate fails, the form will rebuild and show the original hidden form element(flaged by $form_state['flag']). It's also possible to do a
drupal_set_message()to remind user what happened, in form's build function then.Yeah, form_error will terminate the form process. But with
$form_state['rebuild'] = TRUE;, form will remember variables inside$form_state, then our problem gets solved. It doesn't have to do with submit handler. You may see the idea in http://drupal.org/project/domain_ip and http://drupal.org/project/redirect.You could add a confirmation
You could add a confirmation step to your form. See http://api.drupal.org/api/drupal/modules!system!system.module/function/confirm_form/7
Well, if use a confirm_form,
Well, if use a confirm_form, then this involves a multi-step form. User submits something in the first form, and then browser shows user the second form(i.e., confirm form). This could be a way to solve this issue.
However, I guess
$form_state['rebuild'] = TRUE;could be a more generic way to do this though:)