Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
V
VenJob
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mai Hoang Thai Ha
VenJob
Commits
be7ba250
Commit
be7ba250
authored
Aug 16, 2021
by
Mai Hoang Thai Ha
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add script to validate form
parent
24d4089f
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
107 additions
and
9 deletions
+107
-9
app/assets/stylesheets/applies.scss
+10
-0
app/javascript/packs/application.js
+1
-0
app/javascript/packs/validation.js
+78
-0
app/views/applies/_ribbon.html.slim
+0
-4
app/views/applies/new.html.slim
+18
-5
No files found.
app/assets/stylesheets/applies.scss
View file @
be7ba250
...
...
@@ -82,3 +82,12 @@ $text-color-white: #fff;
.btn-height
{
height
:
50px
;
}
.invalid
{
.form-msg
{
color
:
#f33a58
;
}
input
{
border-color
:
#f33a58
;
}
}
\ No newline at end of file
app/javascript/packs/application.js
View file @
be7ba250
...
...
@@ -15,3 +15,4 @@ ActiveStorage.start()
import
"bootstrap"
window
.
bootstrap
=
require
(
"bootstrap"
);
import
"../stylesheets/application.scss"
;
app/javascript/packs/validation.js
0 → 100644
View file @
be7ba250
Validator
=
function
(
options
)
{
var
selectorRules
=
{};
// validate
function
validate
(
inputElement
,
rule
)
{
var
errorMessage
;
var
errorElement
=
inputElement
.
parentElement
.
querySelector
(
options
.
errorSelector
);
// get rules of selector
var
rules
=
selectorRules
[
rule
.
selector
];
// stop if error
for
(
var
i
=
0
;
i
<
rules
.
length
;
++
i
)
{
errorMessage
=
rules
[
i
](
inputElement
.
value
)
if
(
errorMessage
)
break
;
}
if
(
errorMessage
)
{
errorElement
.
innerText
=
errorMessage
inputElement
.
parentElement
.
classList
.
add
(
'invalid'
)
}
else
{
errorElement
.
innerText
=
''
inputElement
.
parentElement
.
classList
.
remove
(
'invalid'
)
}
}
// get Element
var
formElement
=
document
.
querySelector
(
options
.
form
)
if
(
formElement
)
{
options
.
rules
.
forEach
(
function
(
rule
){
// save rules
if
(
Array
.
isArray
(
selectorRules
[
rule
.
selector
]))
{
selectorRules
[
rule
.
selector
].
push
(
rule
.
test
)
}
else
{
selectorRules
[
rule
.
selector
]
=
[
rule
.
test
];
}
var
inputElement
=
formElement
.
querySelector
(
rule
.
selector
);
if
(
inputElement
)
{
// blur
inputElement
.
onblur
=
function
()
{
validate
(
inputElement
,
rule
)
}
// when input
inputElement
.
oninput
=
function
()
{
var
errorElement
=
inputElement
.
parentElement
.
querySelector
(
options
.
errorSelector
)
errorElement
.
innerText
=
''
inputElement
.
parentElement
.
classList
.
remove
(
'invalid'
)
}
}
});
}
}
Validator
.
isRequired
=
function
(
selector
)
{
return
{
selector
:
selector
,
test
:
function
(
value
)
{
return
value
.
trim
()
?
undefined
:
"This field can't be blank"
}
}
}
Validator
.
isEmail
=
function
(
selector
)
{
return
{
selector
:
selector
,
test
:
function
(
value
)
{
var
regex
=
/^
(([^
<>()[
\]\\
.,;:
\s
@"
]
+
(\.[^
<>()[
\]\\
.,;:
\s
@"
]
+
)
*
)
|
(
".+"
))
@
((\[[
0-9
]{1,3}\.[
0-9
]{1,3}\.[
0-9
]{1,3}\.[
0-9
]{1,3}\])
|
(([
a-zA-Z
\-
0-9
]
+
\.)
+
[
a-zA-Z
]{2,}))
$/
;
return
regex
.
test
(
value
)
?
undefined
:
'Email invalid'
;
}
}
}
app/views/applies/_ribbon.html.slim
View file @
be7ba250
...
...
@@ -12,6 +12,3 @@
span
.circle
|
3
|
Done
\ No newline at end of file
app/views/applies/new.html.slim
View file @
be7ba250
...
...
@@ -7,19 +7,21 @@
p
.fs-5.fw-bold.mb-4.text-center
=
@job
.
title
.col
=
form_with
(
model:
@apply
,
url:
confirm_path
,
local:
true
)
do
|
f
|
=
form_with
(
model:
@apply
,
url:
confirm_path
,
local:
true
,
id:
'apply-form'
)
do
|
f
|
=
render
'shared/error_messages'
,
object:
f
.
object
.row.mb-5
.row.mb-5
.form-group
=
f
.
hidden_field
:job_id
,
value:
@job
.
id
.col-2
=
f
.
label
:user_name
,
'Full name'
,
class:
'form-label label'
.col-10
=
f
.
text_field
:user_name
,
class:
'form-control'
.row.mb-5
=
f
.
text_field
:user_name
,
value:
@current_user
.
name
,
class:
'form-control'
span
.form-msg
.row.mb-5.form-group
.col-2
=
f
.
label
:email
,
class:
'form-label label'
.col-10
=
f
.
text_field
:email
,
class:
'form-control'
=
f
.
text_field
:email
,
value:
@current_user
.
email
,
class:
'form-control'
span
.form-msg
.row.mb-5
/ - if @user.cv.attached?
/ = url_for(@user.cv)
...
...
@@ -30,3 +32,14 @@
=
f
.
submit
'Confirm'
,
class:
'btn btn-primary w-25 my-4 btn-height'
=
render
'validate_form'
=
javascript_pack_tag
'validation'
javascript:
Validator
({
form
:
'#apply-form'
,
errorSelector
:
'.form-msg'
,
rules
:
[
Validator
.
isRequired
(
'#apply_job_user_name'
),
Validator
.
isRequired
(
'#apply_job_email'
),
Validator
.
isEmail
(
'#apply_job_email'
)
]
})
Write
Preview
Markdown
is supported
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