我的模特中有:
Email
email Text
user UserId Maybe
verkey Text Maybe
UniqueEmail email
我有一个表单,用户可以输入电子邮件(现有用户)和金额(int)。基于此电子邮件,我想找到该用户的UserId。我有声音:
paymentForm :: Form Payment
paymentForm = renderDivs $ Payment
<$> lift (liftIO getCurrentTime)
<*> pure userId
<*> areq userIdField "To" Nothing
<*> areq intField "Amount" Nothing
where
message :: Text
message = "This user doesnt exist"
userIdField = check hisUserId emailField
hisUserId x | rawSql (SELECT user FROM "Email" WHERE "Email".email = x) == "" = Left message
| otherwise = Right x
但这似乎不起作用。有什么想法吗?
答案 0 :(得分:1)
rawSql不会将单个值作为字符串返回。相反,您可以使用Persistent实际查找电子邮件然后再决定。
我不知道为什么你的userId是一个Maybe。为什么你只有一个电子邮件ID而不是相应的用户ID?无论如何,我将假设这是一个必须保持简单的领域。
像(未经测试)的东西:
hisUserId :: Text -> Bool
hisUserId email = do
res <- runDB $ selectList [EmailEmail ==. email] []
user <- entityVal res
case user of
Just u -> return True
-- if you really want to treat user as a Maybe then
-- you can get it using (emailUser u) and then check for its existence
Nothing -> return False