diff options
| author | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-13 21:42:14 +0100 |
|---|---|---|
| committer | Gregor Kleen <gkleen@yggdrasil.li> | 2016-01-13 21:42:14 +0100 |
| commit | 605f2c807115baff10efa27f0d3348ef952b969f (patch) | |
| tree | df414874b24125d20cce10c8e5f533dad7a3f6bb | |
| parent | c3911983dbe7a1c383b3887846cf5a01444879ce (diff) | |
| download | uni-605f2c807115baff10efa27f0d3348ef952b969f.tar uni-605f2c807115baff10efa27f0d3348ef952b969f.tar.gz uni-605f2c807115baff10efa27f0d3348ef952b969f.tar.bz2 uni-605f2c807115baff10efa27f0d3348ef952b969f.tar.xz uni-605f2c807115baff10efa27f0d3348ef952b969f.zip | |
FFP 11.3
| -rw-r--r-- | ws2015/ffp/blaetter/11/FFP_U11-3_Yesod.hs | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/ws2015/ffp/blaetter/11/FFP_U11-3_Yesod.hs b/ws2015/ffp/blaetter/11/FFP_U11-3_Yesod.hs index badd9e0..78c057c 100644 --- a/ws2015/ffp/blaetter/11/FFP_U11-3_Yesod.hs +++ b/ws2015/ffp/blaetter/11/FFP_U11-3_Yesod.hs | |||
| @@ -46,8 +46,10 @@ | |||
| 46 | 46 | ||
| 47 | 47 | ||
| 48 | import Yesod | 48 | import Yesod |
| 49 | import Data.Text | 49 | import Data.Text (Text) |
| 50 | import qualified Data.Text as T | ||
| 50 | import Control.Applicative | 51 | import Control.Applicative |
| 52 | import Data.Traversable (sequenceA) | ||
| 51 | import Yesod.Form | 53 | import Yesod.Form |
| 52 | 54 | ||
| 53 | 55 | ||
| @@ -83,21 +85,28 @@ carAForm = Car | |||
| 83 | <*> areq intField "Year" (Just 1996) | 85 | <*> areq intField "Year" (Just 1996) |
| 84 | <*> aopt textField "Color" Nothing | 86 | <*> aopt textField "Color" Nothing |
| 85 | 87 | ||
| 86 | carForm :: Html -> MForm Handler (FormResult Car, Widget) | ||
| 87 | carForm = renderBootstrap2 carAForm | ||
| 88 | |||
| 89 | -- Beispiel zur Kombination zweier applikativer Formulare zu einem: | 88 | -- Beispiel zur Kombination zweier applikativer Formulare zu einem: |
| 90 | twoCarAForm :: AForm Handler (Car,Car) | 89 | twoCarAForm :: AForm Handler (Car,Car) |
| 91 | twoCarAForm = (,) <$> carAForm <*> carAForm | 90 | twoCarAForm = (,) <$> carAForm <*> carAForm |
| 92 | 91 | ||
| 93 | 92 | ||
| 94 | handleHomeR :: Handler Html | 93 | handleHomeR :: Handler Html |
| 95 | handleHomeR = redirect $ CarR 3 | 94 | handleHomeR = do |
| 95 | ((result, widget), enctype) <- runFormPost . renderBootstrap2 $ areq intField "Number of Cars" Nothing | ||
| 96 | case result of | ||
| 97 | FormMissing -> defaultLayout $ do | ||
| 98 | [whamlet| | ||
| 99 | <h2>Form Demo | ||
| 100 | <form method=post action=@{HomeR} enctype=#{enctype}> | ||
| 101 | ^{widget} | ||
| 102 | <button>Submit | ||
| 103 | |] | ||
| 104 | FormSuccess n -> redirect $ CarR n | ||
| 96 | 105 | ||
| 97 | 106 | ||
| 98 | handleCarR :: Int -> Handler Html | 107 | handleCarR :: Int -> Handler Html |
| 99 | handleCarR n = do | 108 | handleCarR n = do |
| 100 | ((result,widget), enctype) <- runFormPost $ carForm | 109 | ((result,widget), enctype) <- runFormPost . renderBootstrap2 . sequenceA . replicate n $ carAForm |
| 101 | case result of | 110 | case result of |
| 102 | FormMissing -> defaultLayout $ do | 111 | FormMissing -> defaultLayout $ do |
| 103 | setTitle "Form Demo" | 112 | setTitle "Form Demo" |
| @@ -108,12 +117,12 @@ handleCarR n = do | |||
| 108 | <button>Submit | 117 | <button>Submit |
| 109 | |] | 118 | |] |
| 110 | 119 | ||
| 111 | FormSuccess car -> defaultLayout $ do | 120 | FormSuccess cars -> defaultLayout $ do |
| 112 | setTitle "Form Auswerten" | 121 | setTitle "Form Auswerten" |
| 113 | [whamlet| | 122 | [whamlet| |
| 114 | <h2>#{n} Cars should have been received: | 123 | <h2>#{n} Cars should have been received: |
| 115 | <ul> | 124 | <ul> |
| 116 | $forall acar <- [car] | 125 | $forall acar <- cars |
| 117 | <li>#{show acar} | 126 | <li>#{show acar} |
| 118 | <p> | 127 | <p> |
| 119 | <a href=@{HomeR}>Zurück | 128 | <a href=@{HomeR}>Zurück |
