1 /*
2 * #%L
3 * Nuiton Validator
4 * %%
5 * Copyright (C) 2013 - 2014 Code Lutin, Tony Chemit
6 * %%
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Lesser Public License for more details.
16 *
17 * You should have received a copy of the GNU General Lesser Public
18 * License along with this program. If not, see
19 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20 * #L%
21 */
22 package org.nuiton.validator.xwork2.field;
23
24 import com.opensymphony.xwork2.validator.ValidationException;
25
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 /**
30 * Validator for French last names :
31 * - No number accepted
32 * - Accept spaces, - and '
33 *
34 * @author Jean Couteau - couteau@codelutin.com
35 * @since 2.3
36 */
37 public class FrenchLastNameFieldValidator extends NuitonFieldValidatorSupport {
38
39 protected static String LAST_NAME_REGEXP = "[^\\d]+";
40
41 protected static Pattern p = Pattern.compile(LAST_NAME_REGEXP);
42
43 @Override
44 public void validateWhenNotSkip(Object object) throws ValidationException {
45
46 String fieldName = getFieldName();
47 Object value = getFieldValue(fieldName, object);
48
49 if (value == null) {
50 // no value defined
51 return;
52 }
53 if (value instanceof String) {
54 if ("".equals(value)) {
55 // no value defined
56 return;
57 }
58 Matcher m = p.matcher((String) value);
59 if (!m.matches()) {
60 addFieldError(fieldName, object);
61 }
62 } else {
63 addFieldError(fieldName, object);
64 }
65 }
66
67 @Override
68 public String getValidatorType() {
69 return "frenchLastName";
70 }
71 }