How do I add a reset button to a form? I have a form with a CKEditor and other fields, plus hidden fields. How do I reset the form so all fields are empty?
1 Answer
I was able to get this to work.
Within the buildForm() method, part of the form array contains this for the reset button:
$form['rmdrs_form_reset_button'] = array( '#type' => 'submit', '#value' => t('Reset'), '#limit_validation_errors' => TRUE, '#submit' => ['::reminderFormReset'], ); Also in that Form class I have method for handling the reset:
public function reminderFormReset($form, &$form_state) { $form_state->setRebuild(FALSE); } The downside is that it is, IMHO, a glorified page reload. The button acts like a submit button, but instead calls the reminderFormReset() method in the same class.
- Is limit_validation_errors anything to do with the solution or may that be removed?mona lisa– mona lisa2023-11-29 21:24:49 +00:00Commented Nov 29, 2023 at 21:24
- 1'#limit_validation_errors' => TRUE, was added to stop the action of validation that normally happens when submit is called. Without it validation of HTML5 required fields would take place. At least that is my limited understanding of it.Cy Jobes– Cy Jobes2023-11-29 22:25:23 +00:00Commented Nov 29, 2023 at 22:25
$form_state->setRebuild()in the submit handler.#ajaxon the submit button, and$form_state->setValuein the submit handler to clear the relevant values (with the ->setRebuild too)