Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Darya Rednikina
elm_frontend
Commits
c48341ef
Commit
c48341ef
authored
Dec 13, 2019
by
Darya Rednikina
Browse files
added validation for registration. didn't test it
parent
37b3f55a
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
index.html
View file @
c48341ef
This diff is collapsed.
Click to expand it.
src/Register.elm
View file @
c48341ef
...
...
@@ -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
]
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment