Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grouped checkboxes in Haskell/Yesod

I am currently developing with Haskell on the Yesod web platform and I need to create a form in one of my webpages. This form must contain several checkboxes, but they need to be grouped into categories. For instance, the user would need to select 3 disciplines in a list of 10 checkboxes, 5 items and of those a maximum of 2 can be weapons.

I have read parts of the book on the Yesod website, and from my understanding I would need to create a custom field to create grouped checkboxes. Unfortunately, I am not very good with Haskell (have just started using it 1-2 weeks ago) and I have some difficulties understanding how I could achieve this. For reference, this is the page that I have read of the book on Yesod's website that talks about custom fields: http://www.yesodweb.com/book/forms

Knowing that, my question would be: how do I create grouped checkboxes in Haskell/Yesod?

EDIT: In the link I have provided, I understand the last two values of the constructor of Field (fieldView and fieldEnctype), it is the first one (fieldParse) that I do not know how to modify for my purpose. Here is the example in the link that I was referencing:

passwordConfirmField :: Field Handler Text
passwordConfirmField = Field
    { fieldParse = \rawVals _fileVals ->
        case rawVals of
            [a, b]
                | a == b -> return $ Right $ Just a
                | otherwise -> return $ Left "Passwords don't match"
            [] -> return $ Right Nothing
            _ -> return $ Left "You must enter two values"
    , fieldView = \idAttr nameAttr otherAttrs eResult isReq ->
        [whamlet|
            <input id=#{idAttr} name=#{nameAttr} *{otherAttrs} type=password>
            <div>Confirm:
            <input id=#{idAttr}-confirm name=#{nameAttr} *{otherAttrs} type=password>
        |]
    , fieldEnctype = UrlEncoded
    }
like image 301
Choub890 Avatar asked Nov 08 '13 22:11

Choub890


1 Answers

So after much research, I have realized that in my case, in order to return the values of the checkboxes that were checked, i need to do:

fieldParse  = \rawVals _fileVals ->
        return $ Right $ Just rawVals

Since rawVals contains a list of Text of all the values of the checkboxes that were checked.

like image 74
Choub890 Avatar answered Oct 04 '22 14:10

Choub890