ตรวจสอบฟิลด์จากฐานข้อมูล Yesod

สวัสดี ฉันต้องการสร้างไฟล์ที่ยอมรับเฉพาะค่าที่มีอยู่ในตาราง (มีมากกว่า 20,000 แถว) ดังนั้นฉันจึงมีรหัสต่อไปนี้

demoForm :: Maybe Demo -> AForm Handler Demo
demoForm   demo = Demo 
                <$> areq nitField (bfs (MsgName)) (demoFieldOne <$> demo)
                <*> areq intField (bfs (MsgName)) (demoFieldTwo <$> demo)

           where 
             errorMessage :: Text
             errorMessage = "the company no exist!"                 

             nitField = check validateNit textField

             validateNit nit
                | companiesMatch nit  = Left errorMessage
                | otherwise = Right nit

             companiesMatch name = do
                  entities <- runDB $ selectList [CompanyName ==. name] []
                  return (null entities)

แต่ฉันได้รับข้อผิดพลาด Couldn't match expected type ‘Bool’with actual type ‘m0 (HandlerT site0 IO Bool)’ ดังนั้นจะรับค่าบูลจาก monad ได้อย่างไร หรือมีวิธีที่ดีกว่าในการตรวจสอบความถูกต้องนี้


person oriaj    schedule 21.10.2016    source แหล่งที่มา
comment
ฉันคิดว่าคุณต้องการใช้ฟังก์ชัน checkM แทน   -  person Michael Snoyman    schedule 23.10.2016


คำตอบ (1)


ขอบคุณ @Michael Snoyman คุณพูดถูก ฉันแค่ต้องใช้ checkM

demoForm :: Maybe Demo -> AForm Handler Demo
demoForm   demo = Demo 
                <$> areq nitField (bfs (MsgName)) (demoFieldOne <$> demo)
                <*> areq intField (bfs (MsgName)) (demoFieldTwo <$> demo)
           where 
            nitField = checkM validateNit textField
            validateNit input = do
              mbNit <- runDB $ selectList [CompanyName ==. input] []
              return $ case null mbNit of
                True  -> Left (MsgErrNit :: AppMessage)
                False -> Right input          
person oriaj    schedule 24.10.2016