Skip to content

Commit 916620c

Browse files
committed
Added validation for property names
1 parent 05689a2 commit 916620c

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

resources/magento2/validation.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ validator.mustNotBeEmptyShouldContainLettersOrNumbers=Must not be empty, should
2929
validator.magentoRouteIdInvalid=The route id is invalid
3030
validator.magentoAclResourceIdInvalid=The ACL resource id is invalid
3131
validator.lowercaseCharacters={0} must contain lowercase characters only
32+
validator.lowerSnakeCase=The {0} field must be of the lower snake case format

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewDataModelDialog.form

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<grid id="cbd77" binding="contentPanel" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
44
<margin top="10" left="10" bottom="10" right="10"/>
55
<constraints>
6-
<xy x="48" y="54" width="454" height="423"/>
6+
<xy x="48" y="54" width="463" height="423"/>
77
</constraints>
88
<properties>
99
<preferredSize width="500" height="600"/>
@@ -38,7 +38,7 @@
3838
</component>
3939
</children>
4040
</grid>
41-
<grid id="ebe15" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
41+
<grid id="ebe15" layout-manager="GridLayoutManager" row-count="3" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4242
<margin top="5" left="5" bottom="5" right="5"/>
4343
<constraints>
4444
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@@ -63,9 +63,22 @@
6363
</component>
6464
</children>
6565
</scrollpane>
66+
<component id="f86c5" class="javax.swing.JTextPane">
67+
<constraints>
68+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="6" anchor="0" fill="3" indent="0" use-parent-layout="false">
69+
<preferred-size width="150" height="50"/>
70+
</grid>
71+
</constraints>
72+
<properties>
73+
<editable value="false"/>
74+
<enabled value="false"/>
75+
<text value="Properties must be defined in snake casing. For example: 'product_id' or 'category'."/>
76+
<toolTipText value=""/>
77+
</properties>
78+
</component>
6679
<component id="7e1b3" class="javax.swing.JButton" binding="addProperty">
6780
<constraints>
68-
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
81+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
6982
</constraints>
7083
<properties>
7184
<text value="Add Property"/>

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

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,15 @@
2222
import com.magento.idea.magento2plugin.actions.generation.generator.DataModelInterfaceGenerator;
2323
import com.magento.idea.magento2plugin.actions.generation.generator.PreferenceDiXmlGenerator;
2424
import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder;
25+
import com.magento.idea.magento2plugin.bundles.CommonBundle;
26+
import com.magento.idea.magento2plugin.bundles.ValidatorBundle;
2527
import com.magento.idea.magento2plugin.magento.files.DataModel;
2628
import com.magento.idea.magento2plugin.magento.files.DataModelInterface;
2729
import com.magento.idea.magento2plugin.ui.table.ComboBoxEditor;
2830
import com.magento.idea.magento2plugin.ui.table.DeleteRowButton;
2931
import com.magento.idea.magento2plugin.ui.table.TableButton;
3032
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
33+
import com.magento.idea.magento2plugin.util.RegExUtil;
3134
import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil;
3235
import java.awt.event.ActionEvent;
3336
import java.awt.event.KeyEvent;
@@ -37,6 +40,7 @@
3740
import javax.swing.JButton;
3841
import javax.swing.JCheckBox;
3942
import javax.swing.JComponent;
43+
import javax.swing.JOptionPane;
4044
import javax.swing.JPanel;
4145
import javax.swing.JTable;
4246
import javax.swing.JTextField;
@@ -47,10 +51,13 @@
4751

4852
@SuppressWarnings({
4953
"PMD.ExcessiveImports",
54+
"PMD.TooManyMethods",
5055
})
5156
public class NewDataModelDialog extends AbstractDialog {
5257
private final Project project;
5358
private final String moduleName;
59+
private final ValidatorBundle validatorBundle;
60+
private final CommonBundle commonBundle;
5461
private NamespaceBuilder interfaceNamespace;
5562
private NamespaceBuilder modelNamespace;
5663
private String formattedProperties;
@@ -83,6 +90,8 @@ public NewDataModelDialog(final Project project, final PsiDirectory directory) {
8390

8491
this.project = project;
8592
this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project);
93+
this.validatorBundle = new ValidatorBundle();
94+
this.commonBundle = new CommonBundle();
8695

8796
setContentPane(contentPanel);
8897
setModal(true);
@@ -132,6 +141,46 @@ private void onOK() {
132141
}
133142
}
134143

144+
@Override
145+
protected boolean validateFormFields() {
146+
boolean valid = false;
147+
if (super.validateFormFields()) {
148+
valid = true;
149+
final String errorTitle = commonBundle.message("common.error");
150+
final int column = 0;
151+
for (int row = 0; row < properties.getRowCount(); row++) {
152+
final String propertyName = ((String) properties.getValueAt(row, column)).trim();
153+
if (propertyName.isEmpty()) {
154+
valid = false;
155+
final String errorMessage = validatorBundle.message(
156+
"validator.notEmpty", "name"
157+
);
158+
JOptionPane.showMessageDialog(
159+
null,
160+
errorMessage,
161+
errorTitle,
162+
JOptionPane.ERROR_MESSAGE
163+
);
164+
break;
165+
} else if (!propertyName.matches(RegExUtil.LOWER_SNAKE_CASE)) {
166+
valid = false;
167+
final String errorMessage = validatorBundle.message(
168+
"validator.lowerSnakeCase", "name"
169+
);
170+
JOptionPane.showMessageDialog(
171+
null,
172+
errorMessage,
173+
errorTitle,
174+
JOptionPane.ERROR_MESSAGE
175+
);
176+
break;
177+
}
178+
}
179+
}
180+
181+
return valid;
182+
}
183+
135184
@Override
136185
public void onCancel() {
137186
dispose();

src/com/magento/idea/magento2plugin/util/RegExUtil.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ public class RegExUtil {
2222
public static final String IDENTIFIER
2323
= "[a-zA-Z0-9_\\-]*";
2424

25+
public static final String LOWER_SNAKE_CASE
26+
= "[a-z][a-z0-9_]*";
27+
2528
public static final String CLI_COMMAND_NAME
2629
= "[a-zA-Z0-9_\\-\\:]*";
2730

0 commit comments

Comments
 (0)