]>
Dogcows Code - chaz/p5-CGI-Ex/blob - samples/app/cgi_ex_1.cgi
5 cgi_ex_1.cgi - Show a basic example using some of the CGI::Ex tools (but not App based)
13 ###----------------------------------------------------------------###
17 use CGI
::Ex
::Validate
();
18 use CGI
::Ex
::Dump
qw(debug);
20 ###----------------------------------------------------------------###
23 my $cgix = CGI
::Ex-
>new;
24 my $vob = CGI
::Ex
::Validate-
>new;
25 my $form = $cgix->get_form();
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
$|) {
33 $cgix->print_js($info);
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';
43 ### failed validation - send out the template
44 if (! $has_info || $errob) {
46 ### get a template and swap defaults
47 my $swap = defaults_hash
();
49 ### add errors to the swap (if any)
51 my $hash = $errob->as_hash();
52 $swap->{$_} = delete($hash->{$_}) foreach keys %$hash;
53 $swap->{'error_header'} = 'Please correct the form information below';
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
62 ### swap in defaults, errors and js_validation
63 my $content = $cgix->swap_template(get_content_form
(), $swap);
66 $cgix->fill(\
$content, $form);
70 $cgix->print_content_type();
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
());
85 ###----------------------------------------------------------------###
89 'group order' => ['username', 'password', 'password_verify'],
95 # could probably all be done with match => 'm/^\w{3,30}$/'
102 validate_if
=> 'password',
103 equals
=> 'password',
110 title
=> 'My Application',
111 script_name
=> $ENV{'SCRIPT_NAME'},
112 color
=> ['#ccccff', '#aaaaff'],
116 ###----------------------------------------------------------------###
118 sub get_content_form
{
122 <title>[% title %]</title>
132 <h1 style
='color:blue'>Please Enter information
</h1
>
133 <span style
='color:red'>[% error_header
%]</span
>
136 <form name
="[% form_name %]" action
="[% script_name %]" method="POST">
137 <input type
=hidden name
=processing value
=1>
140 <tr bgcolor
=[% color
.0 %]>
143 <input type
=text size
=30 name
=username
>
144 <span
class=error id
=username_error
>[% username_error
%]</span></td
>
146 <tr bgcolor
=[% color
.1 %]>
148 <td
><input type
=password size
=20 name
=password
>
149 <span
class=error id
=password_error
>[% password_error
%]</span></td
>
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
>
156 <tr bgcolor
=[% color
.1 %]>
157 <td colspan
=2 align
=right
><input type
=submit value
=Submit
></td
>
170 sub get_content_success
{
173 <head><title>[% title %]</title></head>
175 <h1 style='color:green'>Success</h1>
177 print "I can now continue on with the rest of my script!";
This page took 0.048352 seconds and 5 git commands to generate.