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