Commit c48341ef authored by Darya Rednikina's avatar Darya Rednikina
Browse files

added validation for registration. didn't test it

parent 37b3f55a
This diff is collapsed.
......@@ -4,6 +4,7 @@ import Browser exposing (..)
import Html exposing (..)
import Html.Attributes exposing (..)
import Html.Events exposing (..)
import List exposing (..)
import Route exposing (..)
......@@ -18,7 +19,7 @@ type alias Model =
type Problem
= InvalidEntry String
= InvalidEntry ValidatedField String
| ServerError String
......@@ -69,6 +70,18 @@ update msg model =
EnteredName name ->
updateForm (\form -> { form | name = name }) model
SubmittedForm ->
case validate model.form of
Ok validForm ->
( { model | problems = [] }
, Cmd.none
)
Err problems ->
( { model | problems = problems }
, Cmd.none
)
_ ->
( model, Cmd.none )
......@@ -135,11 +148,122 @@ view model =
[ a [ Route.href Route.Login ]
[ text "Already have an account?" ]
]
-- , ul [ class "error-messages" ]
-- (List.map viewProblem model.problems)
, ul [ class "error-messages" ]
(List.map viewProblem model.problems)
, viewForm model.form
]
]
]
]
-- VALIDATION
{-| to not send unvalidated fields
-}
type TrimmedForm
= Trimmed Form
type ValidatedField
= Email
| Password
| Login
| Name
fieldsToValidate : List ValidatedField
fieldsToValidate =
[ Email
, Password
, Login
, Name
]
trimFields : Form -> TrimmedForm
trimFields form =
Trimmed
{ email = String.trim form.email
, password = String.trim form.password
, login = String.trim form.login
, name = String.trim form.name
}
{-| Trim the form and validate its fields.
-}
validate : Form -> Result (List Problem) TrimmedForm
validate form =
let
trimmedForm =
trimFields form
in
case List.concatMap (validateField trimmedForm) fieldsToValidate of
[] ->
Ok trimmedForm
problems ->
Err problems
{-| Checks all the fields for errors.
-}
validateField : TrimmedForm -> ValidatedField -> List Problem
validateField (Trimmed form) field =
let
emptyValidation str msg =
if String.isEmpty str then
[ msg ]
else
[]
lengthValidation : String -> (Int -> Bool) -> String -> List String
lengthValidation str p msg =
if p (String.length str) then
[]
else
[ msg ]
passwordValidation : String -> List String
passwordValidation pswd =
let
errEmpty =
emptyValidation pswd "password can't be empty."
errLength =
lengthValidation pswd (\x -> x > 8) "password must be > 8."
in
List.append errEmpty errLength
in
List.map (InvalidEntry field) <|
case field of
Email ->
emptyValidation form.email "email can't be blank."
Password ->
passwordValidation form.password
Login ->
emptyValidation form.login "login can't be blank."
Name ->
emptyValidation form.name "name can't be blank."
viewProblem : Problem -> Html msg
viewProblem problem =
let
errorMessage =
case problem of
InvalidEntry _ msg ->
"вв"
ServerError str ->
str
in
li [] [ text errorMessage ]
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment