Yesod form with multiple buttons

让人想犯罪 __ 提交于 2019-12-07 08:49:33

问题


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:


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.



来源:https://stackoverflow.com/questions/7051714/yesod-form-with-multiple-buttons

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!