jaspervdj / digestive-functors

A general way to consume input using applicative functors
149 stars 71 forks source link

best practice for pre-populating form with existing data? #97

Closed mavenraven closed 10 years ago

mavenraven commented 10 years ago

So, I'm working on a form that edits data that has already been saved to the database. Right now, I'm just manually splicing in the data with heist, but that kind of falls apart with my sum type / drop down mapping. I could dump the data into the text form digestive functor likes (e.g. "form.name=hello&form.field=bye..."), but I was thinking that there might be a nicer way to do that that I can't figure out, since it seems like a common use case? If not, I was thinking of adding something like

prepopulate :: a -> Form v Identity a -> View v

to digestive functors.

cimmanon commented 10 years ago

I've just been passing in the data as an argument to the form:

addressForm :: Monad m => Maybe Address -> Form Text m Address
addressForm a = monadic $ do
    stateChoices <- getStatesFromDB
    countryChoices <- getCountriesFromDB
    return $
      Address
          <$> "name" .: optionalString (name =<< a)
          <*> "street" .: string (street <$> a)
          <*> "city" .: string (city <$> a)
          <*> "state" .: choiceWith stateChoices (state <$> a)
          <*> "country" .: choiceWith countryChoices (country <$> a)
          <*> "zipcode" .: string (zipcode <$> a)
mightybyte commented 10 years ago

Yeah, this is exactly what the formlet type alias is for.

http://hackage.haskell.org/package/digestive-functors-0.7.1.1/docs/Text-Digestive-Form.html#t:Formlet

mavenraven commented 10 years ago

Oh yeah, that makes perfect sense, thanks for the help all!