Yesod 表单有多个按钮
我有一个 Yesod 表单,用于编辑一些使用 markdown 编写的静态页面的内容(使用 Pandoc 处理)。我想要两个按钮 - 一个“预览”按钮,用于处理标记并在表单下方显示结果,以及一个“提交”按钮,用于将内容保存到数据库。
使用 Yesod 执行此操作的最简单方法是什么? Yesod 书中的所有表单示例都只有一个按钮。我查看了公开的函数/api,但即使我向表单添加了多个具有不同名称和/或值的提交按钮,我也不知道如何让 Yesod 告诉我按下了哪一个。
谁能给我一个简单的例子,说明 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以仅使用输入表单函数来获取原始值,并在各个按钮上显式设置名称属性。 HTML 中是这样的:
在 Haskell 代码中:
抱歉,如果这没有进行类型检查,我现在没有正常的开发系统。但我认为这是正确的一般方法。
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:
And in the Haskell code:
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.