flex-tasks
Safe HaskellNone
LanguageHaskell2010

FlexTask.Form.Core

Description

Typed construction and rendering of Yesod input forms.

Forms are built from type-safe FormPieces describing individual fields and their layout. Pieces can be combined horizontally or vertically and rendered as a CompleteForm using formify.

The result type of a form determines the types and requiredness of its fields. For custom product types, this structure can be derived generically through Formify.

This module provides basic input fields, single- and multiple-choice fields, list forms, and several specialized field types.

Synopsis

Form Types

class Formify a Source #

Class for generic derivation of overall form types. Any type you want to create a completed form for needs to be an instance of this type. Bodyless instances can be declared for most types deriving Generic. Alternatively, you can also derive Formify itself using DeriveAnyClass.

Generic derivation is not supported for:

  • types with a single nullary constructor
  • sum types in which any constructor has fields

Manually implementing Formify's internals is not supported.

Associated Types

type FormTypes a Source #

The type-level non-empty list of types needed for a complete form, e.g.

OneField Int for Int

ManyFields Text for [Text]

OneField Text :> OneField Bool for (Text,Bool)

type FormTypes a = GFormTypes a (Rep a)

Instances

Instances details
Formify SingleChoiceSelection Source # 
Instance details

Defined in FlexTask.Form.Formify

Formify Text Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes Text 
Instance details

Defined in FlexTask.Form.Formify

Methods

formDefaults :: Maybe Text -> TypeList (FormTypes Text)

Formify Textarea Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes Textarea 
Instance details

Defined in FlexTask.Form.Formify

Methods

formDefaults :: Maybe Textarea -> TypeList (FormTypes Textarea)

Formify Integer Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes Integer 
Instance details

Defined in FlexTask.Form.Formify

Methods

formDefaults :: Maybe Integer -> TypeList (FormTypes Integer)

Formify Bool Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes Bool 
Instance details

Defined in FlexTask.Form.Formify

Methods

formDefaults :: Maybe Bool -> TypeList (FormTypes Bool)

Formify Double Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes Double 
Instance details

Defined in FlexTask.Form.Formify

Methods

formDefaults :: Maybe Double -> TypeList (FormTypes Double)

Formify Int Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes Int 
Instance details

Defined in FlexTask.Form.Formify

Methods

formDefaults :: Maybe Int -> TypeList (FormTypes Int)

Formify (Hidden a) Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes (Hidden a) 
Instance details

Defined in FlexTask.Form.Formify

Methods

formDefaults :: Maybe (Hidden a) -> TypeList (FormTypes (Hidden a))

Formify (MultipleChoice a) Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes (MultipleChoice a) 
Instance details

Defined in FlexTask.Form.Formify

Methods

formDefaults :: Maybe (MultipleChoice a) -> TypeList (FormTypes (MultipleChoice a))

Formify (SingleInputList a) Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes (SingleInputList a) 
Instance details

Defined in FlexTask.Form.Formify

CanBeOptional a => Formify (Maybe a) Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes (Maybe a) 
Instance details

Defined in FlexTask.Form.Formify

type FormTypes (Maybe a) = OneField a

Methods

formDefaults :: Maybe (Maybe a) -> TypeList (FormTypes (Maybe a))

Formify a => Formify [a] Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes [a] 
Instance details

Defined in FlexTask.Form.Formify

type FormTypes [a]

Methods

formDefaults :: Maybe [a] -> TypeList (FormTypes [a])

(Formify a, Formify b) => Formify (a, b) Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes (a, b) 
Instance details

Defined in FlexTask.Form.Formify

type FormTypes (a, b)

Methods

formDefaults :: Maybe (a, b) -> TypeList (FormTypes (a, b))

(Formify a, Formify b, Formify c) => Formify (a, b, c) Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes (a, b, c) 
Instance details

Defined in FlexTask.Form.Formify

type FormTypes (a, b, c)

Methods

formDefaults :: Maybe (a, b, c) -> TypeList (FormTypes (a, b, c))

(Formify a, Formify b, Formify c, Formify d) => Formify (a, b, c, d) Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes (a, b, c, d) 
Instance details

Defined in FlexTask.Form.Formify

type FormTypes (a, b, c, d)

Methods

formDefaults :: Maybe (a, b, c, d) -> TypeList (FormTypes (a, b, c, d))

(Formify a, Formify b, Formify c, Formify d, Formify e) => Formify (a, b, c, d, e) Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes (a, b, c, d, e) 
Instance details

Defined in FlexTask.Form.Formify

type FormTypes (a, b, c, d, e)

Methods

formDefaults :: Maybe (a, b, c, d, e) -> TypeList (FormTypes (a, b, c, d, e))

(Formify a, Formify b, Formify c, Formify d, Formify e, Formify f) => Formify (a, b, c, d, e, f) Source # 
Instance details

Defined in FlexTask.Form.Formify

Associated Types

type FormTypes (a, b, c, d, e, f) 
Instance details

Defined in FlexTask.Form.Formify

type FormTypes (a, b, c, d, e, f)

Methods

formDefaults :: Maybe (a, b, c, d, e, f) -> TypeList (FormTypes (a, b, c, d, e, f))

data FormPiece finalType fields Source #

The layout data type. This type is used to select and arrange the input fields in a type-safe manner. Each FormPiece is a form fragment parametrized by the desired final type of the complete form and the type of the fragment itself. The overall type is given as a plain type, while the type of the fragment is a type-level non-empty list of types, corresponding to the selected input fields.

For example, for a product type

data Person = Person {name :: Text, age :: Int, occupation :: Text}

we might want to define a form fragment that contains only the first two parts of the record, but is locked into becoming a Person form:

nameAgePiece :: FormPiece Person (OneField Text :> OneField Int)

or we could leave the overall type variable, so any type consisting of Text and Int in that order, e.g. (Text,Int) could use the fragment:

stringIntPiece :: FormPiece t (OneField Text :> OneField Int)

You will mostly be able to use the simpler type synonyms SimpleFormPiece, ListFormPiece, AnyFormPiece or CompleteForm to avoid dealing with type-level lists.

type SimpleFormPiece t a = FormPiece t (OneField a) Source #

Alias for a FormPiece with exactly one type and field that avoids having to write out the type-level list.

SimpleFormPiece t Text ≡ FormPiece t (OneField Text)

type ListFormPiece t a = FormPiece t (ManyFields a) Source #

Alias for a FormPiece with exactly one type but multiple fields (a collection) that avoids having to write out the type-level list.

ListFormPiece t Double ≡ FormPiece t (ManyFields Double)

type AnyFormPiece t a = FormPiece t (FormTypes a) Source #

Alias for a FormPiece with arbitrarily many types that avoids having to write out the type-level list.

AnyFormPiece t Person ≡ FormPiece t (OneField Text :> OneField Int :> OneField Text)
AnyFormPiece t (Text,Int,Text) ≡ FormPiece t (OneField Text :> OneField Int :> OneField Text)

type CompleteForm a = AnyFormPiece a a Source #

Alias for a FormPiece whose fields exactly match the complete form representation of its final type. Adding another piece will therefore result in a value that is no longer a CompleteForm.

CompleteForm Person ≡ AnyFormPiece Person Person ≡ FormPiece Person (FormTypes Person)

Creating Form Pieces

class (Formify a, FormTypes a ~ OneField a) => BaseField a where Source #

Types that can be represented as a simple Yesod input field. A BaseField instance of type a is needed if a requires a type-specific input method, i.e. it is not just a wrapping newtype or product type that can be built out of existing input methods.

Common types, like Int, Text or Bool, are already instances of this class, so you should not need to write your own instances.

An instance can be given manually using the Field constructor or the convertField function on an existing Field if required anyway.

Example

Expand
>>> instance BaseField MyCoolType where baseField = convertField toCool fromCool existingField

Methods

baseField :: Field Handler a Source #

The Yesod field used to render values of this type. The fieldParse component of the Field is not used. It can be assigned undefined.

Instances

Instances details
BaseField Text Source # 
Instance details

Defined in FlexTask.Form.Core

BaseField Textarea Source # 
Instance details

Defined in FlexTask.Form.Core

BaseField Integer Source # 
Instance details

Defined in FlexTask.Form.Core

BaseField Bool Source # 
Instance details

Defined in FlexTask.Form.Core

BaseField Double Source # 
Instance details

Defined in FlexTask.Form.Core

BaseField Int Source # 
Instance details

Defined in FlexTask.Form.Core

(Formify a, PathPiece a) => BaseField (Hidden a) Source # 
Instance details

Defined in FlexTask.Form.Core

Show a => BaseField (SingleInputList a) Source # 
Instance details

Defined in FlexTask.Form.Core

basic :: BaseField a => FieldSettings FlexForm -> SimpleFormPiece t a Source #

A typed single input FormPiece.

Example

Expand
>>> printWidget "en" $ formify @Int Nothing $ basic "Age"
<div class="flex-form-div form-group">
...
    <label for="flexident1">
      Age
    </label>
    <input id="flexident1" name="flex1" type="number" step="1" required="" value="">
...
</div>

singleChoice Source #

Arguments

:: ChoiceShape 
-> FieldSettings FlexForm

FieldSettings for the choice field.

-> [SomeMessage FlexForm]

option labels

-> SimpleFormPiece t SingleChoiceSelection 

A FormPiece for the predefined SingleChoiceSelection type. This is either a set of radio buttons or a selection menu, depending on the given ChoiceShape.

Example

Expand
>>> let labels = ["First Option", "Second Option", "Third Option"]
>>> printWidget "en" $ formify (Just $ singleChoiceAnswer 3) $ singleChoice Dropdown "Choose one" labels
<div class="flex-form-div form-group">
...
    <label for="flexident1">
      Choose one
    </label>
    <select id="flexident1" ...>
      <option value="" selected disabled>
        &lt;None&gt;
      </option>
      <option value="1">
        First Option
      </option>
      <option value="2">
        Second Option
      </option>
      <option value="3" selected>
        Third Option
      </option>
    </select>
...
</div>

singleChoiceEnum Source #

Arguments

:: (Eq a, Bounded a, Enum a) 
=> ChoiceShape 
-> FieldSettings FlexForm

FieldSettings for the choice field.

-> (a -> SomeMessage FlexForm)

Function from enum type values to labels

-> SimpleFormPiece t a 

A FormPiece for custom enum types. This is either a set of radio buttons or a selection menu, depending on the given ChoiceShape.

The third argument is an assignment of labels for each enum constructor.

Examples

Expand
>>> printWidget "en" $ formify (Just Two) $ singleChoiceEnum (Buttons Horizontal) "Choose one" $ showToUniversalLabel @MyType
...
<div class="flex-form-div form-group">
...
    <label for="flexident1">
      Choose one
    </label>
    <div>
      <span id="flexident1">
        <label>
          <input id="flexident1-1" type="radio" ... value="1" required...>
          One
        </label>
        <label>
          <input id="flexident1-2" type="radio" ... value="2" checked required...>
          Two
        </label>
        <label>
          <input id="flexident1-3" type="radio" ... value="3" required...>
          Three
        </label>
      </span>
    </div>
...
</div>

>>> printWidget "en" $ formify (Just Two) $ singleChoiceEnum Dropdown "Choose one" $ showToUniversalLabel @MyType
<div class="flex-form-div form-group">
...
    <label for="flexident1">
      Choose one
    </label>
    <select id="flexident1" ...>
      <option value="" selected disabled>
        &lt;None&gt;
      </option>
      <option value="1">
        One
      </option>
      <option value="2" selected>
        Two
      </option>
      <option value="3">
        Three
      </option>
    </select>
...
</div>

multipleChoice Source #

A FormPiece for the predefined MultipleChoiceSelection type. This is either a set of checkboxes or a multi-selection menu, depending on the given ChoiceShape.

Example

Expand
>>> let labels = ["First Option", "Second Option", "Third Option"]
>>> printWidget "en" $ formify (Just $ multipleChoiceAnswer [1,2]) $ multipleChoice Dropdown "Choose some" labels
<div class="flex-form-div form-group">
...
    <label for="flexident1">
      Choose some
    </label>
    <select id="flexident1" ... multiple>
      <option value="1" selected>
        First Option
      </option>
      <option value="2" selected>
        Second Option
      </option>
      <option value="3">
        Third Option
      </option>
    </select>
...
</div>

multipleChoiceEnum Source #

Arguments

:: (Eq a, Bounded a, Enum a) 
=> ChoiceShape 
-> FieldSettings FlexForm

FieldSettings for the choice field.

-> (a -> SomeMessage FlexForm)

Function from enum type values to labels

-> SimpleFormPiece t (MultipleChoice a) 

A FormPiece for custom enum types. This is either a set of checkboxes or a multi-selection menu, depending on the given ChoiceShape.

The third argument is an assignment of labels for each enum constructor.

Examples

Expand
>>> let prefill = Just $ MultipleChoice [Two,Three]
>>> printWidget "en" $ formify prefill $ multipleChoiceEnum (Buttons Horizontal) "Choose" $ showToUniversalLabel @MyType
...
<div class="flex-form-div form-group">
...
    <label for="flexident1">
      Choose
    </label>
...
...
      <label>
        <input type="checkbox" ... value="1">
        One
      </label>
      <label>
        <input type="checkbox" ... value="2" checked>
        Two
      </label>
      <label>
        <input type="checkbox" ... value="3" checked>
        Three
      </label>
...
</div>

>>> let prefill = Just $ MultipleChoice [Two,Three]
>>> printWidget "en" $ formify prefill $ multipleChoiceEnum Dropdown "Choose some" $ showToUniversalLabel @MyType
<div class="flex-form-div form-group">
...
    <label for="flexident1">
      Choose some
    </label>
    <select id="flexident1" ... multiple>
      <option value="1">
        One
      </option>
      <option value="2" selected>
        Two
      </option>
      <option value="3" selected>
        Three
      </option>
    </select>
...
</div>

list Source #

Arguments

:: Alignment

Alignment of the individual fields

-> (a -> TypeField b)

how to build each field given an arbitrary value

-> [a]

the list of values to build individual fields out of

-> ListFormPiece t b 

Creates a FormPiece for a list type. The length of the list is equal to the amount of seed values provided.

Preconditions:

  • The list of seed values may not be empty
  • When finally rendered through formify, if a default value list is given, then it must have the same length as the list of seed values

Otherwise you will encounter a runtime error.

Example

Expand
>>> let labels = ["Input 1", "Input 2", "Input 3"]
>>> printWidget "en" $ formify @[Double] Nothing $ list Horizontal basicField labels
<div class="flex-form-div form-group">
...
    <label for="flexident1">
      Input 1
    </label>
    <input id="flexident1" ... type="number" step="any" ...>
...
    <label for="flexident2">
      Input 2
    </label>
    <input id="flexident2" ... type="number" step="any" ...>
...
    <label for="flexident3">
      Input 3
    </label>
    <input id="flexident3" ... type="number" step="any" ...>
...
</div>

listRepeatedly Source #

Arguments

:: Alignment

Alignment of the individual fields

-> Int

How many copies

-> TypeField a

The field to multiply

-> ListFormPiece t a 

Same as list but copies a single given TypeField multiple times.

Preconditions:

  • The number of copies is positive
  • When finally rendered through formify, if a default value list is given, then its length must match the number of copies

Otherwise you will encounter a runtime error.

Example

Expand
>>> printWidget "en" $ formify @[Int] Nothing $ listRepeatedly Vertical 3 (basicField "input")
<div class="flex-form-div form-group">
...
    <label for="flexident1">
      input
    </label>
    <input id="flexident1" ... type="number" ... value="">
...
</div>
<div class="flex-form-div form-group">
...
    <label for="flexident2">
      input
    </label>
    <input id="flexident2" ... type="number" ... value="">
...
</div>
<div class="flex-form-div form-group">
...
    <label for="flexident3">
      input
    </label>
    <input id="flexident3" ... type="number" ... value="">
...
</div>

listWithoutLabels Source #

Arguments

:: Alignment

Alignment of the individual fields

-> Int

Amount of fields

-> (FieldSettings FlexForm -> TypeField a)

The TypeField primitive to use

-> [(Text, Text)]

List of attribute and value pairs (attribute "class" for classes)

-> ListFormPiece t a 

Same as list, but without using any field labels. Per field attributes and CSS classes cannot be set with this function. Instead, all fields share the given list of attributes. Use list if individual configuration is required.

Preconditions:

  • The amount of fields is positive
  • When finally rendered through formify, if a default value list is given, then its length must match the amount of fields

Otherwise you will encounter a runtime error.

Example

Expand

Renders a series of four input fields of type Text organized vertically beneath each other. They are prefilled with the values given above, assigned the CSS class "helloInput" and have no labels attached to them.

>>> let defaults = ["Hallo", "Hello", "Hola", "Ciao" :: Text]
>>> printWidget "en" $ formify (Just defaults) $ listWithoutLabels Vertical 4 basicField [("class","helloInput")]
<div class="flex-form-div form-group">
...
    <input id="flexident1" ... type="text" ... value="Hallo" class="helloInput">
...
</div>
<div class="flex-form-div form-group">
...
    <input id="flexident2" ... type="text" ... value="Hello" class="helloInput">
...
</div>
<div class="flex-form-div form-group">
...
    <input id="flexident3" ... type="text" ... value="Hola" class="helloInput">
...
</div>
<div class="flex-form-div form-group">
...
    <input id="flexident4" ... type="text" ... value="Ciao" class="helloInput">
...
</div>

Fields for List Forms

The functions in this section behave identically to their counterparts producing a FormPiece, for example basicField behaves like basic, but produces a TypeField instead.

Their purpose is to restrict the options that can be used with list at the type level. The builder passed to list must produce a TypeField. It cannot produce a composed FormPiece, such as one obtained using >|.

The restriction is necessary, because the current approach to parsing the user input cannot handle arbitrary combinations, but only a specific subset of those. There is also no TypeField equivalent for multipleChoice and multipleChoiceEnum for the same reason. Lists of MultipleChoice forms are therefore not supported at the moment.

data TypeField a Source #

Represents a specific input field associated with given type a.

basicField :: BaseField a => FieldSettings FlexForm -> TypeField a Source #

A typed single input field.

singleChoiceField Source #

Arguments

:: ChoiceShape 
-> FieldSettings FlexForm

FieldSettings for the choice field.

-> [SomeMessage FlexForm]

Option labels

-> TypeField SingleChoiceSelection 

A TypeField for the predefined SingleChoiceSelection type. This is either a set of radio buttons or a selection menu, depending on the given ChoiceShape.

singleChoiceEnumField Source #

Arguments

:: (Eq a, Bounded a, Enum a) 
=> ChoiceShape 
-> FieldSettings FlexForm

FieldSettings for the choice field.

-> (a -> SomeMessage FlexForm)

Function from enum type values to labels.

-> TypeField a 

A TypeField for custom enum types. This is either a set of radio buttons or a selection menu, depending on the given ChoiceShape.

The third argument is an assignment of labels for each enum constructor.

Composition and Layout

(>|) :: FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys) infixr 5 Source #

Combines two FormPieces horizontally, i.e. beside each other.

Examples

Expand

Input

basic "field1" >| basic "field2"

Renders as:

Horizontal composition does not preserve empty columns when the two pieces have different numbers of rows. If the right-hand piece is taller than the left-hand piece, its remaining rows will therefore appear in the leftmost position.

Input

list Vertical basicField ["field1_1, field1_2"] >| list Vertical basicField ["field2_1", "field2_2", field2_3]

will not result in

but instead in

beside :: FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys) Source #

A non-infix alias for >|

(>-) :: FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys) infixr 4 Source #

Combines two FormPieces vertically, i.e. below each other.

Examples

Expand

Input

basic "field1" >- basic "field2"

Renders as:

above :: FormPiece t xs -> FormPiece t ys -> FormPiece t (xs ++ ys) Source #

A non-infix alias for >-

Rendering

formify Source #

Arguments

:: Formify a 
=> Maybe a

Optional default value.

-> CompleteForm a

Structure and type of the form.

-> Rendered Widget

Rendered form.

Renders a form from a CompleteForm description, optionally prefilling its fields with a default value.

Whether individual fields are required or optional is determined by the CompleteForm's result type. For example, a field corresponding to Text is required, while one corresponding to Maybe Text is optional. The type of the default value, when present, must match that result type and can be used to infer it.

For example

formify (Just "enter name") $ basic "last name"

determines the Text input is required

formify (Just $ Just "enter name") $ basic "last name"

would make it optional instead.

This also applies for multiple fields

formify (Just ("enter name", True)) $ basic "last name" >- basic "receive newsletter?"

means both fields are required

formify (Just ("enter name", Nothing)) $ basic "last name" >- basic "receive newsletter?"

means the Text field is required, while the Bool field is optional.

Forms for custom data types will also automatically infer this based on the types of the constructor fields.

Note that the type of the form can only be inferred if either the default is a Just value or the CompleteForm was previously given an explicit type signature. You will have to use TypeApplications on formify if that's not the case, e.g.

formify @(Text, Maybe Bool) Nothing $ basic "last name" >- basic "receive newsletter?"

More examples can be found under sections Creating Form Pieces and Composition and Layout

formifyComponents Source #

Arguments

:: Formify a 
=> Maybe a

Optional default value for form

-> CompleteForm a

Structure and type of form

-> Rendered [[Widget]]

structured sub-renders

Like formify, but yields the individual sub-renders instead of a combined form. Retains the layout structure given by the CompleteForm as a nested list. Each inner list is a row in the form layout. This can be used in custom forms to incorporate formify-generated forms.

formifyComponentsFlat Source #

Arguments

:: Formify a 
=> Maybe a

Optional default value for form

-> CompleteForm a

Structure and type of form

-> Rendered [Widget]

sub-renders

Like formifyComponents, but forgets the layout information of CompleteForm. Simply returns all sub-renders in a flat list.

Appearance and Layout Options

data Alignment Source #

Choice between horizontal and vertical alignment.

Constructors

Horizontal 
Vertical 

Instances

Instances details
Show Alignment Source # 
Instance details

Defined in FlexTask.Form.Core

Eq Alignment Source # 
Instance details

Defined in FlexTask.Form.Core

data ChoiceShape Source #

Choice between radio buttons/checkboxes and selection menus

Constructors

Buttons Alignment

Buttons arranged according to the given alignment

Dropdown

Selection menu

Type-Level Helper Types

data OneField a Source #

A marker for type forms with exactly one input field

data ManyFields a Source #

A marker for type forms with multiple input fields, e.g. lists.

data x :> xs infixr 6 Source #

Type-level non-empty list equivalent of "cons" (:)

type family xs ++ ys where ... infixr 5 Source #

type-level non-empty list equivalent of "append" (++)

Equations

(OneField a) ++ ys = OneField a :> ys 
(ManyFields a) ++ ys = ManyFields a :> ys 
(x :> xs) ++ ys = x :> (xs ++ ys) 

type family CanBeOptional a where ... Source #

A constraint for types that can meaningfully be made optional.

Equations

CanBeOptional (MultipleChoice a) = TypeError ('Text "MultipleChoice cannot be optional." ':$$: 'Text "No selection is represented by the empty list.") :: Constraint 
CanBeOptional [a] = TypeError ('Text "Lists cannot be optional as a whole." ':$$: 'Text "Use type [Maybe a] to turn all fields optional instead.") :: Constraint 
CanBeOptional a = ()