]> Dogcows Code - chaz/p5-CGI-Ex/blob - samples/cgi_ex_1.cgi
CGI::Ex 2.00
[chaz/p5-CGI-Ex] / samples / cgi_ex_1.cgi
1 #!/usr/bin/perl -w
2
3 =head1 NAME
4
5 cgi_ex_1.cgi - Show a basic example using some of the CGI::Ex tools
6
7 =cut
8
9 if (__FILE__ eq $0) {
10 handler();
11 }
12
13 ###----------------------------------------------------------------###
14
15 use strict;
16 use CGI::Ex;
17 use CGI::Ex::Validate ();
18 use CGI::Ex::Dump qw(debug);
19
20 ###----------------------------------------------------------------###
21
22 sub handler {
23 my $cgix = CGI::Ex->new;
24 my $vob = CGI::Ex::Validate->new;
25 my $form = $cgix->get_form();
26
27 ### allow for js validation libraries
28 ### path_info should contain something like /CGI/Ex/yaml_load.js
29 ### see the line with 'js_val' below
30 my $info = $ENV{PATH_INFO} || '';
31 if ($info =~ m|^(/\w+)+.js$|) {
32 $info =~ s|^/+||;
33 $cgix->print_js($info);
34 return;
35 }
36
37
38 debug $form;
39
40
41 ### check for errors - if they have submitted information
42 my $has_info = ($form->{processing}) ? 1 : 0;
43 my $errob = $has_info ? $vob->validate($form, validation_hash()) : undef;
44 my $form_name = 'formfoo';
45
46 ### failed validation - send out the template
47 if (! $has_info || $errob) {
48
49 ### get a template and swap defaults
50 my $swap = defaults_hash();
51
52 ### add errors to the swap (if any)
53 if ($errob) {
54 my $hash = $errob->as_hash();
55 $swap->{$_} = delete($hash->{$_}) foreach keys %$hash;
56 $swap->{'error_header'} = 'Please correct the form information below';
57 }
58
59 ### get js validation ready
60 $swap->{'form_name'} = $form_name;
61 $swap->{'js_val'} = $vob->generate_js(validation_hash(), # filename or valhash
62 $form_name, # name of form
63 $ENV{SCRIPT_NAME}); # browser path to cgi that calls print_js
64
65 ### swap in defaults, errors and js_validation
66 my $content = $cgix->swap_template(get_content_form(), $swap);
67
68 ### fill form fields
69 $cgix->fill(\$content, $form);
70 #debug $content;
71
72 ### print it out
73 $cgix->print_content_type();
74 print $content;
75 return;
76 }
77
78
79 ### show some sort of success if there were no errors
80 $cgix->print_content_type;
81 my $content = $cgix->swap_template(get_content_success(), defaults_hash());
82 print $content;
83 return;
84
85 }
86
87 ###----------------------------------------------------------------###
88
89 sub validation_hash {
90 return {
91 'group order' => ['username', 'password'],
92 username => {
93 required => 1,
94 min_len => 3,
95 max_len => 30,
96 match => 'm/^\w+$/',
97 # could probably all be done with match => 'm/^\w{3,30}$/'
98 },
99 password => {
100 required => 1,
101 max_len => 20,
102 },
103 password_verify => {
104 validate_if => 'password',
105 equals => 'password',
106 },
107 };
108 }
109
110 sub defaults_hash {
111 return {
112 title => 'My Application',
113 script => $ENV{SCRIPT_NAME},
114 color => ['#ccf', '#aaf'],
115 }
116 }
117
118 ###----------------------------------------------------------------###
119
120 sub get_content_form {
121 return qq{
122 <html>
123 <head>
124 <title>[% title %]</title>
125 <style>
126 .error {
127 display: block;
128 color: red;
129 font-weight: bold;
130 }
131 </style>
132 </head>
133 <body>
134 <h1 style='color:blue'>Please Enter information</h1>
135 <span style='color:red'>[% error_header %]</span>
136 <br>
137
138 <form name="[% form_name %]">
139 <input type=hidden name=processing value=1>
140
141 <table>
142 <tr bgcolor=[% color.0 %]>
143 <td>Username:</td>
144 <td>
145 <input type=text size=30 name=username>
146 <span class=error id=username_error>[% username_error %]</span></td>
147 </tr>
148 <tr bgcolor=[% color.1 %]>
149 <td>Password:</td>
150 <td><input type=password size=20 name=password>
151 <span class=error id=password_error>[% password_error %]</span></td>
152 </tr>
153 <tr bgcolor=[% color.0 %]>
154 <td>Password Verify:</td>
155 <td><input type=password size=20 name=password_verify>
156 <span class=error id=password_verify_error>[% password_verify_error %]</span></td>
157 </tr>
158 <tr bgcolor=[% color.1 %]>
159 <td colspan=2 align=right><input type=submit value=Submit></td>
160 </tr>
161
162 </table>
163
164 </form>
165
166 [% js_val %]
167 </body>
168 </html>
169 };
170 }
171
172 sub get_content_success {
173 return qq{
174 <html>
175 <head><title>[% title %]</title></head>
176 <body>
177 <h1 style='color:green'>Success</h1>
178 <br>
179 print "I can now continue on with the rest of my script!";
180 </body>
181 </html>
182 };
183 }
184
185
186 1;
This page took 0.043903 seconds and 4 git commands to generate.