Skip to content

Commit 4e16819

Browse files
committed
Remove repeat hyphen
1 parent c9dfb75 commit 4e16819

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

src/main/java/ru/mystamps/web/controller/CategoryController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ protected void initBinder(WebDataBinder binder) {
6565
StringTrimmerEditor editor = new StringTrimmerEditor(false);
6666
binder.registerCustomEditor(String.class, "name", editor);
6767
binder.registerCustomEditor(String.class, "nameRu", editor);
68+
binder.registerCustomEditor(String.class, "name", new CustomCategoryNameEditor());
69+
binder.registerCustomEditor(String.class, "nameRu", new CustomCategoryNameEditor());
6870
}
6971

7072
@RequestMapping(Url.ADD_CATEGORY_PAGE)
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ru.mystamps.web.controller;
2+
3+
import java.beans.PropertyEditorSupport;
4+
import java.util.regex.Matcher;
5+
import java.util.regex.Pattern;
6+
7+
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_REPEAT_HYPHEN_REGEXP;
8+
9+
/**
10+
* Created by Максим on 28.08.2016.
11+
*/
12+
13+
@SuppressWarnings("PMD")
14+
public class CustomCategoryNameEditor extends PropertyEditorSupport {
15+
16+
@Override
17+
public void setAsText(String categoryName) throws IllegalArgumentException {
18+
19+
Pattern regexpCheck = Pattern.compile(CATEGORY_REPEAT_HYPHEN_REGEXP);
20+
Matcher regexpMatch = regexpCheck.matcher(categoryName);
21+
22+
if (!regexpMatch.matches()) {
23+
24+
setValue(categoryName.replaceAll(CATEGORY_REPEAT_HYPHEN_REGEXP, "-"));
25+
26+
} else {
27+
28+
setValue(categoryName);
29+
30+
}
31+
}
32+
33+
}

src/main/java/ru/mystamps/web/model/AddCategoryForm.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_NO_HYPHEN_REGEXP;
3737
import static ru.mystamps.web.validation.ValidationRules.CATEGORY_NAME_RU_REGEXP;
3838

39+
3940
@Getter
4041
@Setter
4142
@GroupSequence({

src/main/java/ru/mystamps/web/validation/ValidationRules.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public final class ValidationRules {
4141
public static final String CATEGORY_NAME_EN_REGEXP = "[- a-zA-Z]+";
4242
public static final String CATEGORY_NAME_RU_REGEXP = "[- а-яёА-ЯЁ]+";
4343
public static final String CATEGORY_NAME_NO_HYPHEN_REGEXP = "[ \\p{L}]([- \\p{L}]+[ \\p{L}])*";
44+
public static final String CATEGORY_REPEAT_HYPHEN_REGEXP = "[-]{2,}";
4445

4546
public static final int COUNTRY_NAME_MIN_LENGTH = 3;
4647
public static final int COUNTRY_NAME_MAX_LENGTH = Db.Country.NAME_LENGTH;

0 commit comments

Comments
 (0)