yesodweb/yesod — Issue #183: Add multiSelectField' and some other improvements
Author: ondrap
Created: 2011-12-14 00:15:56+00:00
State: open

I was missing multiSelectField', so here is my implementation (needs FlexibileContexts, but otherwise seems to work):

```haskell
multiSelectField' :: forall a sub master . (Eq a, RenderMessage master FormMessage) =>
GGHandler sub master IO (OptionList a) -> Field sub master [a]
multiSelectField' ioptlist = Field parse view
where
parse [] = return $ Right Nothing
parse optlist = do
mapopt <- olReadExternal `fmap` ioptlist
case sequence $ map mapopt optlist of
Nothing -> return $ Left "Error parsing values"
Just res -> return $ Right $ Just res

view theId name val isReq = do
opts <- fmap olOptions $ lift $ liftIOHandler ioptlist
let selOpts = map (id &&& (optselected val)) opts
[whamlet|

$forall (opt, optsel) <- selOpts

#{optionDisplay opt}
|]
where
optselected (Left _) _ = False
optselected (Right vals) opt = (optionInternalValue opt) `elem` vals
```
It would also be nice to have some version of 'optionsPersist' that would just return the 'Id' (the Key) as optionInternalValue. I just wonder how usable is the current implementation of optionPersist, as you would have to supply the pair (Key, Value) as input to the multi/singleSelectField, which is a little impracitical.