View Javadoc
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  import org.apache.commons.lang3.StringUtils;
26  import org.nuiton.util.StringUtil;
27  
28  /**
29   * Validator for email addresses :
30   * - Deal with + in addresses
31   *
32   * @author Jean Couteau - couteau@codelutin.com
33   * @since 2.3
34   */
35  public class EmailFieldValidator extends NuitonFieldValidatorSupport {
36  
37      @Override
38      public void validateWhenNotSkip(Object object) throws ValidationException {
39  
40          String fieldName = getFieldName();
41          Object value = getFieldValue(fieldName, object);
42  
43          if (value == null) {
44              // no value defined
45              return;
46          }
47          if (value instanceof String) {
48              if (StringUtils.isEmpty((String) value)) {
49                  // no value defined
50                  return;
51              }
52              if (!StringUtil.isEmail((String) value)) {
53                  addFieldError(fieldName, object);
54              }
55          } else {
56              addFieldError(fieldName, object);
57          }
58      }
59  
60      @Override
61      public String getValidatorType() {
62          return "emailNuiton";
63      }
64  }