0

我的模型中有:

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

但这似乎不起作用。有任何想法吗?

4

1 回答 1

1

rawSql does not return a single value as a string. Instead, you can use Persistent to actually look up the email and then decide.

I don't know why your userId is a Maybe. Why would you have just an email ID and not a corresponding userID? Anyways, I am going to assume it is a required field to keep things simple.

Something like (untested):

 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
于 2013-11-10T18:14:47.790 回答