|
@@ -1,117 +1,113 @@ |
1 |
1 |
|
package com.temp.client.foundation.widget; |
2 |
2 |
|
|
3 |
|
- |
import com.temp.shared.utils.ObjectUtils; |
4 |
|
- |
import com.temp.shared.utils.StringUtils; |
5 |
|
- |
import com.temp.shared.validators.EmailValidator; |
6 |
|
- |
import com.temp.shared.validators.PhoneValidator; |
7 |
|
- |
|
8 |
|
- |
import com.google.gwt.user.client.ui.Label; |
9 |
|
- |
import com.google.gwt.user.client.ui.ListBox; |
10 |
|
- |
import com.google.gwt.user.client.ui.PasswordTextBox; |
11 |
|
- |
import com.google.gwt.user.client.ui.TextBox; |
|
3 |
+ |
import com.google.gwt.user.client.ui.*; |
|
4 |
+ |
import com.temp.shared.utils.*; |
|
5 |
+ |
import com.temp.shared.validators.*; |
12 |
6 |
|
|
13 |
7 |
|
public class Form { |
14 |
|
- |
public static final String HTTPS_URL_REGEX = "^https\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\'\\/\\\\+&%\\$#_]*)?$"; |
|
8 |
+ |
public static final String HTTPS_URL_REGEX = |
|
9 |
+ |
"^https\\:\\/\\/[0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*(:(0-9)*)*(\\/?)([a-zA-Z0-9\\-\\.\\?\\,\\'\\/\\\\+&%\\$#_]*)?$"; |
15 |
10 |
|
|
16 |
|
- |
public static boolean somethingSelected(ListBox field) { |
17 |
|
- |
if (field instanceof OptionBox<?>) { |
|
11 |
+ |
public static boolean somethingSelected( ListBox field ) { |
|
12 |
+ |
if ( field instanceof OptionBox<?> ) { |
18 |
13 |
|
return (null != ((OptionBox<?>) field).getSelected()); |
19 |
14 |
|
} |
20 |
15 |
|
int selectedIndex = field.getSelectedIndex(); |
21 |
|
- |
if ((0 <= selectedIndex) && (selectedIndex < field.getItemCount())) { |
22 |
|
- |
String selectedItemText = StringUtils.noEmpty(field.getItemText(selectedIndex)); |
23 |
|
- |
if ((selectedItemText != null) && !"-".equals(selectedItemText)) { |
|
16 |
+ |
if ( (0 <= selectedIndex) && (selectedIndex < field.getItemCount()) ) { |
|
17 |
+ |
String selectedItemText = StringUtils.noEmpty( field.getItemText( selectedIndex ) ); |
|
18 |
+ |
if ( (selectedItemText != null) && !"-".equals( selectedItemText ) ) { |
24 |
19 |
|
return true; |
25 |
20 |
|
} |
26 |
21 |
|
} |
27 |
22 |
|
return false; |
28 |
23 |
|
} |
29 |
24 |
|
|
30 |
|
- |
public static boolean validateSomethingSelected(ListBox field, Label error) { |
31 |
|
- |
return updateError(error, somethingSelected(field)); |
|
25 |
+ |
public static boolean validateSomethingSelected( ListBox field, Label error ) { |
|
26 |
+ |
return updateError( error, somethingSelected( field ) ); |
32 |
27 |
|
} |
33 |
28 |
|
|
34 |
|
- |
public static boolean validatePhone(boolean required, TextBox field, Label error) { |
35 |
|
- |
String value = StringUtils.noEmpty(field.getText()); |
36 |
|
- |
boolean textOK = ((null == value) && !required) || PhoneValidator.INSTANCE.isValid(value); |
37 |
|
- |
return updateError(error, textOK); |
|
29 |
+ |
public static boolean validatePhone( boolean required, TextBox field, Label error ) { |
|
30 |
+ |
String value = StringUtils.noEmpty( field.getText() ); |
|
31 |
+ |
boolean textOK = ((null == value) && !required) || PhoneValidator.INSTANCE.isValid( value ); |
|
32 |
+ |
return updateError( error, textOK ); |
38 |
33 |
|
} |
39 |
34 |
|
|
40 |
|
- |
public static boolean validateEmail(boolean required, TextBox field, Label error) { |
41 |
|
- |
String value = StringUtils.noEmpty(field.getText()); |
42 |
|
- |
boolean textOK = ((null == value) && !required) || EmailValidator.INSTANCE.isValid(value); |
43 |
|
- |
return updateError(error, textOK); |
|
35 |
+ |
public static boolean validateEmail( boolean required, TextBox field, Label error ) { |
|
36 |
+ |
String value = StringUtils.noEmpty( field.getText() ); |
|
37 |
+ |
boolean textOK = ((null == value) && !required) || EmailValidator.INSTANCE.isValid( value ); |
|
38 |
+ |
return updateError( error, textOK ); |
44 |
39 |
|
} |
45 |
40 |
|
|
46 |
|
- |
public static boolean validatePasswords(boolean required, PasswordTextBox primeField, Label primeError, PasswordTextBox confirmField, Label confirmError) { |
47 |
|
- |
String prime = StringUtils.noEmpty(primeField.getText()); |
48 |
|
- |
String confirm = StringUtils.noEmpty(confirmField.getText()); |
|
41 |
+ |
public static boolean validatePasswords( boolean required, PasswordTextBox primeField, Label primeError, PasswordTextBox confirmField, Label confirmError ) { |
|
42 |
+ |
String prime = StringUtils.noEmpty( primeField.getText() ); |
|
43 |
+ |
String confirm = StringUtils.noEmpty( confirmField.getText() ); |
49 |
44 |
|
//if the passwords don't match bail |
50 |
|
- |
if(!updateError(confirmError, ObjectUtils.areEqual(prime, confirm))) |
|
45 |
+ |
if ( !updateError( confirmError, ObjectUtils.areEqual( prime, confirm ) ) ) { |
51 |
46 |
|
return false; |
52 |
|
- |
return updateError(primeError, !required || (prime != null)); |
|
47 |
+ |
} |
|
48 |
+ |
return updateError( primeError, !required || (prime != null) ); |
53 |
49 |
|
} |
54 |
50 |
|
|
55 |
|
- |
public static boolean validateTextIfNotEmptyMatches(TextBox field, String regEx, Label error) { |
56 |
|
- |
String value = StringUtils.noEmpty(field.getText()); |
57 |
|
- |
boolean textOK = (null == value) || value.matches(regEx); |
58 |
|
- |
return updateError(error, textOK); |
|
51 |
+ |
public static boolean validateTextIfNotEmptyMatches( TextBox field, String regEx, Label error ) { |
|
52 |
+ |
String value = StringUtils.noEmpty( field.getText() ); |
|
53 |
+ |
boolean textOK = (null == value) || value.matches( regEx ); |
|
54 |
+ |
return updateError( error, textOK ); |
59 |
55 |
|
} |
60 |
56 |
|
|
61 |
|
- |
public static boolean validateTextNotEmptyAndMatches(TextBox field, String regEx, Label error) { |
62 |
|
- |
String value = StringUtils.noEmpty(field.getText()); |
63 |
|
- |
boolean textOK = (null != value) && value.matches(regEx); |
64 |
|
- |
return updateError(error, textOK); |
|
57 |
+ |
public static boolean validateTextNotEmptyAndMatches( TextBox field, String regEx, Label error ) { |
|
58 |
+ |
String value = StringUtils.noEmpty( field.getText() ); |
|
59 |
+ |
boolean textOK = (null != value) && value.matches( regEx ); |
|
60 |
+ |
return updateError( error, textOK ); |
65 |
61 |
|
} |
66 |
62 |
|
|
67 |
|
- |
public static boolean validateTextNotEmpty(TextBox field, Label error) { |
68 |
|
- |
return validateTextNotEmpty(field.getText(), error); |
|
63 |
+ |
public static boolean validateTextNotEmpty( TextBox field, Label error ) { |
|
64 |
+ |
return validateTextNotEmpty( field.getText(), error ); |
69 |
65 |
|
} |
70 |
66 |
|
|
71 |
|
- |
public static boolean validateTextNotEmpty(String fieldText, Label error) { |
72 |
|
- |
boolean textOK = (null != StringUtils.noEmpty(fieldText)); |
73 |
|
- |
return updateError(error, textOK); |
|
67 |
+ |
public static boolean validateTextNotEmpty( String fieldText, Label error ) { |
|
68 |
+ |
boolean textOK = (null != StringUtils.noEmpty( fieldText )); |
|
69 |
+ |
return updateError( error, textOK ); |
74 |
70 |
|
} |
75 |
71 |
|
|
76 |
|
- |
public static boolean validateUrl(String url, Label error) { |
77 |
|
- |
boolean isValidUrl = !StringUtils.isBlank(url) && url.matches(HTTPS_URL_REGEX); |
78 |
|
- |
return updateError(error, isValidUrl); |
|
72 |
+ |
public static boolean validateUrl( String url, Label error ) { |
|
73 |
+ |
boolean isValidUrl = !StringUtils.isBlank( url ) && url.matches( HTTPS_URL_REGEX ); |
|
74 |
+ |
return updateError( error, isValidUrl ); |
79 |
75 |
|
} |
80 |
76 |
|
|
81 |
|
- |
public static void hideErrorIfNotEmpty(Object toCheck, Label error) { |
82 |
|
- |
if ((toCheck == null) || ((toCheck instanceof String) && StringUtils.isBlank(toCheck.toString()))) { |
|
77 |
+ |
public static void hideErrorIfNotEmpty( Object toCheck, Label error ) { |
|
78 |
+ |
if ( (toCheck == null) || ((toCheck instanceof String) && StringUtils.isBlank( toCheck.toString() )) ) { |
83 |
79 |
|
return; |
84 |
80 |
|
} |
85 |
|
- |
showError(error, false); |
|
81 |
+ |
showError( error, false ); |
86 |
82 |
|
} |
87 |
83 |
|
|
88 |
|
- |
public static boolean updateError(Label error, boolean fieldOK) { |
89 |
|
- |
showError(error, !fieldOK); |
|
84 |
+ |
public static boolean updateError( Label error, boolean fieldOK ) { |
|
85 |
+ |
showError( error, !fieldOK ); |
90 |
86 |
|
return fieldOK; |
91 |
87 |
|
} |
92 |
88 |
|
|
93 |
|
- |
public static void hideErrors(Label... errors) { |
94 |
|
- |
if (errors != null) { |
95 |
|
- |
for (Label error : errors) { |
96 |
|
- |
showError(error, false); |
|
89 |
+ |
public static void hideErrors( Label... errors ) { |
|
90 |
+ |
if ( errors != null ) { |
|
91 |
+ |
for ( Label error : errors ) { |
|
92 |
+ |
showError( error, false ); |
97 |
93 |
|
} |
98 |
94 |
|
} |
99 |
95 |
|
} |
100 |
96 |
|
|
101 |
|
- |
public static void showError(Label error, boolean showIt) { |
102 |
|
- |
showLabel(error, showIt); |
|
97 |
+ |
public static void showError( Label error, boolean showIt ) { |
|
98 |
+ |
showLabel( error, showIt ); |
103 |
99 |
|
} |
104 |
100 |
|
|
105 |
|
- |
public static void updateLabel(Label label, String text, boolean showIt) { |
106 |
|
- |
if (label != null) { |
107 |
|
- |
label.setText(StringUtils.deNull(text).trim()); |
108 |
|
- |
showLabel(label, showIt); |
|
101 |
+ |
public static void updateLabel( Label label, String text, boolean showIt ) { |
|
102 |
+ |
if ( label != null ) { |
|
103 |
+ |
label.setText( StringUtils.deNull( text ).trim() ); |
|
104 |
+ |
showLabel( label, showIt ); |
109 |
105 |
|
} |
110 |
106 |
|
} |
111 |
107 |
|
|
112 |
|
- |
public static void showLabel(Label label, boolean showIt) { |
113 |
|
- |
if (label != null) { |
114 |
|
- |
label.setVisible(showIt); |
|
108 |
+ |
public static void showLabel( Label label, boolean showIt ) { |
|
109 |
+ |
if ( label != null ) { |
|
110 |
+ |
label.setVisible( showIt ); |
115 |
111 |
|
} |
116 |
112 |
|
} |
117 |
113 |
|
} |