我正在尝试使用email
和emailConfirmation
,或password
和passwordConfirmation
等字段的注册表单。验证email
和password
很简单,有规则,我已经编写了相应的功能。
另外两个更难。我找到了这个question并尝试编写我的代码:
表单定义,使用Blaze:
registrationForm :: (View Html) -> Html
registrationForm view = docTypeHtml $ do
form ! name "registration" ! method "post" ! action "/register" $ do
fieldset $ do
label ! for "password" $ (text "Password")
inputText "password" view
errorList "password" view
br
label ! for "passwordConfirmation" $ (text "Password Confirmation")
inputText "passwordConfirmation" view
errorList "passwordConfirmation" view
和验证器:
data Password = Password { password :: Text }
validateForm :: Monad m => Form Html m Password
validateForm =
Password
<$> "password" .: validatePassword
where
validatePassword =
validate fst' $ (,) <$> ("password" .: D.text Nothing)
<*> ("passwordConfirmation" .: D.text Nothing)
fst' (p1, p2) | p1 == p2 = Success p1
| otherwise = Error "Passwords must match"
但每当我运行服务器时,我都会收到一条消息“密码不是字段”。如果我删除了验证并给password
一个简单的验证,那么它会按预期工作。我在这里错过了什么吗?
答案 0 :(得分:1)
我在消化 - 仿函数存储库上得到了帮助。 Thanks cimmanon
这是最终的代码,注意字段名称现在是&#34;密码.p1&#34; /&#34;密码.p2&#34;而不只是密码/密码确认。
registrationForm :: (View Html) -> Html
registrationForm view = docTypeHtml $ do
form ! name "registration" ! method "post" ! action "/register" $ do
fieldset $ do
inputText "password.p1" view
br
inputText "password.p2" view
errorList "password" view
data Password = Password { password :: Text }
验证器是相同的,除了新名称:
validateForm :: Monad m => Form Html m Password
validateForm =
Password
<$> "password" .: validatePassword
where
validatePassword =
validate fst' $ (,) <$> ("p1" .: D.text Nothing)
<*> ("p2" .: D.text Nothing)
fst' (p1, p2) | p1 == p2 = Success p1
| otherwise = Error "Passwords must match"