Skip to content
Grav 2.1 is out: every page speaks Markdown. Read the announcement →

Handling reset button

Started by Richard Hainsworth 8 years ago · 1 replies · 1077 views
8 years ago

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.

8 years ago

[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.
    YAML
    form:
    buttons:
    - type: submit
      value: Click here for another action
      task: mybutton
    
  • include in OnFormProcessed the lines:
    PHP
    if (isset($_POST['task']) && $_POST['task'] == 'mybutton' ) { do_stuff(); }
    

Suggested topics

Topic Participants Replies Views Activity
Forms & Blueprints · by FrViPofm, 4 days ago
5 86 4 days ago
Forms & Blueprints · by Ton Haarmans, 6 years ago
13 2113 7 months ago
Forms & Blueprints · by Hugo Oliveira, 8 months ago
0 597 8 months ago
Forms & Blueprints · by Flachy Joe, 9 months ago
9 690 9 months ago
Forms & Blueprints · by Augustus, 10 months ago
7 737 10 months ago