]>
Dogcows Code - chaz/p5-CGI-Ex/blob - samples/cgi_ex_1.cgi
5 cgi_ex_1.cgi - Show a basic example using some of the CGI::Ex tools
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);
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';
46 ### failed validation - send out the template
47 if (! $has_info || $errob) {
49 ### get a template and swap defaults
50 my $swap = defaults_hash
();
52 ### add errors to the swap (if any)
54 my $hash = $errob->as_hash();
55 $swap->{$_} = delete($hash->{$_}) foreach keys %$hash;
56 $swap->{'error_header'} = 'Please correct the form information below';
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
65 ### swap in defaults, errors and js_validation
66 my $content = $cgix->swap_template(get_content_form
(), $swap);
69 $cgix->fill(\
$content, $form);
73 $cgix->print_content_type();
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
());
87 ###----------------------------------------------------------------###
91 'group order' => ['username', 'password'],
97 # could probably all be done with match => 'm/^\w{3,30}$/'
104 validate_if
=> 'password',
105 equals
=> 'password',
112 title
=> 'My Application',
113 script
=> $ENV{SCRIPT_NAME
},
114 color
=> ['#ccf', '#aaf'],
118 ###----------------------------------------------------------------###
120 sub get_content_form
{
124 <title>[% title %]</title>
134 <h1 style
='color:blue'>Please Enter information
</h1
>
135 <span style
='color:red'>[% error_header
%]</span
>
138 <form name
="[% form_name %]">
139 <input type
=hidden name
=processing value
=1>
142 <tr bgcolor
=[% color
.0 %]>
145 <input type
=text size
=30 name
=username
>
146 <span
class=error id
=username_error
>[% username_error
%]</span></td
>
148 <tr bgcolor
=[% color
.1 %]>
150 <td
><input type
=password size
=20 name
=password
>
151 <span
class=error id
=password_error
>[% password_error
%]</span></td
>
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
>
158 <tr bgcolor
=[% color
.1 %]>
159 <td colspan
=2 align
=right
><input type
=submit value
=Submit
></td
>
172 sub get_content_success
{
175 <head><title>[% title %]</title></head>
177 <h1 style='color:green'>Success</h1>
179 print "I can now continue on with the rest of my script!";
This page took 0.048194 seconds and 4 git commands to generate.