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.business.jaxb.formdefinition.FileName;
38 import fr.paris.lutece.plugins.formengine.web.FormErrorsList;
39 import fr.paris.lutece.portal.service.util.AppLogService;
40
41 import java.util.Iterator;
42
43
44
45
46
47 public class ValidatorMaxLength extends FieldValidator
48 {
49 private static final String PROPERTY_VALIDATION_MESSAGE = "formengine.validator.message.maxLength";
50 private static final String PROPERTY_VALIDATION_MESSAGE_FILE = "formengine.validator.message.maxFileSize";
51
52
53
54
55
56
57
58
59 public boolean validate( Field field, FormErrorsList errors )
60 {
61 String strFieldValue = field.getValue( );
62
63 int nMaxLength = Integer.parseInt( this.getRuleParameter( ) );
64
65
66 boolean bValid = true;
67
68 if ( field.getFileNames( ) != null )
69 {
70 Iterator<FileName> iterFileNames = field.getFileNames( ).getFileName( ).iterator( );
71
72 while ( bValid && iterFileNames.hasNext( ) )
73 {
74 String strSize = ( (FileName) iterFileNames.next( ) ).getSize( );
75
76 try
77 {
78 long lSize = Long.parseLong( strSize.trim( ) );
79
80 if ( lSize > nMaxLength )
81 {
82 bValid = false;
83 }
84 }
85 catch ( NumberFormatException nfe )
86 {
87 AppLogService.error( nfe );
88 }
89 }
90 }
91
92 if ( ( strFieldValue != null ) && ( strFieldValue.length( ) > nMaxLength ) )
93 {
94 bValid = false;
95 }
96
97 if ( !bValid )
98 {
99 String[] messageParams = new String[3];
100 messageParams[0] = field.getName( );
101 messageParams[1] = field.getLabel( );
102 messageParams[2] = String.valueOf( nMaxLength );
103
104 if ( this.getErrorMessage( ) != null )
105 {
106 errors.addErrorMessage( this.getErrorMessage( ) );
107 }
108 else
109 {
110 if ( field.getFileNames( ) != null )
111 {
112 errors.addError( PROPERTY_VALIDATION_MESSAGE_FILE, messageParams );
113 }
114 else
115 {
116 errors.addError( PROPERTY_VALIDATION_MESSAGE, messageParams );
117 }
118 }
119
120 return false;
121 }
122
123 return true;
124 }
125 }