CGI-FormBuilder

 view release on metacpan or  search on metacpan

lib/CGI/FormBuilder.pm  view on Meta::CPAN

435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
                if ($k) {
                    push @{$self->{fieldsets}}, [$k,$v];
                }
            }
        } else {
            puke "Invalid usage of \$form->fieldsets(name => 'Label')"
        }
    }
 
    # We look for all the fieldset definitions, checking the main
    # form for a "proper" legend ala our other settings. We then
    # divide up all the fields and group them in fieldsets.
    my(%legends, @sets);
    for (optalign($self->{fieldsets})) {
        my($o,$n) = optval($_);
        next if exists $legends{$o};
        push @sets, $o;
        debug 2, "added fieldset $o (legend=$n) to \@sets";
        $legends{$o} = $n;
    }
 
    # find *all* our fieldsets, even hidden in fields w/o Human Tags
    for ($self->field) {
        next unless my $o = $_->fieldset;
        next if exists $legends{$o};
        push @sets, $o;
        debug 2, "added fieldset $o (legend=undef) to \@sets";
        $legends{$o} = $o# use fieldset as <legend>
    }
    return wantarray ? @sets : \%legends;
}
 
sub fieldlist {
    my $self = shift;
    my @fields = @_ ? @_ : $self->field;
    my(%saw, @ret);
    for my $set ($self->fieldsets) {
        # reorder fields
        for (@fields) {
            next if $saw{$_};

lib/CGI/FormBuilder.pod  view on Meta::CPAN

525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
This allows you to define fieldsets for your form. Fieldsets are used
to group fields together. Fields are rendered in order, inside the
fieldset they belong to. If a field does not have a fieldset, it
is appended to the end of the form.
 
To use fieldsets, specify an arrayref of C<< <fieldset> >> names:
 
    fieldsets => [qw(account preferences contacts)]
 
You can get a different C<< <legend> >> tag if you specify a nested arrayref:
 
    fieldsets => [
        [ account  => 'Account Information' ],
        [ preferences => 'Website Preferences' ],
        [ contacts => 'Email and Phone Numbers' ],
    ]
 
If you're using the source file, that looks like this:
 
    fieldsets: account=Account Information,preferences=...

lib/CGI/FormBuilder.pod  view on Meta::CPAN

550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
    $form->field(name => 'last_name'fieldset => 'account');
    $form->field(name => 'email_me',   fieldset => 'preferences');
    $form->field(name => 'home_phone', fieldset => 'contacts');
    $form->field(name => 'work_phone', fieldset => 'contacts');
 
You can also automatically create a new C<fieldset> on the fly by
specifying a new one:
 
    $form->field(name => 'remember_me', fieldset => 'advanced');
 
To set the C<< <legend> >> in this case, you have two options.
First, you can just choose a more readable C<fieldset> name:
 
    $form->field(name => 'remember_me',
                 fieldset => 'Advanced');
 
Or, you can change the name using the C<fieldset> accessor:
 
    $form->fieldset(advanced => 'Advanced Options');
 
Note that fieldsets without fields are silently ignored, so you can

lib/CGI/FormBuilder/Template/Builtin.pm  view on Meta::CPAN

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
}
 
# Render hidden fields first
my @unhidden;
for my $field ($form->fieldlist) {
    push(@unhidden, $field), next if $field->type ne 'hidden';
    push @html, $field->tag;   # no label/etc for hidden fields
}
 
# Support fieldset => 'name' to organize by fieldset on the fly
my $legend = $form->fieldsets;
 
# Get table stuff and reused calls
my $table = $form->table(id => $form->idname($form->bodyname), class => $form->class);
my $tabn = 1;
push @html, $table if $table;
 
# Render regular fields in table
my $lastset;
for my $field (@unhidden) {
    if (my $set = $field->fieldset) {

lib/CGI/FormBuilder/Template/Builtin.pm  view on Meta::CPAN

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
            }
        }
 
        # Wrap fieldset in a <div> to allow jquery #tabs
        push @html, $form->div(id => $form->idname($form->tabname.$tabn++),
                               class => $form->class($form->tabname));
 
        (my $sn = lc $set) =~ s/\W+/_/g;
        push @html, htmltag('fieldset', id => $form->idname("_$sn"),
                                        class => $form->class('_set'));
        push @html, htmltag('legend') . ($legend->{$set}||$set) . htmltag('/legend')
          if defined $legend->{$set};
 
        # Wrap fields in a table
        push @html, $form->table if $table;
 
        $lastset = $set;
    }
} elsif ($lastset) {
    # ended <fieldset> defs before form has ended
    # remaining fields are not in a fieldset
    push @html, htmltag('/table') if $table;

lib/CGI/FormBuilder/Template/Div.pm  view on Meta::CPAN

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
for my $field ($form->fieldlist) {
    push(@unhidden, $field), next if $field->type ne 'hidden';
    push @html, $field->tag;   # no label/etc for hidden fields
}
 
my $div = $form->div(id => $form->idname($form->bodyname), class => $form->class);
my $tabn = 1;
push @html, $div if $div;
 
# Support fieldset => 'name' to organize by fieldset on the fly
my $legend = $form->fieldsets;
 
# Render regular fields in <div> for CSS happiness
my $lastset;
for my $field (@unhidden) {
    if (my $set = $field->fieldset) {
        # hooks (hack?) for fieldsets
        if ($set ne $lastset) {
            # close any open divs/fieldsets
            if ($lastset) {
                push @html, htmltag('/fieldset');

lib/CGI/FormBuilder/Template/Div.pm  view on Meta::CPAN

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
            }
        }
 
        # wrap fieldset in a <div> to allow jquery #tabs
        push @html, $form->div(id => $form->idname($form->tabname.$tabn++),
                               class => $form->class($form->tabname));
 
        (my $sn = lc $set) =~ s/\W+/_/g;
        push @html, htmltag('fieldset', id => $form->idname("_$sn"),
                                        class => $form->class('_set'));
        push @html, htmltag('legend') . ($legend->{$set}||$set) . htmltag('/legend')
          if defined $legend->{$set};
 
        $lastset = $set;
    }
} elsif ($lastset) {
    # ended <fieldset> defs before form has ended
    # remaining fields are not in a fieldset
    push @html, htmltag('/div') if $div;
    push @html, htmltag('/fieldset');
    push @html, $div;
    undef $lastset;     # avoid dup </fieldset> below

t/1a-test32.html  view on Meta::CPAN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<form action="TEST" class="fb_form" id="account" method="get" name="account">
<div class="fb_state" id="account_state"><input id="_submitted_account" name="_submitted_account" type="hidden" value="1" /></div>
<div class="fb_tab" id="account_tab1">
<fieldset class="fb_set" id="account_acct">
<legend>Account Information</legend>
<table class="fb">
<tr id="account_first_name_row">
  <td class="fb_label" id="account_first_name_label">First Name</td>
  <td class="fb_field" id="account_first_name_field"><input class="fb_input" id="first_name" name="first_name" type="text" /></td>
</tr>
<tr id="account_last_name_row">
  <td class="fb_label" id="account_last_name_label">Last Name</td>
  <td class="fb_field" id="account_last_name_field"><input class="fb_input" id="last_name" name="last_name" type="text" /></td>
</tr>
<tr id="account_email_row">

t/1a-test32.html  view on Meta::CPAN

20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
  <td class="fb_label" id="account_sex_label">Sex</td>
  <td class="fb_field" id="account_sex_field"><input class="fb_radio" id="sex_Yes" name="sex" type="radio" value="Yes" /> <label class="fb_option" for="sex_Yes">Yes</label>
<input class="fb_radio" id="sex_No" name="sex" type="radio" value="No" /> <label class="fb_option" for="sex_No">No</label>
</td>
</tr>
</table>
</fieldset>
</div>
<div class="fb_tab" id="account_tab2">
<fieldset class="fb_set" id="account_prefs">
<legend>User Preferences</legend>
<table class="fb">
<tr id="account_call_me_row">
  <td class="fb_label" id="account_call_me_label">Call Me</td>
  <td class="fb_field" id="account_call_me_field"><input class="fb_input" id="call_me" name="call_me" type="text" /></td>
</tr>
<tr id="account_email_me_row">
  <td class="fb_label" id="account_email_me_label">Email Me</td>
  <td class="fb_field" id="account_email_me_field"><input class="fb_input" id="email_me" name="email_me" type="text" /></td>
</tr>
</table>
</fieldset>
</div>
<div class="fb_tab" id="account_tab3">
<fieldset class="fb_set" id="account_phone">
<legend>Phone Number(s)</legend>
<table class="fb">
<tr id="account_home_phone_row">
  <td class="fb_label" id="account_home_phone_label">Home Phone</td>
  <td class="fb_field" id="account_home_phone_field"><input class="fb_input" id="home_phone" name="home_phone" type="text" /></td>
</tr>
<tr id="account_work_phone_row">
  <td class="fb_label" id="account_work_phone_label">Work Phone</td>
  <td class="fb_field" id="account_work_phone_field"><input class="fb_input" id="work_phone" name="work_phone" type="text" /></td>
</tr>
</table>
</fieldset>
</div>
<div class="fb_tab" id="account_tab4">
<fieldset class="fb_set" id="account_inline_created">
<legend>Inline Created</legend>
<table class="fb">
<tr id="account_new_set_row">
  <td class="fb_label" id="account_new_set_label">New Set</td>
  <td class="fb_field" id="account_new_set_field"><input class="fb_input" id="new_set" name="new_set" type="text" /></td>
</tr>
</table>
</fieldset>
</div>
<table class="fb" id="account_body">
<tr id="account_outside_1_row">

t/1a-test33.html  view on Meta::CPAN

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<p>Fields that are <span class="fb_required">highlighted</span> are required.</p>
<form action="TEST" class="fb_form" id="parts" method="post" name="parts" onsubmit="return validate_parts(this);">
<div class="fb_state" id="parts_state"><input id="_submitted_parts" name="_submitted_parts" type="hidden" value="1" /></div>
<div class="fb_extra" id="parts_extra"><input id="replacement" name="replacement" type="hidden" value="TRUE" />
<input id="action" name="action" type="hidden" value="Unsubscribe" />
<input id="name" name="name" type="hidden" value="Pete Peteson" />
<input id="extra" name="extra" type="hidden" value="junk" />
<input id="other_test" name="other_test" type="hidden" value="_other_other_test" /></div>
<div class="fb_tab" id="parts_tab1">
<fieldset class="fb_set" id="parts_acct">
<legend>Account Information</legend>
<div id="parts_ticket_row">
  <div class="fb_label" id="parts_ticket_label"><span class="fb_required">Ticket</span></div>
  <div class="fb_field" id="parts_ticket_field"><input class="fb_input" id="ticket" name="ticket" type="text" value="111" /></div>
</div>
</fieldset>
</div>
<div class="fb_tab" id="parts_tab2">
<fieldset class="fb_set" id="parts_prefs">
<legend>Part Information</legend>
<div id="parts_email_row">
  <div class="fb_label" id="parts_email_label">Email</div>
  <div class="fb_field" id="parts_email_field"><input class="fb_input" id="email" name="email" type="text" value="pete@peteson.com" /></div>
</div>
</div>
</fieldset>
<div class="fb" id="parts_body">
<div id="parts_user_row">
  <div class="fb_label" id="parts_user_label">User</div>
  <div class="fb_field" id="parts_user_field"><input class="fb_input" id="user" name="user" type="text" value="pete" /></div>



( run in 0.286 second using v1.01-cache-2.11-cpan-8d75d55dd25 )