Textarea
This commit is contained in:
parent
24e6806cde
commit
adc8a8cf63
@ -10,6 +10,7 @@ module Yesod.Form
|
|||||||
, FormResult (..)
|
, FormResult (..)
|
||||||
, Enctype (..)
|
, Enctype (..)
|
||||||
, FormFieldSettings (..)
|
, FormFieldSettings (..)
|
||||||
|
, Textarea (..)
|
||||||
-- * Type synonyms
|
-- * Type synonyms
|
||||||
, Form
|
, Form
|
||||||
, Formlet
|
, Formlet
|
||||||
@ -35,6 +36,7 @@ module Yesod.Form
|
|||||||
import Yesod.Form.Core
|
import Yesod.Form.Core
|
||||||
import Yesod.Form.Fields
|
import Yesod.Form.Fields
|
||||||
import Yesod.Form.Class
|
import Yesod.Form.Class
|
||||||
|
import Yesod.Form.Profiles (Textarea (..))
|
||||||
|
|
||||||
import Text.Hamlet
|
import Text.Hamlet
|
||||||
import Yesod.Request
|
import Yesod.Request
|
||||||
|
|||||||
@ -9,6 +9,7 @@ module Yesod.Form.Class
|
|||||||
import Text.Hamlet
|
import Text.Hamlet
|
||||||
import Yesod.Form.Fields
|
import Yesod.Form.Fields
|
||||||
import Yesod.Form.Core
|
import Yesod.Form.Core
|
||||||
|
import Yesod.Form.Profiles (Textarea)
|
||||||
import Data.Int (Int64)
|
import Data.Int (Int64)
|
||||||
import Data.Time (Day, TimeOfDay)
|
import Data.Time (Day, TimeOfDay)
|
||||||
|
|
||||||
@ -53,3 +54,8 @@ instance ToFormField Html y where
|
|||||||
toFormField = htmlField
|
toFormField = htmlField
|
||||||
instance ToFormField (Maybe Html) y where
|
instance ToFormField (Maybe Html) y where
|
||||||
toFormField = maybeHtmlField
|
toFormField = maybeHtmlField
|
||||||
|
|
||||||
|
instance ToFormField Textarea y where
|
||||||
|
toFormField = textareaField
|
||||||
|
instance ToFormField (Maybe Textarea) y where
|
||||||
|
toFormField = maybeTextareaField
|
||||||
|
|||||||
@ -255,10 +255,10 @@ emailInput n =
|
|||||||
mapFormXml fieldsToInput $
|
mapFormXml fieldsToInput $
|
||||||
requiredFieldHelper emailFieldProfile (nameSettings n) Nothing
|
requiredFieldHelper emailFieldProfile (nameSettings n) Nothing
|
||||||
|
|
||||||
textareaField :: FormFieldSettings -> FormletField sub y String
|
textareaField :: FormFieldSettings -> FormletField sub y Textarea
|
||||||
textareaField = requiredFieldHelper textareaFieldProfile
|
textareaField = requiredFieldHelper textareaFieldProfile
|
||||||
|
|
||||||
maybeTextareaField :: FormFieldSettings -> FormletField sub y (Maybe String)
|
maybeTextareaField :: FormFieldSettings -> FormletField sub y (Maybe Textarea)
|
||||||
maybeTextareaField = optionalFieldHelper textareaFieldProfile
|
maybeTextareaField = optionalFieldHelper textareaFieldProfile
|
||||||
|
|
||||||
hiddenField :: FormFieldSettings -> FormletField sub y String
|
hiddenField :: FormFieldSettings -> FormletField sub y String
|
||||||
|
|||||||
@ -1,4 +1,6 @@
|
|||||||
{-# LANGUAGE QuasiQuotes #-}
|
{-# LANGUAGE QuasiQuotes #-}
|
||||||
|
{-# LANGUAGE OverloadedStrings #-}
|
||||||
|
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
|
||||||
module Yesod.Form.Profiles
|
module Yesod.Form.Profiles
|
||||||
( stringFieldProfile
|
( stringFieldProfile
|
||||||
, textareaFieldProfile
|
, textareaFieldProfile
|
||||||
@ -13,6 +15,7 @@ module Yesod.Form.Profiles
|
|||||||
, fileFieldProfile
|
, fileFieldProfile
|
||||||
, parseDate
|
, parseDate
|
||||||
, parseTime
|
, parseTime
|
||||||
|
, Textarea (..)
|
||||||
) where
|
) where
|
||||||
|
|
||||||
import Yesod.Form.Core
|
import Yesod.Form.Core
|
||||||
@ -23,6 +26,10 @@ import Data.Time (Day, TimeOfDay(..))
|
|||||||
import qualified Data.ByteString.Lazy.UTF8 as U
|
import qualified Data.ByteString.Lazy.UTF8 as U
|
||||||
import qualified Text.Email.Validate as Email
|
import qualified Text.Email.Validate as Email
|
||||||
import Network.URI (parseURI)
|
import Network.URI (parseURI)
|
||||||
|
import Database.Persist (PersistField)
|
||||||
|
|
||||||
|
import Text.Blaze.Builder.Utf8 (writeChar)
|
||||||
|
import Text.Blaze.Builder.Core (writeList, writeByteString)
|
||||||
|
|
||||||
intFieldProfile :: Integral i => FieldProfile sub y i
|
intFieldProfile :: Integral i => FieldProfile sub y i
|
||||||
intFieldProfile = FieldProfile
|
intFieldProfile = FieldProfile
|
||||||
@ -77,10 +84,27 @@ htmlFieldProfile = FieldProfile
|
|||||||
|]
|
|]
|
||||||
}
|
}
|
||||||
|
|
||||||
textareaFieldProfile :: FieldProfile sub y String
|
-- | A newtype wrapper around a 'String' that converts newlines to HTML
|
||||||
|
-- br-tags.
|
||||||
|
newtype Textarea = Textarea { unTextarea :: String }
|
||||||
|
deriving (Show, Read, Eq, PersistField)
|
||||||
|
instance ToHtml Textarea where
|
||||||
|
toHtml =
|
||||||
|
Html . writeList writeHtmlEscapedChar . unTextarea
|
||||||
|
where
|
||||||
|
-- Taken from blaze-builder and modified with newline handling.
|
||||||
|
writeHtmlEscapedChar '<' = writeByteString "<"
|
||||||
|
writeHtmlEscapedChar '>' = writeByteString ">"
|
||||||
|
writeHtmlEscapedChar '&' = writeByteString "&"
|
||||||
|
writeHtmlEscapedChar '"' = writeByteString """
|
||||||
|
writeHtmlEscapedChar '\'' = writeByteString "'"
|
||||||
|
writeHtmlEscapedChar '\n' = writeByteString "<br>"
|
||||||
|
writeHtmlEscapedChar c = writeChar c
|
||||||
|
|
||||||
|
textareaFieldProfile :: FieldProfile sub y Textarea
|
||||||
textareaFieldProfile = FieldProfile
|
textareaFieldProfile = FieldProfile
|
||||||
{ fpParse = Right
|
{ fpParse = Right . Textarea
|
||||||
, fpRender = id
|
, fpRender = unTextarea
|
||||||
, fpWidget = \theId name val _isReq -> addBody [$hamlet|
|
, fpWidget = \theId name val _isReq -> addBody [$hamlet|
|
||||||
%textarea#$theId$!name=$name$ $val$
|
%textarea#$theId$!name=$name$ $val$
|
||||||
|]
|
|]
|
||||||
|
|||||||
@ -57,7 +57,7 @@ getRootR = applyLayoutW $ flip wrapWidget wrapper $ do
|
|||||||
addHead [$hamlet|%meta!keywords=haskell|]
|
addHead [$hamlet|%meta!keywords=haskell|]
|
||||||
|
|
||||||
handleFormR = do
|
handleFormR = do
|
||||||
(res, form, enctype) <- runFormPost $ fieldsToTable $ (,,,,,,,,,)
|
(res, form, enctype) <- runFormPost $ fieldsToTable $ (,,,,,,,,,,)
|
||||||
<$> stringField (FormFieldSettings "My Field" "Some tooltip info" Nothing Nothing) Nothing
|
<$> stringField (FormFieldSettings "My Field" "Some tooltip info" Nothing Nothing) Nothing
|
||||||
<*> stringField ("Another field") (Just "some default text")
|
<*> stringField ("Another field") (Just "some default text")
|
||||||
<*> intField (FormFieldSettings "A number field" "some nums" Nothing Nothing) (Just 5)
|
<*> intField (FormFieldSettings "A number field" "some nums" Nothing Nothing) (Just 5)
|
||||||
@ -75,8 +75,12 @@ handleFormR = do
|
|||||||
<*> nicHtmlField ("HTML")
|
<*> nicHtmlField ("HTML")
|
||||||
(Just $ string "You can put rich text here")
|
(Just $ string "You can put rich text here")
|
||||||
<*> maybeEmailField ("An e-mail addres") Nothing
|
<*> maybeEmailField ("An e-mail addres") Nothing
|
||||||
|
<*> maybeTextareaField "A text area" Nothing
|
||||||
let mhtml = case res of
|
let mhtml = case res of
|
||||||
FormSuccess (_, _, _, _, _, _, _, _, x, _) -> Just x
|
FormSuccess (_, _, _, _, _, _, _, _, x, _, _) -> Just x
|
||||||
|
_ -> Nothing
|
||||||
|
let txt = case res of
|
||||||
|
FormSuccess (_, _, _, _, _, _, _, _, _, _, Just x) -> Just x
|
||||||
_ -> Nothing
|
_ -> Nothing
|
||||||
applyLayoutW $ do
|
applyLayoutW $ do
|
||||||
addStyle [$cassius|
|
addStyle [$cassius|
|
||||||
@ -98,6 +102,8 @@ textarea.html
|
|||||||
%input!type=submit
|
%input!type=submit
|
||||||
$maybe mhtml html
|
$maybe mhtml html
|
||||||
$html$
|
$html$
|
||||||
|
$maybe txt t
|
||||||
|
$t$
|
||||||
|]
|
|]
|
||||||
setTitle $ string "Form"
|
setTitle $ string "Form"
|
||||||
|
|
||||||
@ -114,9 +120,9 @@ getAutoCompleteR = do
|
|||||||
|
|
||||||
data Person = Person String Int
|
data Person = Person String Int
|
||||||
getCustomFormR = do
|
getCustomFormR = do
|
||||||
let customForm = GForm $ \e f -> do
|
let customForm = GForm $ do
|
||||||
(a1, [b1], c1) <- deform (stringInput "name") e f
|
(a1, [b1], c1) <- deform $ stringInput "name"
|
||||||
(a2, [b2], c2) <- deform (intInput "age") e f
|
(a2, [b2], c2) <- deform $ intInput "age"
|
||||||
let b = do
|
let b = do
|
||||||
b1' <- extractBody b1
|
b1' <- extractBody b1
|
||||||
b2' <- extractBody b2
|
b2' <- extractBody b2
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user