@@ -9,33 +9,50 @@ class FormObject
9
9
10
10
attr_accessor :email , :password , :confirm_password
11
11
12
- validates :email , presence : true
12
+ validates :email , presence : true , email_format : { message : "formatted incorrectly" }
13
13
validates :password , presence : true , length : { minimum : 8 , maximum : 64 }
14
- validates :confirm_password , presence : true , comparison : { equal_to : -> { password } }
14
+ validates :confirm_password , presence : true
15
+ validate :ensure_confirm_password_equals_password
16
+
17
+ validate :ensure_no_user_with_details # Must run last for security
18
+
19
+ def ensure_no_user_with_details
20
+ return unless errors . blank?
21
+
22
+ if User . exists_with_email? ( email )
23
+ errors . add :email , :email_used , message : "is already taken"
24
+ end
25
+ end
26
+
27
+ def ensure_confirm_password_equals_password
28
+ if confirm_password != password
29
+ errors . add :confirm_password , :not_password , message : "must match password"
30
+ end
31
+ end
15
32
end
16
33
17
- class View
34
+ class View < ApplicationView
18
35
def template
19
36
h1 { "Create a user" }
20
37
21
38
render Form
22
39
end
23
40
end
24
41
25
- class Form
42
+ class Form < ApplicationView
26
43
def template
27
- form_with ( url : authentication_register_path , id : "register" ) do |f |
44
+ form_with ( model : ViewContext . form_object , url : authentication_register_path , id : "register" ) do |f |
28
45
f . label :email , "Email"
29
- f . text_field :email , value : @submission [ :email ]
30
- render @submission . errors_for ( :email )
46
+ f . text_field :email
47
+ render ViewContext . form_object . errors_for ( :email )
31
48
32
49
f . label :password , "Password"
33
50
f . password_field :password
34
- render @submission . errors_for ( :password )
51
+ render ViewContext . form_object . errors_for ( :password )
35
52
36
53
f . label :confirm_password , "Confirm Password"
37
54
f . password_field :confirm_password
38
- render @submission . errors_for ( :confirm_password )
55
+ render ViewContext . form_object . errors_for ( :confirm_password )
39
56
40
57
f . submit "Register"
41
58
end
@@ -53,11 +70,8 @@ def view
53
70
def submit
54
71
ViewContext . form_object = FormObject . new ( user_params )
55
72
56
- if ViewContext . form_object . validations_passed?
57
- end
58
-
59
- if ViewContext . form_object . validations_passed?
60
- user = User . create! ( email : submission [ :email ] , password : submission [ :password ] )
73
+ if ViewContext . form_object . valid?
74
+ user = User . create! ( email : ViewContext . form_object . password , password : ViewContext . form_object . password )
61
75
62
76
user . add_to_session session
63
77
@@ -72,7 +86,7 @@ def submit
72
86
end
73
87
74
88
def user_params
75
- params . permit (
89
+ params . require ( :authentication_pages_register_controller_form_object ) . permit (
76
90
:email ,
77
91
:password ,
78
92
:confirm_password
0 commit comments