yesodcsrf-protectionhamletyesod-forms

How generate a token widget on Yesod?


I have the next problem with Yesod. I want to make my own form on my HTML because I want my own tags and labels, but the problem is how can I do that if my form is autogenerated with the next code:

entryForm :: Form Post
entryForm = renderDivs $ Post
    <$> areq   textField "name" Nothing
    <*> areq   textField "text" Nothing
    <*> areq   timeField "hour" Nothing

My handler is:

getPostNewR :: Handler RepHtml
getPostNewR = do
    (postWidget, enctype) <- generateFormPost entryForm
    defaultLayout $ do
        $(widgetFile "post_new")

And my post_new.hamlet file is this:

<form method=post enctype=#{enctype}>
^{postWidget}
<div>
    <input type=submit value="Create Post">

I want my form look on this way:

<form method="post" id="form_new_post" action="/post/new_do">
    <h4>Name:</h4>
    <div class="form-group">
        <textarea name="name" id="post_name" class="form-control" autofocus="autofocus"></textarea>
    </div>
    <h4>Text:</h4>
    <div class="form-group">
        <textarea name="text" id="post_text" class="form-control"></textarea>
    </div>
    <input type=submit value="Create Post">
</form>

But that need a tag with the token like this:

<input type="hidden" name="_token" value="ETjYBzdwQ1">

How I generate this token value or how can I change my entryForm for do that?

Thanks for your time and I really appreciate your help


Solution

  • You can use getRequest to get a YesodRequest, and then reqToken to get the token itself, e.g. fmap reqToken getRequest.