Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yesod form with multiple buttons

Tags:

haskell

yesod

I have a Yesod form for editing the contents of some static pages which are written using markdown (processed using Pandoc). I want to have two buttons - a 'preview' button which processes the markup and displays the result underneath the form, and a 'submit' button which saves the contents to the database.

What is the simplest way to do this with Yesod? All the form examples in the Yesod book have exactly one button. I've looked at the exposed functions / api, but I even if I add more than one submit button with different names and/or values to the form I can't figure out how to get Yesod to tell me which one was pressed.

Can anyone give me a simple example of a form with more than one button in Yesod, which trigger different actions?

like image 233
chrisdb Avatar asked Feb 23 '23 06:02

chrisdb


1 Answers

You can just use the Input form functions to get the raw values, and explicitly set a name attribute on the various buttons. Something like this in the HTML:

<input type="submit" name="preview" value="Preview">

And in the Haskell code:

res <- runFormPost ...
isPreview <- runInputPost $ iopt boolField "preview"
if isPreview then ... else ...

Sorry if this doesn't typecheck, I don't have my normal development system right now. But I think this is the right general approach.

like image 137
Michael Snoyman Avatar answered Mar 04 '23 03:03

Michael Snoyman