1+ /**
2+ * Smart Form Validation
3+ * Comprehensive form validation with custom rules and real-time feedback
4+ */
5+
6+ function onChange ( control , oldValue , newValue , isLoading ) {
7+ if ( isLoading || newValue === '' ) {
8+ return ;
9+ }
10+
11+ var validator = new SmartFormValidator ( ) ;
12+ validator . validateField ( control , newValue ) ;
13+ }
14+
15+ function onSubmit ( ) {
16+ var validator = new SmartFormValidator ( ) ;
17+ return validator . validateForm ( ) ;
18+ }
19+
20+ var SmartFormValidator = Class . create ( ) ;
21+ SmartFormValidator . prototype = {
22+ initialize : function ( ) {
23+ // Validation rules with regex patterns and custom functions
24+ this . rules = {
25+ // Basic field types
26+ email : {
27+ pattern : / ^ [ ^ \s @ ] + @ [ ^ \s @ ] + \. [ ^ \s @ ] + $ / ,
28+ message : 'Please enter a valid email address'
29+ } ,
30+ phone : {
31+ pattern : / ^ \+ ? [ \d \s - ] { 10 , } $ / ,
32+ message : 'Please enter a valid phone number'
33+ } ,
34+ number : {
35+ pattern : / ^ \d + $ / ,
36+ message : 'Please enter a valid number'
37+ } ,
38+ date : {
39+ validate : function ( value ) {
40+ var date = new Date ( value ) ;
41+ return ! isNaN ( date . getTime ( ) ) ;
42+ } ,
43+ message : 'Please enter a valid date'
44+ } ,
45+ // Special validations
46+ password : {
47+ validate : function ( value ) {
48+ return / ^ (? = .* [ A - Z a - z ] ) (? = .* \d ) (? = .* [ @ $ ! % * # ? & ] ) [ A - Z a - z \d @ $ ! % * # ? & ] { 8 , } $ / . test ( value ) ;
49+ } ,
50+ message : 'Password must contain at least 8 characters, one letter, one number, and one special character'
51+ } ,
52+ futureDate : {
53+ validate : function ( value ) {
54+ var date = new Date ( value ) ;
55+ return date > new Date ( ) ;
56+ } ,
57+ message : 'Date must be in the future'
58+ } ,
59+ workingHours : {
60+ validate : function ( value ) {
61+ var time = value . split ( ':' ) ;
62+ var hour = parseInt ( time [ 0 ] ) ;
63+ return hour >= 9 && hour < 17 ;
64+ } ,
65+ message : 'Time must be between 9 AM and 5 PM'
66+ }
67+ } ;
68+
69+ // Field dependencies storage
70+ this . dependencies = { } ;
71+ } ,
72+
73+ // Add a dependency between fields
74+ addDependency : function ( field , dependentField , validationFunc ) {
75+ if ( ! this . dependencies [ field ] ) {
76+ this . dependencies [ field ] = [ ] ;
77+ }
78+ this . dependencies [ field ] . push ( {
79+ field : dependentField ,
80+ validate : validationFunc
81+ } ) ;
82+ } ,
83+
84+ // Validate dependencies for a field
85+ validateDependencies : function ( field ) {
86+ if ( this . dependencies [ field ] ) {
87+ this . dependencies [ field ] . forEach ( function ( dep ) {
88+ var dependentValue = g_form . getValue ( dep . field ) ;
89+ var isValid = dep . validate ( dependentValue ) ;
90+
91+ if ( ! isValid ) {
92+ g_form . showFieldMsg ( dep . field , 'This field depends on ' + field , 'warning' ) ;
93+ }
94+ } ) ;
95+ }
96+ } ,
97+
98+ // Validate a single field
99+ validateField : function ( control , value ) {
100+ var fieldType = this . _getFieldValidationType ( control ) ;
101+ var rule = this . rules [ fieldType ] ;
102+
103+ if ( rule ) {
104+ var isValid = rule . pattern ?
105+ rule . pattern . test ( value ) :
106+ rule . validate ( value ) ;
107+
108+ if ( ! isValid ) {
109+ g_form . showFieldMsg ( control , rule . message , 'error' ) ;
110+ return false ;
111+ }
112+
113+ // Check field dependencies
114+ this . validateDependencies ( control ) ;
115+ g_form . hideFieldMsg ( control ) ;
116+ return true ;
117+ }
118+ return true ;
119+ } ,
120+
121+ // Validate entire form
122+ validateForm : function ( ) {
123+ var isValid = true ;
124+ var fields = g_form . getFields ( ) ;
125+ var invalidFields = [ ] ;
126+
127+ fields . forEach ( function ( field ) {
128+ var fieldName = field . getName ( ) ;
129+ var value = g_form . getValue ( fieldName ) ;
130+
131+ if ( ! this . validateField ( fieldName , value ) ) {
132+ isValid = false ;
133+ invalidFields . push ( fieldName ) ;
134+ }
135+ } , this ) ;
136+
137+ if ( ! isValid ) {
138+ this . _showValidationSummary ( invalidFields ) ;
139+ }
140+
141+ return isValid ;
142+ } ,
143+
144+ // Determine validation type for a field
145+ _getFieldValidationType : function ( fieldName ) {
146+ var field = g_form . getField ( fieldName ) ;
147+ var type = field . getType ( ) ;
148+
149+ // Check for special validation rules
150+ var validationType = g_form . getValue ( fieldName + '_validation_type' ) ;
151+ if ( validationType && this . rules [ validationType ] ) {
152+ return validationType ;
153+ }
154+
155+ // Map field types to validation rules
156+ switch ( type ) {
157+ case 'email' :
158+ return 'email' ;
159+ case 'phone' :
160+ return 'phone' ;
161+ case 'integer' :
162+ case 'decimal' :
163+ return 'number' ;
164+ case 'date' :
165+ return 'date' ;
166+ default :
167+ return null ;
168+ }
169+ } ,
170+
171+ // Show validation summary dialog
172+ _showValidationSummary : function ( invalidFields ) {
173+ var dialog = new GlideModal ( 'validation_summary' ) ;
174+ dialog . setTitle ( 'Form Validation Errors' ) ;
175+
176+ var html = '<div class="validation-summary">' ;
177+ html += '<p>Please correct the following fields:</p>' ;
178+ html += '<ul>' ;
179+
180+ invalidFields . forEach ( function ( fieldName ) {
181+ var label = g_form . getLabel ( fieldName ) ;
182+ html += '<li>' + label + '</li>' ;
183+ } ) ;
184+
185+ html += '</ul></div>' ;
186+ dialog . renderWithContent ( html ) ;
187+ } ,
188+
189+ type : 'SmartFormValidator'
190+ } ;
0 commit comments