Hallo.
Ich habe das Problem, das der vordefinerte Filter trim
http://search.cpan.org/~markstos/Data-FormValidator-4.66/lib/Data/FormValidator.pm#filters irgendwie nicht angewendet wird. Das Wiederanzeigen des Formulars mit der Einagbe des Users wird nicht um umgebende Leerzeichen bereinigt.
Testen durch Aufruf der Datei form.cgi.
Hier paar Code Fragmente:
Datei form.cgi:
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
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;
if ( scalar($cgi->param)==0 ) {
display_virgin_site();
} else{
check_missings();
}
sub display_virgin_site
{
my $template = HTML::Template->new( filename => 'main.html' );
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();
my $template = HTML::Template->new( filename => 'main.html' );
@used_keys = qw(
lastname
phone
mobile
email
);
my %clearedHash;
foreach(@used_keys){
$clearedHash{'error_'.$_} = $errhash->{'error_'.$_};
}
$template->param(\%clearedHash);
%clearedHash=();
foreach(@used_keys){
$clearedHash{$_} = $cgiHash{$_};
}
$template->param(\%clearedHash);
print( "Content-Type: text/html\n\n", $template->output );
}
Datei criteria.pl:
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
)
],
filters => ['trim'],
field_filter_regexp_map => {
qr/(?:phone|mobile)$/ => sub {
my $value = shift;
$value =~ s§[-/ ]§§g;#Entferne Füllzeichen '/', '-' ' ' aus Telefonnummern
return $value;
},
},
constraint_method_regexp_map => {
qr/email$/ => [email(),FV_length_between(6,50)],
qr/lastname$/ => FV_length_between(2,30)
},
msgs => {
prefix=> 'error_',
missing => 'This value is required',
invalid => 'Invalid value inserted',
invalid_separator => "<br>\n",
format => '<span class="hint">%s</span>',
constraints => {
'length_between' => 'To short or to long',
'email' => 'Not a valid email adress',
},
any_errors => 'errorflag',
}
}
}
Datei main.html:
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>
Ich hoffe ich habe alles vollständig mitgegeben, damit ihr das hier nachvollziehen könnt.
Kann mir jemand den Fehler nennen, warum trim nicht auf z.B. lastname angewendet wird?
Gruß
mcfaq.
Last edited: 2011-04-20 14:07:06 +0200 (CEST)