Handling reset button


#1

I am writing a plugin to handle a sequential form, that is after pressing the Submit button, the user is redirected to the next page in a sequence, which in turn may contain form data.

Everything works as planned, except I now want a mechanism to cancel the sequence by using a ‘reset’ button.

FYI, the draft plugin is at https://github.com/finanalyst/grav-plugin-sequential-form . Once I get the reset button working, I will offer to republish it.

I cannot yet work out how to detect which button of a form was pressed (submit or reset). That is, in onFormProcessed, how can I detect the button that was pressed?

The aim would be to redirect to the first page in the sequence, stop form propagation, and reset the form data.

I am thinking that it will be necessary to add javascript to the form so that hitting reset sends a redirect to the first page.

I have studied form.php, anithubd the templates, and I can see button.task, but there is no documentation about what button.task does.

Also, I can see that blueprints are important. I was wondering whether I could use a rule to cancel data. However, there is no information about blueprints.


#2

[SOLVED]
After some research about html forms and looking at the form twig templates, my solution is

  • Include a new button into the page form yaml,
  • set the type to ‘submit’
  • set the attribute ‘task’ of the button to a new value, eg,. ‘mybutton’, viz.
form:
  buttons:
    - type: submit
      value: Click here for another action
      task: mybutton
  • include in OnFormProcessed the lines:
if (isset($_POST['task']) && $_POST['task'] == 'mybutton' ) { do_stuff(); }