blib/lib/Embperl/Form/Validate/Default.pm untested

filter: Statementstmt 0.0% 0 / 192 Branchbran 0.0% 0 / 44 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 53 Podpod 0.0% 0 / 49 totaltotal 0.0% 0 / 378 CC 23 SCAR 14.7
CC
23
Cov
0%
validate_required
6.0
validate_emptyok
6.0
validate_checked
6.0
CRAP 4.3
1  
2 ###################################################################################
3 #
4 # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de
5 # Embperl - Copyright (c) 2008-2015 Gerald Richter
6 # Embperl - Copyright (c) 2015-2023 actevy.io
7 #
8 # You may distribute under the terms of either the GNU General Public
9 # License or the Artistic License, as specified in the Perl README file.
10 #
11 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
12 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
13 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
14 #
15 ###################################################################################
16  
17  
18 package Embperl::Form::Validate::Default;
19  
20 use strict;
21 use vars qw($VERSION %error_messages %script_functions %prefixes);
22  
23 $VERSION = '3.0.1' ;
24  
25 %script_functions = ();
26 %prefixes = ();
27 use utf8 ;
28  
29 my %errutf8 =
30 (
31 validate_required => 'Bitte Feld "%0" ausfüllen',
32 validate_eq => 'Falscher Inhalt "%1" des Feldes "%0": Erwartet wird "%2"',
33 validate_same => '"%0" stimmt nicht mit "%2" überein',
34 validate_lt => '"%0" muß kleiner als %2 sein',
35 validate_gt => '"%0" muß größer als %2 sein',
36 validate_le => '"%0" muß kleiner oder gleich wie %2 sein',
37 validate_ge => '"%0" muß größer oder gleich %2 sein',
38 validate_ne => '"%0" muß ungleich %2 sein',
39 validate_length_max => 'Inhalt des Feldes "%0" ist zu lang, maximale Länge sind %2, eingegeben wurden %1 Zeichen',
40 validate_length_min => 'Inhalt des Feldes "%0" ist zu kurz, minimal Länge sind %2, eingegeben wurden %1 Zeichen',
41 validate_length_eq => 'Inhalt des Feldes "%0" hat die falsche Länge: Er sollte %2 Zeichen lang sein, ist aber %1 lang',
42 validate_matches_regex => 'Inhalt "%1" des Feldes "%0" entspricht nicht dem regulären Ausdruck /%2/',
43 validate_matches_regex_js => 'Inhalt "%1" des Feldes "%0" entspricht nicht dem regulären Ausdruck /%2/',
44 validate_not_matches_regex => 'Inhalt "%1" des Feldes "%0" darf nicht dem regulären Ausdruck /%2/ entsprechen',
45 validate_not_matches_regex_js => 'Inhalt "%1" des Feldes "%0" darf nicht dem regulären Ausdruck /%2/ entsprechen',
46 validate_matches_wildcard => 'Inhalt "%1" des Feldes "%0" entspricht nicht dem Wildcard-Ausdruck "%2"',
47 validate_must_only_contain => 'Das Feld "%0" darf nur folgende Zeichen enthalten: "%2"',
48 validate_must_contain_one_of => 'Das Feld "%0" muß mindestens eines der folgenden Zeichen enthalten: "%2"',
49 validate_must_not_contain => 'Das Feld "%0" darf folgende Zeichen nicht enthalten: "%2"'
50 ) ;
51  
52 no utf8 ;
53  
54 %error_messages =
55 (
56 de =>
57 {
58 validate_required => 'Bitte Feld "%0" ausfüllen',
59 validate_eq => 'Falscher Inhalt "%1" des Feldes "%0": Erwartet wird "%2"',
60 validate_same => '"%0" stimmt nicht mit "%2" überein',
61 validate_lt => '"%0" muß kleiner als %2 sein',
62 validate_gt => '"%0" muß größer als %2 sein',
63 validate_le => '"%0" muß kleiner oder gleich wie %2 sein',
64 validate_ge => '"%0" muß größer oder gleich %2 sein',
65 validate_ne => '"%0" muß ungleich %2 sein',
66 validate_length_max => 'Inhalt des Feldes "%0" ist zu lang, maximale Länge sind %2, eingegeben wurden %1 Zeichen',
67 validate_length_min => 'Inhalt des Feldes "%0" ist zu kurz, minimal Länge sind %2, eingegeben wurden %1 Zeichen',
68 validate_length_eq => 'Inhalt des Feldes "%0" hat die falsche Länge: Er sollte %2 Zeichen lang sein, ist aber %1 lang',
69 validate_matches_regex => 'Inhalt "%1" des Feldes "%0" entspricht nicht dem regulären Ausdruck /%2/',
70 validate_matches_regex_js => 'Inhalt "%1" des Feldes "%0" entspricht nicht dem regulären Ausdruck /%2/',
71 validate_not_matches_regex => 'Inhalt "%1" des Feldes "%0" darf nicht dem regulären Ausdruck /%2/ entsprechen',
72 validate_not_matches_regex_js => 'Inhalt "%1" des Feldes "%0" darf nicht dem regulären Ausdruck /%2/ entsprechen',
73 validate_matches_wildcard => 'Inhalt "%1" des Feldes "%0" entspricht nicht dem Wildcard-Ausdruck "%2"',
74 validate_must_only_contain => 'Das Feld "%0" darf nur folgende Zeichen enthalten: "%2"',
75 validate_must_contain_one_of => 'Das Feld "%0" muß mindestens eines der folgenden Zeichen enthalten: "%2"',
76 validate_must_not_contain => 'Das Feld "%0" darf folgende Zeichen nicht enthalten: "%2"'
77 },
78
79 'de.utf8' => \%errutf8,
80
81 en =>
82 {
83 validate_required => 'Please enter a value in "%0"',
84 validate_eq => 'Wrong content "%1" of field "%0": Expected "%2"',
85 validate_same => '"%0" does not match "%2"',
86 validate_lt => '"%0" must be less then %2',
87 validate_gt => '"%0" must be greater then %2',
88 validate_le => '"%0" must be less or equal then %2',
89 validate_ge => '"%0" must be greater or equal then %2',
90 validate_ne => 'Wrong content "%1" of field "%0": Expected not "%2"',
91 validate_length_max => 'Content of field "%0" is too long, has %1 characters, maximum is %2 characters',
92 validate_length_min => 'Content of field "%0" is too short, has %1 characters, minimum is %2 characters',
93 validate_length_eq => 'Content of field "%0" has wrong length: It is %1 characters long, but should be %2 characters long',
94 validate_matches_regex => 'Field "%0" doesn"t match regexp /%2/',
95 validate_matches_regex_js => 'Field "%0" doesn"t match regexp /%2/',
96 validate_not_matches_regex => 'Field "%0" must not match regexp /%2/',
97 validate_not_matches_regex_js => 'Field "%0" must not match regexp /%2/',
98 validate_matches_wildcard => 'Field "%0" doesn"t match wildcard expression "%2"',
99 validate_must_only_contain => 'Field "%0" must contain only the following characters: "%2"',
100 validate_must_contain_one_of => 'Field "%0" must contain one of the following characters: "%2"',
101 validate_must_not_contain => 'Field "%0" must not contain the following characters: "%2"'
102 }
103 );
104  
105  
106  
107 # --------------------------------------------------------------
108  
109 sub new
110 {
111 my $invokedby = shift;
112 my $class = ref($invokedby) || $invokedby;
113 my $self = {} ;
114 bless($self, $class);
115 $self->init;
116 return $self;
117 }
118  
119 # --------------------------------------------------------------
120  
121 sub getmsg
122 {
123 my ($self, $id, $language, $default_language) = @_ ;
124  
125 return $error_messages{$language}{$id} || $error_messages{$default_language}{$id} ;
126 }
127  
128 # --------------------------------------------------------------
129  
130 sub init
131 {
132 my $self = shift;
133 return 1;
134 }
135  
136 # --------------------------------------------------------------
137  
138 sub validate
139 {
140 return undef ;
141 }
142  
143 # --------------------------------------------------------------
144  
145 sub validate_required
146 {
147 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
148
149 return defined($value) && $value ne '' ? undef : ['validate_required'] ;
150 }
151  
152 # --------------------------------------------------------------
153  
154 sub getscript_required
155 {
156 my ($self, $arg, $pref) = @_ ;
157
158 return ('obj instanceof NodeList?obj[0].value:obj.value', ['validate_required']) ;
159 }
160  
161 # --------------------------------------------------------------
162  
163 sub validate_emptyok
164 {
165 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
166
167 return !defined($value) || $value eq '' ? [] : undef ;
168 }
169  
170 # --------------------------------------------------------------
171  
172 sub getscript_emptyok
173 {
174 my ($self, $arg, $pref) = @_ ;
175
176 return ('obj.value') ;
177 }
178  
179  
180 # --------------------------------------------------------------
181  
182 sub validate_checked
183 {
184 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
185
186 return !defined($value) || $value eq '' ? undef : [] ;
187 }
188  
189 # --------------------------------------------------------------
190  
191 sub getscript_checked
192 {
193 my ($self, $arg, $pref) = @_ ;
194
195 return ('obj.checked') ;
196 }
197  
198 # --------------------------------------------------------------
199  
200 sub validate_notchecked
201 {
202 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
203
204 return !defined($value) || $value eq '' ? [] : undef ;
205 }
206  
207 # --------------------------------------------------------------
208  
209 sub getscript_notchecked
210 {
211 my ($self, $arg, $pref) = @_ ;
212
213 return ('!obj.checked') ;
214 }
215  
216 # --------------------------------------------------------------
217  
218 sub validate_eq
219 {
220 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
221
222 return $value eq $arg ? undef : ['validate_eq', $value, $arg] ;
223 }
224  
225 # --------------------------------------------------------------
226  
227 sub getscript_eq
228 {
229 my ($self, $arg, $pref) = @_ ;
230
231 return ("obj.value == '$arg'", ['validate_eq', "+'obj.value'+", $arg]) ;
232 }
233  
234 # --------------------------------------------------------------
235  
236 sub validate_same
237 {
238 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
239  
240 my ($key2, $name2) = split (/:/, $arg) ;
241 $name2 ||= $key2 ;
242
243 return $value eq $fdat -> {$key2} ? undef : ['validate_same', $value, $name2] ;
244 }
245  
246 # --------------------------------------------------------------
247  
248 sub getscript_same
249 {
250 my ($self, $arg, $pref, $form) = @_ ;
251
252 my ($key2, $name2) = split (/:/, $arg) ;
253 $name2 ||= $key2 ;
254  
255 return ("obj.value == formelem\['$key2'\].value", ['validate_same', "+'obj.value'+", $name2]) ;
256 }
257  
258 # --------------------------------------------------------------
259  
260 sub validate_gt
261 {
262 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
263
264 return $value gt $arg ? undef : ['validate_gt', $value, $arg] ;
265 }
266  
267 # --------------------------------------------------------------
268  
269 sub getscript_gt
270 {
271 my ($self, $arg, $pref) = @_ ;
272
273 return ("obj.value > '$arg'", ['validate_gt', "+'obj.value'+", $arg]) ;
274 }
275  
276 # --------------------------------------------------------------
277  
278 sub validate_lt
279 {
280 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
281
282 return $value lt $arg ? undef : ['validate_lt', $value, $arg] ;
283 }
284  
285 # --------------------------------------------------------------
286  
287 sub getscript_lt
288 {
289 my ($self, $arg, $pref) = @_ ;
290
291 return ("obj.value < '$arg'", ['validate_lt', "+'obj.value'+", $arg]) ;
292 }
293  
294 # --------------------------------------------------------------
295  
296 sub validate_ge
297 {
298 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
299
300 return $value ge $arg ? undef : ['validate_ge', $value, $arg] ;
301 }
302  
303 # --------------------------------------------------------------
304  
305 sub getscript_ge
306 {
307 my ($self, $arg, $pref) = @_ ;
308
309 return ("obj.value >= '$arg'", ['validate_ge', "+'obj.value'+", $arg]) ;
310 }
311  
312 # --------------------------------------------------------------
313  
314 sub validate_le
315 {
316 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
317
318 return $value le $arg ? undef : ['validate_le', $value, $arg] ;
319 }
320  
321 # --------------------------------------------------------------
322  
323 sub getscript_le
324 {
325 my ($self, $arg, $pref) = @_ ;
326
327 return ("obj.value <= '$arg'", ['validate_gt', "+'obj.value'+", $arg]) ;
328 }
329  
330 # --------------------------------------------------------------
331  
332 sub validate_ne
333 {
334 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
335
336 return $value ne $arg ? undef : ['validate_ne', $value, $arg] ;
337 }
338  
339 # --------------------------------------------------------------
340  
341 sub getscript_ne
342 {
343 my ($self, $arg, $pref) = @_ ;
344
345 return ("obj.value != '$arg'", ['validate_gt', "+'obj.value'+", $arg]) ;
346 }
347  
348 # --------------------------------------------------------------
349  
350 sub validate_length_max
351 {
352 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
353
354 return length($value) <= $arg ? undef : ['validate_length_max', length($value), $arg] ;
355 }
356  
357 # --------------------------------------------------------------
358  
359 sub getscript_length_max
360 {
361 my ($self, $arg, $pref) = @_ ;
362
363 return ("obj.value.length <= $arg", ['validate_length_max', "'+obj.value.length+'", $arg]) ;
364 }
365  
366 # --------------------------------------------------------------
367  
368 sub validate_length_min
369 {
370 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
371
372 return length($value) >= $arg ? undef : ['validate_length_min', length($value), $arg] ;
373 }
374  
375 # --------------------------------------------------------------
376  
377 sub getscript_length_min
378 {
379 my ($self, $arg, $pref) = @_ ;
380
381 return ("obj.value.length >= $arg", ['validate_length_min', "'+obj.value.length+'", $arg]) ;
382 }
383  
384 # --------------------------------------------------------------
385  
386 sub validate_length_eq
387 {
388 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
389
390 return length($value) == $arg ? undef : ['validate_length_eq', length($value), $arg] ;
391 }
392  
393 # --------------------------------------------------------------
394  
395 sub getscript_length_eq
396 {
397 my ($self, $arg, $pref) = @_ ;
398
399 return ("obj.value.length == $arg", ['validate_length_eq', "'+obj.value.length+'", $arg]) ;
400 }
401  
402 # --------------------------------------------------------------
403  
404 sub validate_matches_regex
405 {
406 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
407
408 return ($value =~ /$arg/) ? undef : ['validate_matches_regex', $value, $arg] ;
409 }
410  
411 # --------------------------------------------------------------
412  
413 sub validate_matches_regex_perl
414 {
415 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
416
417 return ($value =~ /$arg/) ? undef : ['validate_matches_regex', $value, $arg] ;
418 }
419  
420 # --------------------------------------------------------------
421  
422 sub validate_matches_regex_js
423 {
424 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
425
426 return undef ; # only client side!
427 }
428  
429 # --------------------------------------------------------------
430  
431 sub getscript_matches_regex
432 {
433 my ($self, $arg, $pref) = @_ ;
434
435 $arg =~ s(/)(\\/)g; # JS needs / escaping
436 return ("obj.value.search(/$arg/) >= 0", ['validate_matches_regex', "'+obj.value+'", $arg]) ;
437 }
438  
439 # --------------------------------------------------------------
440  
441 sub getscript_matches_regex_js
442 {
443 my ($self, $arg, $pref) = @_ ;
444
445 $arg =~ s(/)(\\/)g; # JS needs / escaping
446 return ("obj.value.search(/$arg/) >= 0", ['validate_matches_regex', "'+obj.value+'", $arg]) ;
447 }
448  
449 # --------------------------------------------------------------
450  
451 sub validate_not_matches_regex
452 {
453 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
454
455 return ($value !~ /$arg/) ? undef : ['validate_not_matches_regex', $value, $arg] ;
456 }
457  
458 # --------------------------------------------------------------
459  
460 sub validate_not_matches_regex_perl
461 {
462 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
463
464 return ($value !~ /$arg/) ? undef : ['validate_not_matches_regex', $value, $arg] ;
465 }
466  
467 # --------------------------------------------------------------
468  
469 sub validate_not_matches_regex_js
470 {
471 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
472
473 return undef ; # only client side!
474 }
475  
476 # --------------------------------------------------------------
477  
478 sub getscript_not_matches_regex
479 {
480 my ($self, $arg, $pref) = @_ ;
481
482 $arg =~ s(/)(\\/)g; # JS needs / escaping
483 return ("obj.value.search(/$arg/) < 0", ['validate_not_matches_regex', "'+obj.value+'", $arg]) ;
484 }
485  
486 # --------------------------------------------------------------
487  
488 sub getscript_not_matches_regex_js
489 {
490 my ($self, $arg, $pref) = @_ ;
491
492 $arg =~ s(/)(\\/)g; # JS needs / escaping
493 return ("obj.value.search(/$arg/) < 0", ['validate_not_matches_regex', "'+obj.value+'", $arg]) ;
494 }
495  
496 # --------------------------------------------------------------
497  
498 sub validate_matches_wildcard
499 {
500 my ($self, $key, $value, $wc, $fdat, $pref) = @_ ;
501
502 $wc =~ s/=/==/g;
503 $wc =~ s/(^|[^\\])\?/$1=./g;
504 $wc =~ s/([^\\])\*/$1=.=*/g;
505 $wc =~ s/([^\\])([][])/$1=$2/g;
506 $wc =~ s/=(.)/$1/g;
507  
508 return ($value =~ /$wc/) ? undef : ['validate_matches_wildcard', $value, $wc] ;
509 }
510  
511 # --------------------------------------------------------------
512  
513 sub validate_must_only_contain
514 {
515 my ($self, $key, $value, $moc, $fdat, $pref) = @_ ;
516
517 $moc =~ s/^\^(.)/$1^/;
518 $moc =~ s/^(.*)\]/\]$1/;
519 $moc =~ s/^(.*)-/-$1/;
520 $moc =~ s#/#\\/#;
521 return ($value =~ /^[$moc]*$/) ? undef : ['validate_must_only_contain', $value, $moc] ;
522 }
523  
524 # --------------------------------------------------------------
525  
526 sub getscript_must_only_contain
527 {
528 my ($self, $arg, $pref) = @_ ;
529
530 $arg =~ s/^\^(.)/$1^/;
531 $arg =~ s/^(.*)\]/\]$1/;
532 $arg =~ s/^(.*)-/-$1/;
533 $arg =~ s#/#\\/#;
534 return ("obj.value.search(/^[$arg]*\$/) >= 0", ['validate_must_only_contain', "'+obj.value+'", $arg]) ;
535 }
536  
537 # --------------------------------------------------------------
538  
539 sub validate_must_not_contain
540 {
541 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
542
543 $arg =~ s/^\^(.)/$1^/;
544 $arg =~ s/^(.*)\]/\]$1/;
545 $arg =~ s/^(.*)-/-$1/;
546 $arg =~ s#/#\\/#;
547 return ($value !~ /[$arg]/) ? undef : ['validate_must_only_contain', $value, $arg] ;
548 }
549  
550 # --------------------------------------------------------------
551  
552 sub getscript_must_not_contain
553 {
554 my ($self, $arg, $pref) = @_ ;
555
556 $arg =~ s/^\^(.)/$1^/;
557 $arg =~ s/^(.*)\]/\]$1/;
558 $arg =~ s/^(.*)-/-$1/;
559 $arg =~ s#/#\\/#;
560 return ("obj.value.search(/[$arg]/) == -1", ['validate_must_not_contain', "'+obj.value+'", $arg]) ;
561 }
562  
563 # --------------------------------------------------------------
564  
565 sub validate_must_contain_one_of
566 {
567 my ($self, $key, $value, $arg, $fdat, $pref) = @_ ;
568
569 $arg =~ s/^\^(.)/$1^/;
570 $arg =~ s/^(.*)\]/\]$1/;
571 $arg =~ s/^(.*)-/-$1/;
572 $arg =~ s#/#\\/#;
573 return ($value =~ /[$arg]/) ? undef : ['validate_must_only_contain', $value, $arg] ;
574 }
575  
576 # --------------------------------------------------------------
577  
578 sub getscript_must_contain_one_of
579 {
580 my ($self, $arg, $pref) = @_ ;
581
582 $arg =~ s/^\^(.)/$1^/;
583 $arg =~ s/^(.*)\]/\]$1/;
584 $arg =~ s/^(.*)-/-$1/;
585 $arg =~ s#/#\\/#;
586 return ("obj.value.search(/[$arg]/) >= 0", ['validate_must_contain_one_of', "'+obj.value+'", $arg]) ;
587 }
588  
589 # --------------------------------------------------------------
590  
591  
592 1 ;