4

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?

1 Answer 1

7

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.

8
  • I can't actually find runInputPost. In the latest version of yesod-form it's in Yesod.Form.Input. However, the latest version on hackage of Yesod is 0.8.2.1, and it depends on an older version of yesod-form, namely 0.1.*. Do I need to update to a newer version of Yesod to use this method? If so, when will the new version be on Hackage?
    – chrisdb
    Aug 22, 2011 at 18:21
  • Sorry, I thought you were on the beta version, my mistake. You can just use the equivalent functions from the current yesod-form, namely runFormPost' and stringInput. We're hopefully releasing the 0.9 release candidate tomorrow. Aug 22, 2011 at 19:34
  • I've downloaded it, but I'm having problems. yesod-json depends on something called 'aeson-native' which isn't on hackage. Is it just aeson?
    – chrisdb
    Aug 22, 2011 at 20:07
  • I also get other errors if I change 'aeson-native' to 'aeson'. For example, now I'm getting 'Yesod/Auth/Kerberos.hs:100:20: Not in scope: `html'' when compiling Yesod.Auth. I've looked at the modules it imports and can't find the 'html' it's referring to either. Are any of these build issues known?
    – chrisdb
    Aug 22, 2011 at 21:32
  • 1
    It isn't actually a boolean field. The field is a text field containing (IIRC) the name of the button. A value is only present if the button was pressed. Therefore, you do something like iopt textField "preview" and then if the value is Just something do one thing, and if it's Nothing do another.
    – chrisdb
    Sep 22, 2011 at 16:38

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.