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

Matt Brown matt at softmechanics.net
Fri Oct 15 01:56:39 CEST 2010


Adding fiRequired worked for me, and was quite painless:
http://github.com/softmechanics/yesod/commit/77eb435a629717f063f7a31190162344b0fc6a7f

Now my fieldToDivs is:

fieldsToDivs :: FormField sub y a -> Form sub y a
fieldsToDivs = mapFormXml $ mapM_ go
  where
    go fi = do
        let cls = string $ if fiRequired 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$
|]

-matt

On Thu, Oct 14, 2010 at 1:46 PM, Matt Brown <matt at softmechanics.net> wrote:
> 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