Chapter 8. Forms
I’ve mentioned the boundary issue already: whenever data enters or leaves an application, we need to validate it. Probably the most difficult place this occurs is forms. Coding forms is complex; in an ideal world, we’d like a solution that addresses the following problems:
Ensure data is valid.
Marshal string data in the form submission to Haskell data types.
Generate HTML code for displaying the form.
Generate JavaScript to do clientside validation and provide more user-friendly widgets, such as date pickers.
Build up more complex forms by combining together simpler forms.
Automatically assign names to our fields that are guaranteed to be unique.
The yesod-form package provides all these features in a
simple, declarative API. It builds on top of Yesod’s widgets to simplify styling of forms and
applying JavaScript appropriately. And like the rest of Yesod, it uses Haskell’s type system to
make sure everything is working correctly.
Synopsis
{-# LANGUAGE QuasiQuotes, TemplateHaskell, MultiParamTypeClasses,OverloadedStrings, TypeFamilies #-}importYesodimportYesod.Form.JqueryimportData.Time(Day)importData.Text(Text)importControl.Applicative((<$>),(<*>))dataSynopsis=SynopsismkYesod"Synopsis"[parseRoutes|/RootRGET/personPersonRPOST|]instanceYesodSynopsis-- Tells our application to use the standard English messages.-- If you want i18n, then you can supply a translating function instead.instanceRenderMessageSynopsisFormMessagewhererenderMessage ...