| Safe Haskell | None |
|---|---|
| Language | Haskell98 |
Ditto.Core
Description
The core module for ditto.
This module provides the Form type and helper functions
for constructing typesafe forms inside arbitrary "views" / web frameworks.
ditto is meant to be a generalized formlet library used to write
formlet libraries specific to a web / gui framework
Synopsis
- type FormState m = StateT FormRange m
- data Form m input err view a = Form {
- formDecodeInput :: input -> m (Either err a)
- formInitialValue :: m a
- formFormlet :: FormState m (View err view, Result err (Proved a))
- class Monad m => Environment m input | m -> input where
- environment :: FormId -> m (Value input)
- newtype NoEnvironment input m a = NoEnvironment {
- getNoEnvironment :: m a
- newtype WithEnvironment input m a = WithEnvironment {
- getWithEnvironment :: ReaderT (FormId -> m (Value input)) m a
- noEnvironment :: Applicative m => FormId -> m (Value input)
- (@$) :: Monad m => (view -> view') -> Form m input err view a -> Form m input err view' a
- catchFormError :: Monad m => ([err] -> a) -> Form m input err view a -> Form m input err view a
- catchFormErrorM :: Monad m => Form m input err view a -> ([err] -> Form m input err view a) -> Form m input err view a
- eitherForm :: Monad m => Text -> Form m input err view a -> m (Either view a)
- getFormId :: Monad m => FormState m FormId
- getFormInput :: Environment m input => FormState m (Value input)
- getFormInput' :: Environment m input => FormId -> FormState m (Value input)
- getFormRange :: Monad m => FormState m FormRange
- getNamedFormId :: Monad m => Text -> FormState m FormId
- incrementFormId :: FormId -> FormId
- isInRange :: FormId -> FormRange -> Bool
- mapFormMonad :: Monad f => (forall x. m x -> f x) -> Form m input err view a -> Form f input err view a
- mapResult :: Monad m => (Result err (Proved a) -> Result err (Proved a)) -> (View err view -> View err view) -> Form m input err view a -> Form m input err view a
- mapView :: Functor m => (view -> view') -> Form m input err view a -> Form m input err view' a
- mkOk :: Monad m => FormId -> view -> a -> FormState m (View err view, Result err (Proved a))
- retainChildErrors :: FormRange -> [(FormRange, e)] -> [e]
- retainErrors :: FormRange -> [(FormRange, e)] -> [e]
- runForm :: Monad m => Text -> Form m input err view a -> m (View err view, Result err (Proved a))
- runForm_ :: Monad m => Text -> Form m input err view a -> m (view, Maybe a)
- successDecode :: Applicative m => a -> input -> m (Either err a)
- unitRange :: FormId -> FormRange
- view :: Monad m => view -> Form m input err view ()
- viewForm :: Monad m => Text -> Form m input err view a -> m view
- pureRes :: (Monad m, Monoid view, FormError input err) => a -> Either err a -> Form m input err view a
- liftForm :: (Monad m, Monoid view) => m a -> Form m input err view a
Form types
The representation of formlets
type FormState m = StateT FormRange m Source #
The Form's state is just the range of identifiers so far
data Form m input err view a Source #
ditto's representation of a formlet
Constructors
| Form | |
Fields
| |
Instances
| Functor m => Bifunctor (Form m input err) Source # | |
| (Environment m input, Monoid view, FormError input err) => Monad (Form m input err view) Source # | |
| Functor m => Functor (Form m input err view) Source # | |
| (Monad m, Monoid view) => Applicative (Form m input err view) Source # | |
Defined in Ditto.Core Methods pure :: a -> Form m input err view a # (<*>) :: Form m input err view (a -> b) -> Form m input err view a -> Form m input err view b # liftA2 :: (a -> b -> c) -> Form m input err view a -> Form m input err view b -> Form m input err view c # (*>) :: Form m input err view a -> Form m input err view b -> Form m input err view b # (<*) :: Form m input err view a -> Form m input err view b -> Form m input err view a # | |
| (Monad m, Monoid view, FormError input err, Environment m input) => Alternative (Form m input err view) Source # | |
| (Monad m, Monoid view, Semigroup a) => Semigroup (Form m input err view a) Source # | |
| (Monad m, Monoid view, Monoid a) => Monoid (Form m input err view a) Source # | |
Environment
The interface to a given web framework
class Monad m => Environment m input | m -> input where Source #
The environment typeclass: the interface between ditto and a given framework
Methods
environment :: FormId -> m (Value input) Source #
Instances
| Monad m => Environment (WithEnvironment input m) input Source # | |
Defined in Ditto.Core Methods environment :: FormId -> WithEnvironment input m (Value input) Source # | |
| Monad m => Environment (NoEnvironment input m) input Source # | |
Defined in Ditto.Core Methods environment :: FormId -> NoEnvironment input m (Value input) Source # | |
newtype NoEnvironment input m a Source #
Run the form, but always return the initial value
Constructors
| NoEnvironment | |
Fields
| |
Instances
newtype WithEnvironment input m a Source #
Run the form, but with a given environment function
Constructors
| WithEnvironment | |
Fields
| |
Instances
noEnvironment :: Applicative m => FormId -> m (Value input) Source #
environment which will always return the initial value
Utility functions
(@$) :: Monad m => (view -> view') -> Form m input err view a -> Form m input err view' a infixr 0 Source #
infix mapView: succinctly mix the view dsl and the formlets dsl foo @$ do ..
catchFormError :: Monad m => ([err] -> a) -> Form m input err view a -> Form m input err view a Source #
Catch errors purely
catchFormErrorM :: Monad m => Form m input err view a -> ([err] -> Form m input err view a) -> Form m input err view a Source #
Catch errors inside Form / m
Arguments
| :: Monad m | |
| => Text | Identifier for the form |
| -> Form m input err view a | Form to run |
| -> m (Either view a) | Result |
Evaluate a form
Returns:
Left view- on failure. The
viewwill have already been applied to the errors. Right a- on success.
getFormInput :: Environment m input => FormState m (Value input) Source #
Utility function: Get the current input
getFormInput' :: Environment m input => FormId -> FormState m (Value input) Source #
Utility function: Gets the input of an arbitrary FormId.
incrementFormId :: FormId -> FormId Source #
Increment a form ID
mapFormMonad :: Monad f => (forall x. m x -> f x) -> Form m input err view a -> Form f input err view a Source #
Change the underlying Monad of the form, usually a lift or newtype
mapResult :: Monad m => (Result err (Proved a) -> Result err (Proved a)) -> (View err view -> View err view) -> Form m input err view a -> Form m input err view a Source #
Map over the Result and View of a form
Arguments
| :: Functor m | |
| => (view -> view') | Manipulator |
| -> Form m input err view a | Initial form |
| -> Form m input err view' a | Resulting form |
Common operations on Forms
Change the view of a form using a simple function
This is useful for wrapping a form inside of a <fieldset> or other markup element.
mkOk :: Monad m => FormId -> view -> a -> FormState m (View err view, Result err (Proved a)) Source #
Utility Function: turn a view and pure value into a successful FormState
retainChildErrors :: FormRange -> [(FormRange, e)] -> [e] Source #
Select the errors originating from this form or from any of the children of this form
retainErrors :: FormRange -> [(FormRange, e)] -> [e] Source #
Select the errors for a certain range
runForm :: Monad m => Text -> Form m input err view a -> m (View err view, Result err (Proved a)) Source #
Run a form
runForm_ :: Monad m => Text -> Form m input err view a -> m (view, Maybe a) Source #
Run a form, and unwrap the result
successDecode :: Applicative m => a -> input -> m (Either err a) Source #
Always succeed decoding
unitRange :: FormId -> FormRange Source #
Turns a FormId into a FormRange by incrementing the base for the end Id
Run the form with no environment, return only the html. This means that the values will always be their defaults