开发者

Yesod form with multiple buttons

开发者 https://www.devze.com 2023-03-28 02:43 出处:网络
I have a Yesod form for editing the contents of some static pages which are written using markdown (processed using Pandoc)开发者_如何学Python. I want to have two buttons - a \'preview\' button which

I have a Yesod form for editing the contents of some static pages which are written using markdown (processed using Pandoc)开发者_如何学Python. 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消