2

I am having trouble clearing the form input upon failed validation of the form.

For example the form validation function would throw an error if there is a there is any radio button element in the form, of which there is two groups, left unchecked. If the user select the first group, but does not select for the second group, an error is thrown. I would like to know how I can clear the form input such that the previously selected radio button in group 1 is cleared.

This is being done with Drupal 7.

apaderno
  • 28,547
  • 16
  • 75
  • 90
Kevin
  • 21
  • 2
  • I believe this question has already been answered here: http://stackoverflow.com/questions/9235164/change-form-data-after-submit-in-drupal – Dewayne Jan 21 '13 at 21:35
  • This is a different question - in the other question the form is passing validation and getting to the submit function. This question is about how to change the form data from hook_validate so the submit function is not called... – Felix Eve Apr 11 '13 at 10:11

1 Answers1

-1

You'll need to add a validate function that cleans the form_state...

function my_module_form_FORM_ID_alter(&$form, &$form_state, $form_id) {
  $form['#validate'][] = 'my_module_clear_values_validate';
}

function my_module_clear_values_validate($form, &$form_state) {
  if ($form_state['submitted']) {
    $form_state['values']['field_my_field'][LANGUAGE_NONE][0]['value'] = 'WHATEVS';
  }
}
doublejosh
  • 5,548
  • 4
  • 39
  • 45
  • Actually is does Carl. This is standard stuff, I use it all over many sites to manage form processing. I'm guessing you didn't clear cache before testing, or didn't bother outputting the form to find out what fields you needed to empty. You'll probably want to use `dsm($form)` via Devel to inspect the form. – doublejosh Mar 13 '15 at 21:51
  • 1
    Confirming Carl's comment - this does not work. Your solution works to *change* the input value (to "WHATEVS"), but it does not work to *clear* the value. – aaronbauman Feb 03 '16 at 15:38