以Yesod形式处理数据收集 [英] Handling a collection of data in a Yesod Form

查看:47
本文介绍了以Yesod形式处理数据收集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是的是否可以处理包含数据集合的表单?

Is it possible in Yesod to handle forms that contain a collection of data?

我有一个用户可以添加多个人的表单,在前端,当前形式如下:

I have a form that the user can add multiple people to, on the frontend it currently looks like this:

{ people.map((person, key) => (
  <td>
    <input type="hidden" name={ `person[${key}][firstName]` } value={person.firstName} />
    <input type="hidden" name={ `person[${key}][lastName]` } value={person.lastName} />
    { person.firstName } { person.lastName }
  </td>
)) }

然后,我希望能够像这样将其转换到后端:

I then want to be able to translate that over to the backend like so:

[Person "Michael" "Snoyman", Person "Ed" "Kmett"]

此列表的长度是可变的,因此它可以在 people 值中包含尽可能多的用户.到目前为止,我一直无法弄清楚如何使用Yesod中的 FormInput 复制这种东西.

This list is variable in length, so it could have as many people in the people value as the user likes. So far I've been unable to work out how to replicate this kind of thing using FormInput in Yesod.

推荐答案

您可以通过定义 unFormInput 函数来创建自己的 FormInput .此函数可以从表单中提取字段名称,提取键,然后可以使用 ireq 来推广相关字段.

You could create your own FormInput by defining the unFormInput function. This function could pull the field names from the form, extract the keys, and then you could use ireq to promote the relevant fields.

这可能看起来像

getPeople :: FormInput (your handler type) [People]
getPeople = FormInput $ \m l env fenv ->
    (unFormInput (peopleField peopleKeys)) m l env fenv
        where
            peopleKeys = getPeopleKeys env

getPeopleKeys

此帮助器功能将为您表单中的人员生成所有键值.它们还不需要是有效的,因为稍后会进行字段解析.

getPeopleKeys

This helper function would produce all the key values for the people in your form. They wouldn't need to be valid yet, since field parsing will take care of that later.

getPeopleKeys :: Env -> [Text]
getPeopleKeys env = mapMaybe extractKey (keys env)
    where
        extractKey :: Text -> Maybe Text
        extractKey key = ... -- you could use e.g. regex here
                             -- to pull the keys out of the field names
                             -- and return Nothing otherwise

peopleField

此帮助程序生成 FormInput .

  1. 获取键列表,
  2. 从每个生成一个 FormInput
  1. 为名字和姓氏生成一个字段
  2. 将这些字段转换为 FormInput s
  3. 生成一个 FormInput ,将它们组合为一个 Person
  1. generates a field for the first name and last name
  2. turns those fields into FormInputs
  3. produces a FormInput which combines them into a Person

  • FormInput 的结果连接到 FormInput ... [人员]
  • concatenates the FormInputs' results into a FormInput ... [Person]

  • peopleField :: Monad m => RenderMessage (HandlerSite m) FormMessage => [Text] -> FormInput m [Person]
    peopleField = mapM personField
        where
            personField :: Text -> Field ... Person
            personField key = (liftA2 (Person)) (personFNameField) (personLNameField)
                where
                    personFNameField = (ireq hiddenField) . fNameField key
                    personLNameField = (ireq hiddenField) . lNameField key
    
    fNameField key = ... -- this outputs "person[${key}][firstName]"
    lNameField key = ... -- etc.
    

    这篇关于以Yesod形式处理数据收集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆