[web-devel] Yesod: generating code for optional vs required fields.

Matt Brown matt at softmechanics.net
Thu Oct 14 22:46:58 CEST 2010


Hello all,

I'm new to Yesod, and I'm trying to render my forms like this:
http://www.stylephreak.com/uploads/source/cssforms/cssform.php.

This has two main requirements: Use divs instead of tables, and
specify whether the field is required or optional.  This is what I
have so far:

fieldsToDivs :: FormField sub y a -> Form sub y a
fieldsToDivs = mapFormXml $ mapM_ go
  where
    go fi = do
        let cls = string $ if fieldRequired fi then "fm-req" else "fm-opt"
        wrapWidget (fiInput fi) $ \w -> [$hamlet|
%div!class=$cls$
  %label!for=$fiIdent.fi$ $fiLabel.fi$
    .tooltip $fiTooltip.fi$
 ^w^
 $maybe fiErrors.fi err
   %div.errors $err$
|]

fieldRequired _ = True

I'm not sure how to implement fieldRequired.  Is there a way to infer
required/optional from FieldInfo?  Would it make sense to add
fiRequired :: Bool to FieldInfo?

thanks,
-matt


More information about the web-devel mailing list