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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
package com.temp.client.foundation.widget;
import com.temp.shared.utils.ObjectUtils;
import com.temp.shared.utils.StringUtils;
import com.temp.shared.validators.EmailValidator;
import com.temp.shared.validators.PhoneValidator;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
public class Form {
public static final String HTTPS_URL_REGEX = "^https\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\'\\/\\\\+&%\\$#_]*)?$";
public static boolean somethingSelected(ListBox field) {
if (field instanceof OptionBox<?>) {
return (null != ((OptionBox<?>) field).getSelected());
}
int selectedIndex = field.getSelectedIndex();
if ((0 <= selectedIndex) && (selectedIndex < field.getItemCount())) {
String selectedItemText = StringUtils.noEmpty(field.getItemText(selectedIndex));
if ((selectedItemText != null) && !"-".equals(selectedItemText)) {
return true;
}
}
return false;
}
public static boolean validateSomethingSelected(ListBox field, Label error) {
return updateError(error, somethingSelected(field));
}
public static boolean validatePhone(boolean required, TextBox field, Label error) {
String value = StringUtils.noEmpty(field.getText());
boolean textOK = ((null == value) && !required) || PhoneValidator.INSTANCE.isValid(value);
return updateError(error, textOK);
}
public static boolean validateEmail(boolean required, TextBox field, Label error) {
String value = StringUtils.noEmpty(field.getText());
boolean textOK = ((null == value) && !required) || EmailValidator.INSTANCE.isValid(value);
return updateError(error, textOK);
}
public static boolean validatePasswords(boolean required, PasswordTextBox primeField, Label primeError, PasswordTextBox confirmField, Label confirmError) {
String prime = StringUtils.noEmpty(primeField.getText());
String confirm = StringUtils.noEmpty(confirmField.getText());
//if the passwords don't match bail
if(!updateError(confirmError, ObjectUtils.areEqual(prime, confirm)))
return false;
return updateError(primeError, !required || (prime != null));
}
public static boolean validateTextIfNotEmptyMatches(TextBox field, String regEx, Label error) {
String value = StringUtils.noEmpty(field.getText());
boolean textOK = (null == value) || value.matches(regEx);
return updateError(error, textOK);
}
public static boolean validateTextNotEmptyAndMatches(TextBox field, String regEx, Label error) {
String value = StringUtils.noEmpty(field.getText());
boolean textOK = (null != value) && value.matches(regEx);
return updateError(error, textOK);
}
public static boolean validateTextNotEmpty(TextBox field, Label error) {
return validateTextNotEmpty(field.getText(), error);
}
public static boolean validateTextNotEmpty(String fieldText, Label error) {
boolean textOK = (null != StringUtils.noEmpty(fieldText));
return updateError(error, textOK);
}
public static boolean validateUrl(String url, Label error) {
boolean isValidUrl = !StringUtils.isBlank(url) && url.matches(HTTPS_URL_REGEX);
return updateError(error, isValidUrl);
}
public static void hideErrorIfNotEmpty(Object toCheck, Label error) {
if ((toCheck == null) || ((toCheck instanceof String) && StringUtils.isBlank(toCheck.toString()))) {
return;
}
showError(error, false);
}
public static boolean updateError(Label error, boolean fieldOK) {
showError(error, !fieldOK);
return fieldOK;
}
public static void hideErrors(Label... errors) {
if (errors != null) {
for (Label error : errors) {
showError(error, false);
}
}
}
public static void showError(Label error, boolean showIt) {
showLabel(error, showIt);
}
public static void updateLabel(Label label, String text, boolean showIt) {
if (label != null) {
label.setText(StringUtils.deNull(text).trim());
showLabel(label, showIt);
}
}
public static void showLabel(Label label, boolean showIt) {
if (label != null) {
label.setVisible(showIt);
}
}
}
|