CGI-FormBuilder
view release on metacpan or search on metacpan
lib/CGI/FormBuilder.pm view on Meta::CPAN
435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
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
525526527528529530531532533534535536537538539540541542543544545This 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.
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
550551552553554555556557558559560561562563564565566567568569570
$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
100101102103104105106107108109110111112113114115116117118119120}
# 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
138139140141142143144145146147148149150151152153154155156157158159
}
}
# 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
979899100101102103104105106107108109110111112113114115116117for
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
129130131132133134135136137138139140141142143144145146147148149150
}
}
# 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
123456789101112131415<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
202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
<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
252627282930313233343536373839404142434445464748495051525354<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 )