{-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE AllowAmbiguousTypes #-} {-# LANGUAGE InstanceSigs, UndecidableInstances, AllowAmbiguousTypes, ScopedTypeVariables, IncoherentInstances #-} {-| Module: IHP.View.Form Description: 'IHP.View.Form.formFor' and all form controls Copyright: (c) digitally induced GmbH, 2020 -} module IHP.View.Form where import IHP.Prelude import IHP.ValidationSupport import IHP.HSX.ConvertibleStrings () import IHP.ViewErrorMessages () import IHP.ViewSupport import qualified Text.Blaze.Html5 as Html5 import IHP.HSX.ToHtml import GHC.Types import IHP.ModelSupport (getModelName, inputValue, isNew, Id', InputValue, didTouchField) import IHP.HSX.QQ (hsx) import IHP.View.Types import IHP.View.Classes () import Network.Wai (pathInfo) import IHP.Controller.Context -- | Forms usually begin with a 'formFor' expression. -- -- This is how a simple form can look like: -- -- > renderForm :: Post -> Html -- > renderForm post = formFor post [hsx| -- > {textField #title} -- > {textareaField #body} -- > {submitButton} -- > |] -- -- Calling this form from inside your HSX code will lead to the following HTML being generated: -- -- >
-- -- You can see that the form is submitted via @POST@. The form action has also been set by default to @/CreatePost@. -- -- All inputs have auto-generated class names and ids for styling. Also, all @name@ attributes are set as expected. -- -- __Field Values:__ -- -- A form control is always filled with the value of the given field when rendering. For example, given a post -- -- > let post = Post { ..., title = "Hello World" } -- -- Rendering this, the input value will be set like: -- -- >>> {textField #title} -- -- -- __Validation:__ -- -- When rendering a record that has failed validation, the validation error message will be rendered automatically. -- -- Given a post like this: -- -- > let post = Post { ..., title = "" } -- > |> validateField #title nonEmpty -- -- Rendering @{textField #title}@, the input will have the css class @is-invalid@ and an element with the error message will be rendered below the input: -- -- >