source: text-formbuilder/trunk/lib/Text/FormBuilder/Examples.pod @ 73

Last change on this file since 73 was 46, checked in by peichman, 20 years ago

added examples documentation; documented the create_form function; updated README

File size: 5.4 KB
Line 
1=head1 NAME
2
3Examples - Sample uses of Text::FormBuilder
4
5=head1 EXAMPLES
6
7=head2 Event Form
8
9This is the formspec for an input form for an event calendar. It exercises many
10of the features of the formspec language. This is close to a real world project
11that I have been developing.
12
13    !title Add Event
14    !author Peter Eichman
15    !description {
16        Start and end times are not required, but are recommended. If you leave
17        both of them blank, the event will be considered an all day event.
18    }
19   
20    !pattern TIME /^\s*\d{1,2}(:\d{2})?(\s*[ap]m)?\s*$/
21    !pattern DAY  /^\s*(([1-3][0-9])|[1-9])\s*$/
22    !pattern YEAR /^\s*\d{4}\s*$/
23   
24    !list MONTHS {
25        1[January],    2[February], 3[March],     4[April],
26        5[May],        6[June],     7[July],      8[August],
27        9[September], 10[October], 11[November], 12[December]
28    }
29   
30    !group DATE {
31        month@MONTHS//VALUE
32        day[2]//DAY
33        year[4]//YEAR
34    }
35   
36    !group TIME {
37        start[8]|' '//TIME?
38        end[8]|'–'[(hh:mm am/pm)]//TIME?
39    }
40   
41    !group SERIES {
42        old|Existing:select
43        new[40]|or New
44    }
45   
46    # input fields start here
47   
48    event_type:select//VALUE
49   
50    title[60]//VALUE
51   
52    !field %DATE date
53    !field %TIME time
54   
55    series_old|Existing series:select
56    series_new[60]|New series
57   
58    description[6,60]:textarea
59    contact[60]
60    email[40]//EMAIL?
61    location[60]
62    url[60]|Website
63
64Both of the fields C<event_type> and C<series_old> get filled in
65from a database in the actual CGI script. The relevant bits of
66the CGI script which uses this form go something like this:
67
68    # the module containing the FormBuilder-building code
69    use Calendar::Forms::AddEvent;
70    my $form = Calendar::Forms::AddEvent::get_form($q);
71   
72    # now we have a CGI::FormBuilder object in $form
73   
74    # fill in dropdown lists
75    $form->field(name => 'event_type', values => \@event_types);
76    $form->field(name => 'series_old', values => \@existing_series);
77   
78    unless ($form->submitted && $form->validate) {
79        print $q->header;
80        print $form->render;
81    } else {
82        # process the data ...
83    }
84
85=head1 CGI::FormBuilder EXAMPLES
86
87Here are some of L<CGI::FormBuilder>'s examples, translated into
88Text::FormBuilder's terms.
89
90=head2 Ex1: order.cgi
91
92Formspec F<example1>:
93
94    !title Order Info
95   
96    !list STATES {
97        AL, AK, AZ, AR, CA, CO, CT, DE, DC, FL, GE, HI, ID, IL, IN, IA, KS,
98        KY, LA, ME, MD, MA, MI, MN, MS, MO, MT, NE, NV, NH, NJ, NM, NY, NC,
99        ND, OH, OK, OR, PA, RI, SC, SD, TN, TX, UT, VT, VA, WA, WV, WI, WY
100    }
101   
102    first_name
103    last_name
104    email//EMAIL
105    address
106    state@STATES
107    zipcode//ZIPCODE
108    credit_card//CARD
109    details[10,50]:textarea
110
111Parse and create F<Example1.pm>:
112
113    $ perl -MText::FormBuilder \
114        -e'Text::FormBuilder->parse("example1")->build(method => "POST") \
115        ->write_module("MyForms::Example1")'
116
117Script:
118
119    #!/usr/bin/perl -w
120    use strict;
121   
122    use CGI;    # you have to use CGI.pm explicitly
123    use MyForms::Example1;
124   
125    my $q = CGI->new;
126   
127    my $form = MyForms::Example1::get_form($q);
128   
129    # try to validate it first
130    if ($form->submitted && $form->validate) {
131        # ... more code goes here to do stuff ...
132        print $form->confirm;
133    } else {
134        print $form->render;
135    }
136
137=head2 Ex2: order_form.cgi
138
139You can also include the formspec in your script; the only downside to this
140is that your script has to parse the spec every time it gets called, so this
141method is definitely I<not> recommended for high-traffic forms.
142
143Script F<order_form.cgi>:
144
145    #!/usr/bin/perl -w
146    use strict;
147   
148    use CGI;    # you have to use CGI.pm explicitly
149    use Text::FormBuilder;
150   
151    my $parser = Text::FormBuilder->parse_text(q[
152    first_name
153    last_name
154    email
155    address
156    state@STATE
157    zipcode
158    credit_card
159    details[10,50]:textarea
160    ]);
161   
162    my $q = CGI->new;
163    $parser->build(params => $q, method => 'POST', smartness => 2, debug => 2);
164   
165    my $form = $parser->form;
166   
167    # try to validate it first
168    if ($form->submitted && $form->validate) {
169        # ... more code goes here to do stuff ...
170        print $form->confirm;
171    } else {
172        print $form->render;
173    }
174
175=head2 Ex4: user_info.cgi
176
177    #!/usr/bin/perl -w
178    use strict;
179   
180    use Text::FormBuilder;
181    use CGI;
182    use DBI;
183   
184    my $dbh = DBI->connect('dbi:Oracle:db', 'user', 'pass');
185   
186    my $parser = Text::FormBuilder->parse_text(q[
187    username
188    password
189    confirm_password
190    first_name
191    last_name
192    email
193    ]);
194   
195   
196    my $q = CGI->new;
197    my $form = $parser->build(params => $q)->form;
198   
199    # Now get the value of the username from our app
200    my $user = $form->cgi_param('user');
201    my $sth = $dbh->prepare("select * from user_info where user = '$user'");
202    $sth->execute;
203    my $default_hashref = $sth->fetchrow_hashref;
204    # Render our form with the defaults we got in our hashref
205    print $form->render(values => $default_hashref,
206                        title  => "User information for '$user'",
207    );
208
209=head1 SEE ALSO
210
211L<Text::FormBuilder>,
212L<CGI::FormBuilder>
213
214=head1 AUTHOR
215
216Peter Eichman, C<< <peichman@cpan.org> >>
217
218=head1 COPYRIGHT AND LICENSE
219
220Copyright E<copy>2004 by Peter Eichman.
221
222This program is free software; you can redistribute it and/or
223modify it under the same terms as Perl itself.
224
225=cut
Note: See TracBrowser for help on using the repository browser.