Skip to content

Commit ea591c9

Browse files
89: Changed form field validation, added possibility to add validation through annotations
1 parent 6ad2227 commit ea591c9

File tree

11 files changed

+368
-252
lines changed

11 files changed

+368
-252
lines changed

resources/magento2/validation.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
validator.notEmpty={0} must not be empty
1+
validator.notEmpty=The `{0}` field must not be empty
22
validator.package.validPath=Please specify a valid Magento 2 installation path
33
validator.alphaNumericCharacters={0} must contain letters and numbers only
44
validator.alreadyDeclared={0} is already declared in the {1} module.

src/com/magento/idea/magento2plugin/actions/generation/dialog/AbstractDialog.java

Lines changed: 135 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,34 @@
55

66
package com.magento.idea.magento2plugin.actions.generation.dialog;
77

8+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation;
9+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidations;
810
import com.magento.idea.magento2plugin.bundles.CommonBundle;
911
import java.awt.Dimension;
1012
import java.awt.Toolkit;
11-
import javax.swing.JDialog;
13+
import java.lang.reflect.Field;
14+
import java.lang.reflect.InvocationTargetException;
15+
import java.util.*;
16+
import javax.swing.*;
17+
18+
import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.ValidationRule;
19+
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
1220

1321
/**
1422
* All code generate dialog should extend this class.
1523
*/
1624
@SuppressWarnings({"PMD.ShortVariable", "PMD.MissingSerialVersionUID"})
1725
public abstract class AbstractDialog extends JDialog {
18-
1926
protected CommonBundle bundle;
27+
protected final ValidatorBundle validatorBundle = new ValidatorBundle();
28+
private final String errorTitle;
29+
private final Map<Object, List<ValidationRule>> textFieldValidationRuleMap = new LinkedHashMap<>();
30+
private final Map<Object, Map<ValidationRule, String>> errorMessageFieldValidationRuleMap = new HashMap<>();
2031

2132
public AbstractDialog() {
2233
super();
23-
this.bundle = new CommonBundle();
34+
bundle = new CommonBundle();
35+
errorTitle = bundle.message("common.error");
2436
}
2537

2638
protected void centerDialog(final AbstractDialog dialog) {
@@ -33,4 +45,124 @@ protected void centerDialog(final AbstractDialog dialog) {
3345
protected void onCancel() {
3446
this.setVisible(false);
3547
}
48+
49+
protected boolean validateFormFields() {
50+
addValidationRulesFromAnnotations();
51+
for (Map.Entry<Object, List<ValidationRule>> entry : textFieldValidationRuleMap.entrySet()) {
52+
Object field = entry.getKey();
53+
List<ValidationRule> rules = entry.getValue();
54+
55+
for (ValidationRule rule : rules) {
56+
String value = resolveFieldValueByComponentType(field);
57+
58+
if (value != null && !rule.check(value)) {
59+
if (errorMessageFieldValidationRuleMap.containsKey(field)
60+
&& errorMessageFieldValidationRuleMap.get(field).containsKey(rule)) {
61+
showErrorMessage(errorMessageFieldValidationRuleMap.get(field).get(rule));
62+
}
63+
return false;
64+
}
65+
}
66+
}
67+
return true;
68+
}
69+
70+
protected void showErrorMessage(String errorMessage) {
71+
JOptionPane.showMessageDialog(
72+
null,
73+
errorMessage,
74+
errorTitle,
75+
JOptionPane.ERROR_MESSAGE
76+
);
77+
}
78+
79+
private void addValidationRulesFromAnnotations() {
80+
Class<?> type = this.getClass();
81+
for (Field field : type.getDeclaredFields()) {
82+
field.setAccessible(true);
83+
List<FieldValidation> validations = new LinkedList<>();
84+
85+
if (field.isAnnotationPresent(FieldValidation.class)) {
86+
validations.add(field.getAnnotation(FieldValidation.class));
87+
}
88+
if (field.isAnnotationPresent(FieldValidations.class)) {
89+
validations.addAll(Arrays.asList(field.getAnnotation(FieldValidations.class).value()));
90+
}
91+
92+
for (FieldValidation validation : validations) {
93+
try {
94+
addValidationRuleToField(
95+
field.get(this),
96+
getRuleFromAnnotation(validation),
97+
getMessageFromAnnotation(validation)
98+
);
99+
} catch (Exception exception) {
100+
// Do nothing.
101+
}
102+
}
103+
field.setAccessible(false);
104+
}
105+
}
106+
107+
private String getMessageFromAnnotation(FieldValidation validation) {
108+
String[] params;
109+
if (validation.message().length > 1) {
110+
params = Arrays.copyOfRange(validation.message(), 1, validation.message().length);
111+
} else {
112+
params = new String[]{};
113+
}
114+
return validatorBundle.message(validation.message()[0], params);
115+
}
116+
117+
private ValidationRule getRuleFromAnnotation(FieldValidation validation) throws NoSuchMethodException,
118+
IllegalAccessException, InvocationTargetException, InstantiationException {
119+
ValidationRule rule;
120+
Class<?> ruleType = validation.rule().getRule();
121+
String[] ruleParams = validation.properties();
122+
123+
if (ruleParams.length >= 1 && !ruleParams[0].isEmpty()) {
124+
rule = (ValidationRule)ruleType.getConstructor(String.class).newInstance(ruleParams);
125+
} else {
126+
rule = (ValidationRule)ruleType.getConstructor().newInstance();
127+
}
128+
return rule;
129+
}
130+
131+
protected void addValidationRuleToField(Object field, ValidationRule rule, String message) {
132+
if (!(field instanceof JComponent)) {
133+
return;
134+
}
135+
List<ValidationRule> rules;
136+
if (!textFieldValidationRuleMap.containsKey(field)) {
137+
rules = new ArrayList<>();
138+
} else {
139+
rules = textFieldValidationRuleMap.get(field);
140+
}
141+
142+
if (!rules.contains(rule) && rule != null) {
143+
addFieldValidationRuleMessageAssociation(field, rule, message);
144+
rules.add(rule);
145+
textFieldValidationRuleMap.put(field, rules);
146+
}
147+
}
148+
149+
private void addFieldValidationRuleMessageAssociation(Object field, ValidationRule rule, String message) {
150+
Map<ValidationRule, String> validationRuleErrorMessageMap;
151+
if (!errorMessageFieldValidationRuleMap.containsKey(field)) {
152+
validationRuleErrorMessageMap = new HashMap<>();
153+
} else {
154+
validationRuleErrorMessageMap = errorMessageFieldValidationRuleMap.get(field);
155+
}
156+
validationRuleErrorMessageMap.put(rule, message);
157+
errorMessageFieldValidationRuleMap.put(field, validationRuleErrorMessageMap);
158+
}
159+
160+
private String resolveFieldValueByComponentType(Object field) {
161+
if (field instanceof JTextField) {
162+
return ((JTextField) field).getText();
163+
} else if (field instanceof JComboBox) {
164+
return ((JComboBox) field).getSelectedItem().toString();
165+
}
166+
return null;
167+
}
36168
}

0 commit comments

Comments
 (0)