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