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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
#!/usr/bin/perl -w use HTML::Template; use CGI::Simple; use Data::FormValidator; use Data::FormValidator::Constraints qw(:closures FV_length_between FV_min_length FV_max_length); sub display_virgin_site; sub func2; sub check_missings; my $cgi = new CGI::Simple; #initial invocation of the site. Just display the empty form. if ( scalar($cgi->param)==0 ) { display_virgin_site(); } else{ check_missings(); } sub display_virgin_site { # open the html template my $template = HTML::Template->new( filename => 'main.html' ); # send the obligatory Content-Type and print the template output print( "Content-Type: text/html\n\n", $template->output ); } sub check_missings { my $profile_obj = Data::FormValidator->new('criteria.pl'); my $results = $profile_obj->check($cgi,'profil_name_1'); if($results->success()){ &func2(); } else{ my %cgiHash = $cgi->Vars(); my $errhash = $results->msgs(); # open the html template my $template = HTML::Template->new( filename => 'main.html' ); @used_keys = qw( lastname phone mobile email ); #filter error variables to items from array @used_keys my %clearedHash; foreach(@used_keys){ $clearedHash{'error_'.$_} = $errhash->{'error_'.$_}; } $template->param(\%clearedHash); #clear hash before second usage %clearedHash=(); foreach(@used_keys){ $clearedHash{$_} = $cgiHash{$_}; } $template->param(\%clearedHash); print( "Content-Type: text/html\n\n", $template->output ); }
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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
use Data::FormValidator::Constraints qw(:closures FV_length_between FV_min_length FV_max_length); use Data::FormValidator::Filters; { profil_name_1 => { required => [qw( lastname ) ], # Entferne alle führenden und folgenden Leerstellen von den Werten der Formularfelder filters => ['trim'], field_filter_regexp_map => { qr/(?:phone|mobile)$/ => sub { my $value = shift; $value =~ s§[-/ ]§§g;#Entferne Füllzeichen '/', '-' ' ' aus Telefonnummern return $value; }, }, # alle Felder deren name mit regexp übereinstimmen, bekommen passenden validator zugewiesen constraint_method_regexp_map => { # !Not like in doku on cpan with notation 'email' or 'email()'. That wouldn't work. qr/email$/ => [email(),FV_length_between(6,50)], qr/lastname$/ => FV_length_between(2,30) }, msgs => { # Präfix für Variablen des Formulars die fehlerhaft sind erhalten Namensschema error_<name> prefix=> 'error_', # Fehlertext für Variablen die als Required markiert waren erhalten diesen Fehlertext als Wert, welcher unten im format Schlüssel als %s Argument eingefügt wird missing => 'This value is required', # Fehlertext für Variablen, welche eine der Bedingungen, die für Sie definiert wurden verletzt haben invalid => 'Invalid value inserted', # Trennzeichenkette für das auftreteten von mehreren Bedingungsverletzungen für eine Variable aus dem Formular invalid_separator => "<br>\n", # Die Variablen mit Namensschema error_<name> bekommen diesen Fehlertext als Wert zugewiesen. %s ist entweder der Wert von missing oder von invalid format => '<span class="hint">%s</span>', # if a constraint is violated with one of this names, than create a custom error mmessage instead of the text in format. # length_between is the inlined name of FV_length_between(x,y). Just use the subname without brackets and params as name. constraints => { 'length_between' => 'To short or to long', 'email' => 'Not a valid email adress', }, # if any error is returned by msgs function then hash key 'errorflag' will be defined aka gets value 1 any_errors => 'errorflag', } } }
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
35
36
37
38
39
40
41
42
43
44
45
46
<!DOCTYPE html>
<html lang="en">
<head>
<!-- HTML 5 -->
<title>Titel der Seite</title>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
</head>
<body>
<form action="form.cgi" method="post">
<table>
<tr>
<td>
<label for="lastname">last name</label>
</td>
<td>
<input name="lastname" size="20" maxlength="30" type="text" id="lastname" value="<TMPL_VAR ESCAPE=HTML NAME="lastname">"><TMPL_VAR NAME="error_lastname">
</td>
</tr>
<tr>
<td>
<label for="phone">phone</label>
</td>
<td>
<input name="phone" size="11" maxlength="11" type="tel" id="phone" value="<TMPL_VAR ESCAPE=HTML NAME="phone">"><TMPL_VAR NAME="error_phone">
</td>
</tr>
<tr>
<td>
<label for="mobile">mobile</label>
</td>
<td>
<input name="mobile" size="11" maxlength="11" type="tel" id="mobile" value="<TMPL_VAR ESCAPE=HTML NAME="mobile">"><TMPL_VAR NAME="error_mobile">
</td>
</tr>
<tr>
<td>
<label for="email">email</label>
</td>
<td>
<input name="email" size="11" maxlength="11" type="email" id="email" value="<TMPL_VAR ESCAPE=HTML NAME="email">"><TMPL_VAR NAME="error_email">
</td>
</tr>
</table>
</form>
</body>
</html>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
... sub check_missings { my $profile_obj = Data::FormValidator->new('criteria.pl'); my $results = $profile_obj->check($cgi,'profil_name_1'); ######### NEU ################## foreach(keys(%{$results->valid()})){ $cgi->add_param( $_, $results->valid($_), 'overwrite' ); } ######### END NEU ############## if($results->success()){ &func2(); } else{ ....