1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package fr.paris.lutece.plugins.formengine.service.validator;
35
36 import fr.paris.lutece.plugins.formengine.business.jaxb.formdefinition.Field;
37 import fr.paris.lutece.plugins.formengine.web.FormErrorsList;
38
39
40
41
42
43 public class ValidatorFieldNumeric extends ValidatorPattern
44 {
45 private static final String PROPERTY_VALIDATION_MESSAGE = "formengine.validator.message.numeric";
46 private static final String PROPERTY_VALIDATION_MESSAGE_MIN = "formengine.validator.message.numeric.min";
47 private static final String PROPERTY_VALIDATION_MESSAGE_MAX = "formengine.validator.message.numeric.max";
48 private static final String NUMERIC_PATTERN = "^\\d*$";
49
50
51
52
53
54
55
56
57 public boolean validate( Field field, FormErrorsList errors )
58 {
59 String strFieldValue = field.getValue( );
60
61 if ( ( strFieldValue == null ) || ( strFieldValue.trim( ).equals( "" ) ) )
62 {
63 return true;
64 }
65
66 String[] messageParams = new String[3];
67 messageParams[0] = field.getName( );
68 messageParams[1] = field.getLabel( );
69
70 boolean bMatches = checkValueOnPattern( NUMERIC_PATTERN, strFieldValue );
71
72 if ( bMatches )
73 {
74 int nFieldValue;
75
76 try
77 {
78 nFieldValue = Integer.parseInt( strFieldValue );
79 }
80 catch ( NumberFormatException nfe )
81 {
82 if ( this.getErrorMessage( ) != null )
83 {
84 errors.addErrorMessage( this.getErrorMessage( ) );
85 }
86 else
87 {
88 errors.addError( PROPERTY_VALIDATION_MESSAGE, messageParams );
89 }
90
91 return false;
92 }
93
94 try
95 {
96 int nMinValue = Integer.parseInt( this.getMin( ) );
97
98 if ( nFieldValue < nMinValue )
99 {
100 messageParams[2] = this.getMin( );
101
102 if ( this.getErrorMessage( ) != null )
103 {
104 errors.addErrorMessage( this.getErrorMessage( ) );
105 }
106 else
107 {
108 errors.addError( PROPERTY_VALIDATION_MESSAGE_MIN, messageParams );
109 }
110
111 return false;
112 }
113 }
114 catch ( NumberFormatException e )
115 {
116
117 }
118
119 try
120 {
121 int nMaxValue = Integer.parseInt( this.getMax( ) );
122
123 if ( nFieldValue > nMaxValue )
124 {
125 messageParams[2] = this.getMax( );
126
127 if ( this.getErrorMessage( ) != null )
128 {
129 errors.addErrorMessage( this.getErrorMessage( ) );
130 }
131 else
132 {
133 errors.addError( PROPERTY_VALIDATION_MESSAGE_MAX, messageParams );
134 }
135
136 return false;
137 }
138 }
139 catch ( NumberFormatException e )
140 {
141
142 }
143
144 return true;
145 }
146 else
147 {
148 if ( this.getErrorMessage( ) != null )
149 {
150 errors.addErrorMessage( this.getErrorMessage( ) );
151 }
152 else
153 {
154 errors.addError( PROPERTY_VALIDATION_MESSAGE, messageParams );
155 }
156
157 return false;
158 }
159 }
160 }