]>
Dogcows Code - chaz/p5-CGI-Ex/blob - cgi_ex_1.cgi
18aa11beeb139333b039c83209e78d2a1067fd95
7 ###----------------------------------------------------------------###
11 use CGI
::Ex
::Validate
();
13 use CGI
::Ex
::Dump
qw(debug);
15 ###----------------------------------------------------------------###
18 my $cgix = CGI
::Ex-
>new();
19 my $vob = CGI
::Ex
::Validate-
>new();
20 my $form = $cgix->get_form();
22 ### allow for js validation libraries
23 ### path_info should contain something like /CGI/Ex/yaml_load.js
24 ### see the line with 'js_val' below
25 my $info = $ENV{PATH_INFO
} || '';
26 if ($info =~ m
|^(/\w
+)+.js
$|) {
28 CGI
::Ex-
>new->print_js($info);
36 ### check for errors - if they have submitted information
37 my $has_info = ($form->{processing
}) ? 1 : 0;
38 my $errob = $has_info ? $vob->validate($form, &validation_hash
()) : undef;
39 my $form_name = 'formfoo';
41 ### failed validation - send out the template
42 if (! $has_info || $errob) {
44 ### get a template and swap defaults
45 my $swap = &defaults_hash
();
47 ### add errors to the swap (if any)
49 my $hash = $errob->as_hash();
50 $swap->{$_} = delete($hash->{$_}) foreach keys %$hash;
51 $swap->{'error_header'} = 'Please correct the form information below';
54 ### get js validation ready
55 $swap->{'form_name'} = $form_name;
56 $swap->{'js_val'} = $vob->generate_js(&validation_hash
(), # filename or valhash
57 $form_name, # name of form
58 $ENV{SCRIPT_NAME
}); # browser path to cgi that calls print_js
60 ### swap in defaults, errors and js_validation
61 my $content = $cgix->swap_template(&get_content_form
(), $swap);
64 $cgix->fill(\
$content, $form);
68 &CGI
::Ex
::print_content_type
();
74 ### show some sort of success if there were no errors
75 &CGI
::Ex
::print_content_type
();
76 my $content = $cgix->swap_template(&get_content_success
(), &defaults_hash
());
82 ###----------------------------------------------------------------###
86 'group order' => ['username', 'password'],
92 # could probably all be done with match => 'm/^\w{3,30}$/'
99 validate_if
=> 'password',
100 equals
=> 'password',
107 title
=> 'My Application',
108 script
=> $ENV{SCRIPT_NAME
},
109 color
=> ['#ccf', '#aaf'],
113 ###----------------------------------------------------------------###
115 sub get_content_form
{
119 <title>[% title %]</title>
129 <h1 style
='color:blue'>Please Enter information
</h1
>
130 <span style
='color:red'>[% error_header
%]</span
>
133 <form name
="[% form_name %]">
134 <input type
=hidden name
=processing value
=1>
137 <tr bgcolor
=[% color
.0 %]>
140 <input type
=text size
=30 name
=username
>
141 <span
class=error id
=username_error
>[% username_error
%]</span></td
>
143 <tr bgcolor
=[% color
.1 %]>
145 <td
><input type
=password size
=20 name
=password
>
146 <span
class=error id
=password_error
>[% password_error
%]</span></td
>
148 <tr bgcolor
=[% color
.0 %]>
149 <td
>Password Verify
:</td
>
150 <td
><input type
=password size
=20 name
=password_verify
>
151 <span
class=error id
=password_verify_error
>[% password_verify_error
%]</span></td
>
153 <tr bgcolor
=[% color
.1 %]>
154 <td colspan
=2 align
=right
><input type
=submit value
=Submit
></td
>
167 sub get_content_success
{
170 <head><title>[% title %]</title></head>
172 <h1 style='color:green'>Success</h1>
174 print "I can now continue on with the rest of my script!";
This page took 0.044128 seconds and 3 git commands to generate.