Validation spanning multiple fields

旧城冷巷雨未停 提交于 2019-12-08 19:17:54

问题


I'm trying to grok applicative forms, and I've been wondering how to implement a form that validates fields that depend on other fields. For example a registration form which has password and confirm_password fields and I'd like to validate that password == confirm_password.

I could be done after the form has ran, in the handler, but that would mean losing error messages.

Edit: Forgot to mention, I'm mainly using Yesods applicative forms, but they seem to be quite close to digestive-functors


回答1:


What type of form system are you using? You can easily do this with digestive-functors, here's an example of one of my registration forms:

registrationForm =
    Registration
      <$> "username" .: text Nothing
      <*> "password" .: passwordConfirmer
  where passwordConfirmer =
          validate fst' $ (,) <$> ("p1" .: text Nothing)
                              <*> ("p2" .: text Nothing)
        fst' (p1, p2) | p1 == p2  = Success p1
                      | otherwise = Error "Passwords must match"

Here you can see I generate a value for my 'password' field by using my passwordConfirmer form field. This field uses 2 text fields and puts them into a tuple, but after validation it just takes the fst element (though it could take snd, we've guaranteed they are equal!).

My Registration type is:

data Registration = Registration
    { regUserName :: Text
    , regPassword :: Text
    }


来源:https://stackoverflow.com/questions/12106964/validation-spanning-multiple-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!