Yesod形式与多个按钮 [英] Yesod form with multiple buttons
问题描述
我有一个Yesod表单,用于编辑使用markdown(使用Pandoc处理)写入的一些静态页面的内容。我想要有两个按钮 - 一个预览按钮,用于处理标记并在表单下显示结果;以及一个提交按钮,用于将内容保存到数据库。
>与Yesod做这个最简单的方法是什么? Yesod书中的所有表单示例都只有一个按钮。我已经看过了暴露的函数/ api,但我即使添加了多个不同名称和/或值的提交按钮到表单中,我也无法弄清楚如何让Yesod告诉我哪个按下了。
任何人都可以给我一个在Yesod中有多个按钮的简单示例,它会触发不同的操作?
您可以使用输入表单函数来获取原始值,并在各个按钮上显式设置一个名称属性。在HTML中是这样的:
< input type =submitname =previewvalue =Preview> ;
在Haskell代码中:
res< - runFormPost ...
isPreview< - runInputPost $ iopt boolFieldpreview
if isPreview then ... else ...
对不起,如果这不能检查,我现在还没有正常的开发系统。但我认为这是正确的一般方法。
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?
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.
这篇关于Yesod形式与多个按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!