]> Dogcows Code - chaz/p5-CGI-Ex/blob - t/samples/cgi_ex_2.cgi
CGI::Ex 1.14
[chaz/p5-CGI-Ex] / t / samples / cgi_ex_2.cgi
1 #!/usr/bin/perl -w
2
3 if (__FILE__ eq $0) {
4 &handler();
5 }
6
7 sub handler {
8 MyCGI->navigate();
9 }
10
11 ###----------------------------------------------------------------###
12
13 package MyCGI;
14
15 use strict;
16 use base CGI::Ex::App;
17 use CGI::Ex::Dump qw(debug);
18
19 ###----------------------------------------------------------------###
20
21 sub pre_loop {
22 my $self = shift;
23 my $path = shift;
24 if ($#$path == -1) {
25 push @$path, 'userinfo';
26 }
27 }
28
29 sub userinfo_ready_validate {
30 my $self = shift;
31 return $self->form->{processing} ? 1 : 0;
32 }
33
34 sub userinfo_hash_validation {
35 return {
36 'group order' => ['username', 'password'],
37 username => {
38 required => 1,
39 min_len => 3,
40 max_len => 30,
41 match => 'm/^\w+$/',
42 # could probably all be done with match => 'm/^\w{3,30}$/'
43 },
44 password => {
45 required => 1,
46 max_len => 20,
47 },
48 password_verify => {
49 validate_if => 'password',
50 equals => 'password',
51 },
52 };
53 }
54
55 sub userinfo_hash_form {
56 my $self = shift;
57 my $hash = $self->form;
58 $hash->{form_name} = 'formfoo';
59 $hash->{js_val} = $self->vob->generate_js($self->userinfo_hash_validation(),
60 $hash->{form_name},
61 "$ENV{SCRIPT_NAME}/js");
62 return $hash;
63 }
64
65 sub hash_common {
66 return {
67 title => 'My Application',
68 script => $ENV{SCRIPT_NAME},
69 color => ['#ccf', '#aaf'],
70 }
71 }
72
73 sub print {
74 my $self = shift;
75 my $step = shift;
76 my $form = shift;
77 my $fill = shift;
78
79 my $content = ($step eq 'userinfo') ? &get_content_form()
80 : ($step eq 'main') ? &get_content_success()
81 : "Don't have content for step \"$step\"";
82
83 $self->cgix->swap_template(\$content, $form);
84 $self->cgix->fill(\$content, $fill);
85
86 $self->cgix->print_content_type();
87 print $content;
88 }
89
90 ### this works because we added /js onto $ENV{SCRIPT_NAME} above near js_val
91 sub js_pre_step {
92 my $self = shift;
93 my $info = $ENV{PATH_INFO} || '';
94 if ($info =~ m|^/js(/\w+)+.js$|) {
95 $info =~ s|^/+js/+||;
96 $self->cgix->print_js($info);
97 return 1;
98 }
99 return 0;
100 }
101
102
103 ###----------------------------------------------------------------###
104
105 sub get_content_form {
106 return qq{
107 <html>
108 <head>
109 <title>[% title %]</title>
110 <style>
111 .error {
112 display: block;
113 color: red;
114 font-weight: bold;
115 }
116 </style>
117 </head>
118 <body>
119 <h1 style='color:blue'>Please Enter information</h1>
120 <span style='color:red'>[% error_header %]</span>
121 <br>
122
123 <form name="[% form_name %]">
124 <input type=hidden name=processing value=1>
125
126 <table>
127 <tr bgcolor=[% color.0 %]>
128 <td>Username:</td>
129 <td>
130 <input type=text size=30 name=username>
131 <span class=error id=username_error>[% username_error %]</span></td>
132 </tr>
133 <tr bgcolor=[% color.1 %]>
134 <td>Password:</td>
135 <td><input type=password size=20 name=password>
136 <span class=error id=password_error>[% password_error %]</span></td>
137 </tr>
138 <tr bgcolor=[% color.0 %]>
139 <td>Password Verify:</td>
140 <td><input type=password size=20 name=password_verify>
141 <span class=error id=password_verify_error>[% password_verify_error %]</span></td>
142 </tr>
143 <tr bgcolor=[% color.1 %]>
144 <td colspan=2 align=right><input type=submit value=Submit></td>
145 </tr>
146
147 </table>
148
149 </form>
150
151 [% js_val %]
152 </body>
153 </html>
154 };
155 }
156
157 sub get_content_success {
158 return qq{
159 <html>
160 <head><title>[% title %]</title></head>
161 <body>
162 <h1 style='color:green'>Success</h1>
163 <br>
164 print "I can now continue on with the rest of my script!";
165 </body>
166 </html>
167 };
168 }
169
170
171 1;
This page took 0.050328 seconds and 4 git commands to generate.