Вы можете создать свой собственный FormInput
, определив функцию unFormInput
. Эта функция может извлекать имена полей из формы, извлекать ключи, а затем вы можете использовать ireq
для продвижения соответствующих полей.
Это может выглядеть примерно так:
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 :: 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
. Это
- принимает список ключей,
- генерирует
FormInput
от каждого
- генерирует поле для имени и фамилии
- превращает эти поля в
FormInput
с
- производит
FormInput
, который объединяет их в Person
- объединяет результаты
FormInput
s в 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.