-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathUserData.java
More file actions
102 lines (75 loc) · 2.53 KB
/
UserData.java
File metadata and controls
102 lines (75 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package controllers.forms;
import play.data.validation.Constraints;
import play.data.validation.ValidationError;
import java.util.ArrayList;
import java.util.List;
/**
* Example of validation of the form
*/
@Constraints.Validate
public class UserData implements Constraints.Validatable<List<ValidationError>> {
protected String inputEmail;
;
protected String password1;
protected String password2;
protected Integer age;
public String getInputEmail() {
return inputEmail;
}
public void setInputEmail(String inputEmail) {
this.inputEmail = inputEmail;
}
public String getPassword1() {
return password1;
}
public void setPassword1(String password1) {
this.password1 = password1;
}
public String getPassword2() {
return password2;
}
public void setPassword2(String password2) {
this.password2 = password2;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "UserData{" +
"inputEmail='" + inputEmail + '\'' +
", password1='" + password1 + '\'' +
", password2='" + password2 + '\'' +
", age=" + age +
'}';
}
@Override
public List<ValidationError> validate() {
// keep all errors in this List
final List<ValidationError> errors = new ArrayList<>();
if (!inputEmail.contains("@")) {
errors.add(new ValidationError("inputEmail", "Email must contain '@' character."));
}
if (inputEmail.isBlank()){
errors.add(new ValidationError("inputEmail", "Email cannot be blank."));
}
if (password1.isBlank() || password2.isBlank() || password1.length()<=3 || password2.length()<=3) {
errors.add(new ValidationError("password2", "Passwords must be longer than 3 characters and cannot be blank."));
}
if (!password1.equals(password2)) {
errors.add(new ValidationError("password2", "Both passwords must be the same."));
}
// age must be between 1-99
if (age==null || age < 1 || age > 99){
errors.add(new ValidationError("age", "Age must be between 1-99."));
}
if (!errors.isEmpty()){
// Also add a global error:
errors.add(new ValidationError("", "Form could not be submitted."));
}
return (errors.isEmpty()) ? null : errors;
}
}