flex-tasks-0.1
Safe HaskellNone
LanguageHaskell2010

FlexTask.FormUtil

Description

Functions for creating and composing forms.

Synopsis

Functions for Rendered

($$>) :: (Monad w, Monad m) => Rendered' m (w a) -> Rendered' m (w b) -> Rendered' m (w b) infixr 0 Source #

Compose two forms sequentially. The result contains all fields from both input forms. This is used to compose formify generated forms with custom ones.

Note that forms generated by formify will always be wrapped in an outer <div> tag. This means composing such a form with something else results in a line break. You may circumvent this via CSS rules on the 'flex-form-div' class.

Example

Expand
>>> printWidget "de" $ myForm $$> myOtherForm
<div class="flex-form-div">
...
    <label for="flexident1">
      ...
    </label>
...
</div>
<div class="flex-form-div">
...
    <label for="flexident2">
      ...
    </label>
...
</div>

addCss Source #

Arguments

:: (render ~ RY FlexForm, Functor m) 
=> (render -> Css)

CSS template

-> Rendered' m Widget

Form to add to

-> Rendered' m Widget 

Add CSS to a form. Use with Yesod Cassius or Lucius Shakespeare quasi quoters. The content will be inserted in a <style> tag at the top of the document.

Example

Expand
>>> printWidget "en" $ addCss [lucius| myClass {margin: 2px}|] myForm
<style>
  myClass{margin:2px}
</style>
<div class="flex-form-div">
...
</div>

addJs Source #

Arguments

:: (render ~ RY FlexForm, Functor m) 
=> (render -> Javascript)

Javascript template

-> Rendered' m Widget

Form to add to

-> Rendered' m Widget 

Add JavaScript to a form. Use with Yesod Julius Shakespeare quasi quoters. The content will be inserted in a <script> tag at the bottom of the document.

Example

Expand
>>> printWidget "de" $ addJs [julius|myFunc(){ console.log("Hi"); }|] myForm
<div class="flex-form-div">
...
</div>
<script>
  myFunc(){ console.log("Hi"); }
</script>

addCssAndJs Source #

Arguments

:: (render ~ RY FlexForm, Functor m) 
=> (render -> Css)

CSS template

-> (render -> Javascript)

Javascript template

-> Rendered' m Widget

Form to add to

-> Rendered' m Widget 

Like addCss and addJs, but for including CSS and JavaScript in one step.

applyToWidget :: Functor m => (w -> w') -> Rendered' m w -> Rendered' m w' Source #

Apply some function to the embedded Widget of a Rendered value. This can be used to alter the form HTML after using formify, e.g. if some custom text is to be included with the element.

Example

Expand
>>> printWidget "de" $ applyToWidget ([whamlet| <h1>Insert me at once!|] >>) myForm
<h1>
  Insert me at once!
</h1>
<div class="flex-form-div">
...
</div>

getFormData :: Rendered Widget -> IO ([Text], HtmlDict) Source #

Extract a form from the environment. The result is an IO embedded tuple of field IDs and a map of language and internationalized html pairs.

Convenience functions for Yesod FieldSettings

addAttribute :: (Text, Text) -> FieldSettings app -> FieldSettings app Source #

Add an attribute-value pair to the given FieldSettings.

Example

Expand
>>> addAttribute ("type","hidden") "testSettings" :: FieldSettings FlexForm
FieldSettings {fsLabel = (German: "testSettings", English: "testSettings"), ..., fsAttrs = [("type","hidden")]}

addAttributes :: [(Text, Text)] -> FieldSettings app -> FieldSettings app Source #

Add a list of attribute-value pairs to the given FieldSettings.

addCssClass :: Text -> FieldSettings app -> FieldSettings app Source #

Add a CSS class to the given FieldSettings.

Example

Expand
>>> addCssClass "nav" "testSettings" :: FieldSettings FlexForm
FieldSettings {fsLabel = (German: "testSettings", English: "testSettings"), ..., fsAttrs = [("class","nav")]}

addNameAndCssClass :: Text -> Text -> FieldSettings app Source #

Directly create a Yesod FieldSettings with this name and CSS Class.

Using the IsString instance of `FieldSettings a` only sets the label. The name is then auto generated. Useful when writing a custom form to group multiple inputs.

Example

Expand
>>> addNameAndCssClass "testSettings" "nav" :: FieldSettings FlexForm
FieldSettings {..., fsName = Just "testSettings", fsAttrs = [("class","nav")]}

readOnly :: FieldSettings app -> FieldSettings app Source #

Turn FieldSettings into a read-only input field.

Convenience for internationalization

universalLabel :: String -> SomeMessage FlexForm Source #

Turn a String into a label for all languages.

Example

Expand
>>> universalLabel "index"
(German: "index", English: "index")

showToUniversalLabel :: Show a => a -> SomeMessage FlexForm Source #

Turn the Show instance of a value into a label for all languages.

Example

Expand
>>> showToUniversalLabel (1 :: Int)
(German: "1", English: "1")

functions for custom forms

newFlexId :: MForm Handler Text Source #

Get a unique identifier for an html element. The format is "flexident[number]"

newFlexName :: MForm Handler Text Source #

Get a unique name for an html element. The format is "flex[number]"

repeatFlexName :: MForm Handler Text Source #

repeat the last received name.

debugging

printWidget :: Lang -> Rendered Widget -> IO () Source #

Pretty prints the given embedded Widget's HTML code in the console. Applies the specified language for internationalization. Used for debugging.

Example

Expand
>>> printWidget "en" $ formify (Nothing @Int) [[single "Number Please"]]
<div class="flex-form-div">
  <input type="hidden" name="_hasdata">
  <span class="required flex-form-span">
    <label for="flexident1">
      Number Please
    </label>
    <input id="flexident1" name="flex1" type="number" step="1" required="" value="">
  </span>
</div>