评估Yesod中的checkBoxesFieldList

时间:2015-01-16 16:35:23

标签: forms haskell yesod

我正在努力评估我的Yesod申请中的checkBoxesFieldList一元形式。

我正在使用这些来创建n - 问题ncheckBoxesFieldList的数量,有4个答案(仅True / False)每。但与应用形式相比,这些已经“评估”了结果并告诉我是否正确回答。

如果有未选中的框,或者您检查了四个中的错误,则会出现问题,因此我无法确定您选中的错误答案。

示例:

  • 如果我只检查了第一个框,则结果为[False]
  • 如果我仅检查第二个框错误,则结果仍为[False]
  • 如果我只检查了第三个框(但这是正确答案),结果为[True]。

我想要实现的目标:runFormPost的结果应该基于[Bool]checked -> return True给我一个unchecked -> return False

示例:

如果我只检查了第一个框错误,则结果应为[True, False, False, False]

我已经不得不从库中自定义checkboxesField,但我找不到删除评估的方法。

以下是代码:

data Question = Question {
   , identity   :: Text
   , content    :: Text
   , answerList :: [Answer]
   , maxScore   :: Int
  }

data Answer = Answer {
   , identity  :: Text
   , content   :: Text
   , isCorrect :: Bool
   , hint      :: Text
  }

zipAnswers :: [Answer] -> [(Text, Bool)]
zipAnswers [] = []
zipAnswers ((Answer _ text val _):xs) = (text, val):zipAnswers xs

listEditMForm :: [Question] -> Html -> MForm Handler (FormResult ([FormResult (Maybe [Bool])]), Widget)
listEditMForm xs token = do
      check_fields <- forM xs (\(Question _ content alist _ ) -> mopt (checkboxesFieldList' $ zipAnswers alist) (fromString $ T.unpack content) Nothing)
      let (check_results, check_views) = unzip check_fields
      let numerated_views = zip ([1..]::[Int]) check_views
      let widget = [whamlet|
              ^{token}
                <ul class="tabs">
                   $forall (c,view) <- numerated_views
                       <li>
                           <input type="radio" name="tabs" id="tab#{fvId view}">
                           <label for="tab#{fvId view}">Q #{show c}
                           <div id="tab-content#{fvId view}" class="tab-content animated fadeIn">
                             <p class=boldWhite> #{fvLabel view}: </p>
                             ^{fvInput view}
                             <br>
             <input class=button type=submit value="Testing">
        |]
      return ((FormSuccess check_results), widget)




checkboxesFieldList' :: (Eq a, RenderMessage site FormMessage, RenderMessage site msg) => [(msg, a)]
                     -> Field (HandlerT site IO) [a]
checkboxesFieldList' = checkboxesField' . optionsPairs



checkboxesField' :: (Eq a, RenderMessage site FormMessage)
                 => HandlerT site IO (OptionList a)
                 -> Field (HandlerT site IO) [a]
checkboxesField' ioptlist = (multiSelectField ioptlist)
    { fieldView =
        \theId name attrs val _ -> do
            opts <- fmap olOptions $ handlerToWidget ioptlist
            let optselected (Left _) _ = False
                optselected (Right vals) opt = (optionInternalValue opt) `elem` vals
            [whamlet|
                  <span ##{theId}>
                    $forall opt <- opts
                        <label>
                          <input type=checkbox name=#{name} value=#{optionExternalValue opt} *{attrs} :optselected val opt:checked>
                          <span class=simpleWhite> #{optionDisplay opt}
                |]
    }

0 个答案:

没有答案