Date: Thursday January 24, 2002 @ 8:16
Author: matt
Update of /home/cvs/AxKit-XSP-PerForm
In directory ted:/home/matt/Perl/AxKit-XSP-PerForm
Modified Files:
MANIFEST PerForm.pm
Log Message:
Added XPS stylesheet to MANIFEST
Matthew Smith's patch to allow list of form elements
Index: MANIFEST
===================================================================
RCS file: /home/cvs/AxKit-XSP-PerForm/MANIFEST,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- MANIFEST 2001/06/10 16:39:04 1.1
+++ MANIFEST 2002/01/24 08:16:04 1.2
@@ -5,3 +5,4 @@
PerForm.pm
t/00basic.t
stylesheets/perform_html.xsl
+stylesheets/perform_html.xps
Index: PerForm.pm
===================================================================
RCS file: /home/cvs/AxKit-XSP-PerForm/PerForm.pm,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- PerForm.pm 2001/07/05 20:32:35 1.7
+++ PerForm.pm 2002/01/24 08:16:04 1.8
@@ -1,4 +1,4 @@
-# $Id: PerForm.pm,v 1.7 2001/07/05 20:32:35 matt Exp $
+# $Id: PerForm.pm,v 1.8 2002/01/24 08:16:04 matt Exp $
package AxKit::XSP::PerForm;
@@ -14,14 +14,14 @@
@ISA = qw(Apache::AxKit::Language::XSP);
@EXPORT_TAGLIB = (
- 'textfield($name;$default,$width,$maxlength)',
- 'password($name;$default,$width,$maxlength)',
- 'submit($name;$value,$image,$alt,$border,$align,$goto)',
- 'cancel($name;$value,$image,$alt,$border,$align,$goto)',
- 'checkbox($name;$value,$checked,$label)',
+ 'textfield($name;$default,$width,$maxlength,$index)',
+ 'password($name;$default,$width,$maxlength,$index)',
+ 'submit($name;$value,$image,$alt,$border,$align,$goto,$index)',
+ 'cancel($name;$value,$image,$alt,$border,$align,$goto,$index)',
+ 'checkbox($name;$value,$checked,$label,$index)',
'file_upload($name;$value,$accept)',
- 'hidden($name;$value)',
- 'textarea($name;$cols,$rows,$wrap,$default)',
+ 'hidden($name;$value,$index)',
+ 'textarea($name;$cols,$rows,$wrap,$default,$index)',
'single_select($name):itemtag=option',
'multi_select($name):itemtag=option',
);
@@ -48,6 +48,11 @@
{ Name => "enctype", Value => "multipart/form-data" },
],
};
+#MSS
+# if (Apache->args) {
+# $form_el->{Attributes}[1]{Value} .='?'.Apache->args;
+# }
+#end MSS
$e->start_element($form_el);
@@ -64,12 +69,14 @@
return <<EOT
{
-use vars qw(\$_form_ctxt \@_submit_buttons \%_submit_goto \@_cancel_buttons
\%_cancel_goto);
+use vars qw(\$_form_ctxt \@_submit_buttons \%_submit_goto \%_submit_index
+\@_cancel_buttons \%_cancel_goto \%_cancel_index );
local \$_form_ctxt = { Form => \$cgi->parms, Apache => \$r };
local \@_submit_buttons;
local \@_cancel_buttons;
local \%_submit_goto;
local \%_cancel_goto;
+local \%_submit_index;
+local \%_cancel_index;
start_form_$attribs{name}(\$_form_ctxt, \$cgi->param('__submitting'))
if defined \&start_form_$attribs{name};
EOT
@@ -102,14 +109,16 @@
end_form_${name}(\$_form_ctxt, \$cgi->param('__submitting'))
if defined \&end_form_${name};
+#warn("submitting? ".(\$cgi->param('__submitting')?"yes":"no").", failed?
+".(\$_form_ctxt->{_Failed}?"yes":"no"));
+
if (\$cgi->param('__submitting')) {
foreach my \$cancel (\@_cancel_buttons) {
if (\$cgi->param(\$cancel)) {
no strict 'refs';
my \$redirect;
\$redirect = \$_cancel_goto{\$cancel};
- \$redirect = "cancel_\${cancel}"->(\$_form_ctxt)
- if defined \&{"cancel_\${cancel}"};
+ \$redirect = "cancel_\$_cancel_index{\$cancel}{'name'}"->(\$_form_ctxt,
+\$_cancel_index{\$cancel}{'index'})
+ if defined \&{"cancel_\$_cancel_index{\$cancel}{'name'}"};
if (\$redirect) {
AxKit::XSP::WebUtils::redirect(\$redirect);
}
@@ -118,13 +127,13 @@
}
if (\$cgi->param('__submitting') && !\$_form_ctxt->{_Failed}) {
- foreach my \$submit (\@_submit_buttons) {
+ foreach my \$submit (\@_submit_buttons) {
if (\$cgi->param(\$submit)) {
no strict 'refs';
my \$redirect;
\$redirect = \$_submit_goto{\$submit};
- \$redirect = "submit_\${submit}"->(\$_form_ctxt)
- if defined \&{"submit_\${submit}"};
+ \$redirect = "submit_\$_submit_index{\$submit}{'name'}"->(\$_form_ctxt,
+\$_submit_index{\$submit}{'index'})
+ if defined \&{"submit_\$_submit_index{\$submit}{'name'}"};
if (\$redirect) {
AxKit::XSP::WebUtils::redirect(\$redirect);
}
@@ -132,6 +141,7 @@
}
}
+
}
EOT
}
@@ -140,8 +150,8 @@
}
}
-sub textfield ($;$$$) {
- my ($name, $default, $width, $maxlength) = @_;
+sub textfield ($;$$$$) {
+ my ($name, $default, $width, $maxlength, $index) = @_;
my ($package) = caller;
no strict 'refs';
@@ -156,7 +166,7 @@
if ($params->{'__submitting'}) {
if (defined &{"${package}::validate_${name}"}) {
eval {
- "${package}::validate_${name}"->($ctxt, $params->{$name});
+ "${package}::validate_${name}"->($ctxt, $params->{$name.$index},
+$index);
};
$error = $@;
$ctxt->{_Failed}++ if $error;
@@ -166,10 +176,10 @@
# load
if (defined &{"${package}::load_${name}"}) {
- $params->{$name} = "${package}::load_${name}"->($ctxt, $default,
$params->{$name});
+ $params->{$name.$index} = "${package}::load_${name}"->($ctxt, $default,
+$params->{$name.$index}, $index);
}
elsif (!$params->{'__submitting'}) {
- $params->{$name} = $default;
+ $params->{$name.$index} = $default;
}
return {
@@ -177,14 +187,15 @@
width => $width,
maxlength => $maxlength,
name => $name,
- value => $params->{$name},
+ value => $params->{$name.$index},
+ index => $index,
($error ? (error => $error) : ()),
}
};
}
-sub submit ($;$$$$$$) {
- my ($name, $value, $image, $alt, $border, $align, $goto) = @_;
+sub submit ($;$$$$$$$) {
+ my ($name, $value, $image, $alt, $border, $align, $goto, $index) = @_;
my ($package) = caller;
no strict 'refs';
@@ -192,8 +203,10 @@
my $ctxt = ${"${package}::_form_ctxt"};
my $params = $ctxt->{Form};
- push @{"${package}::_submit_buttons"}, $name;
- ${"${package}::_submit_goto"}{$name} = $goto if $goto;
+ push @{"${package}::_submit_buttons"}, "$name$index";
+ ${"${package}::_submit_goto"}{$name.$index} = $goto if $goto;
+ ${"${package}::_submit_index"}{$name.$index}{'index'} = $index;
+ ${"${package}::_submit_index"}{$name.$index}{'name'} = $name;
# save
if ($image) {
@@ -205,6 +218,7 @@
alt => $alt,
border => $border || 0,
align => $align || "bottom",
+ index => $index,
}
};
}
@@ -213,13 +227,14 @@
submit_button => {
name => $name,
value => $value,
+ index => $index,
}
};
}
}
-sub cancel ($;$$$$$$) {
- my ($name, $value, $image, $alt, $border, $align, $goto) = @_;
+sub cancel ($;$$$$$$$) {
+ my ($name, $value, $image, $alt, $border, $align, $goto, $index) = @_;
my ($package) = caller;
no strict 'refs';
@@ -227,8 +242,10 @@
my $ctxt = ${"${package}::_form_ctxt"};
my $params = $ctxt->{Form};
- push @{"${package}::_cancel_buttons"}, $name;
- ${"${package}::_cancel_goto"}{$name} = $goto if $goto;
+ push @{"${package}::_cancel_buttons"}, $name.$index;
+ ${"${package}::_cancel_goto"}{$name.$index} = $goto if $goto;
+ ${"${package}::_cancel_index"}{$name.$index}{'index'} = $index;
+ ${"${package}::_cancel_index"}{$name.$index}{'name'} = $name;
# save
if ($image) {
@@ -240,6 +257,7 @@
alt => $alt,
border => $border || 0,
align => $align || "bottom",
+ index => $index,
}
};
}
@@ -248,13 +266,14 @@
submit_button => {
name => $name,
value => $value,
+ index => $index,
}
};
}
}
-sub button ($;$) {
- my ($name, $value) = @_;
+sub button ($;$$) {
+ my ($name, $value, $index) = @_;
my ($package) = caller;
@@ -266,8 +285,8 @@
# TODO: What do we want buttons to do?
}
-sub checkbox ($;$$$) {
- my ($name, $value, $checked, $label) = @_;
+sub checkbox ($;$$$$) {
+ my ($name, $value, $checked, $label, $index) = @_;
my ($package) = caller;
no strict 'refs';
@@ -282,7 +301,7 @@
if ($params->{'__submitting'}) {
if (defined &{"${package}::validate_${name}"}) {
eval {
- "${package}::validate_${name}"->($ctxt, $params->{$name});
+ "${package}::validate_${name}"->($ctxt, $params->{$name.$index},
+$index);
};
$error = $@;
$ctxt->{_Failed}++ if $error;
@@ -294,12 +313,12 @@
# load
if (defined &{"${package}::load_${name}"}) {
- my @vals = "${package}::load_${name}"->($ctxt, $params->{$name});
+ my @vals = "${package}::load_${name}"->($ctxt, $params->{$name.$index},
+$index);
$checked = shift @vals;
$value = shift @vals if @vals;
}
elsif ($params->{'__submitting'}) {
- $checked = 1 if defined($params->{$name});
+ $checked = 1 if defined($params->{$name.$index});
}
if ($checked && $checked eq 'yes') {
@@ -316,6 +335,7 @@
( $checked ? (checked => "checked") : () ),
label => $label,
( $error ? (error => $error) : () ),
+ index => $index,
}
};
}
@@ -381,29 +401,28 @@
};
}
-sub hidden ($;$) {
- my ($name, $value) = @_;
+sub hidden ($;$$) {
+ my ($name, $value, $index) = @_;
my ($package) = caller;
no strict 'refs';
my $ctxt = ${"${package}::_form_ctxt"};
-
my $params = $ctxt->{Form};
-
+
if (!defined($value) && defined &{"${package}::load_${name}"}) {
- # load value if not defined
- $value = "${package}::load_${name}"->($ctxt, $value);
+ # load value if not defined
+ $value = "${package}::load_${name}"->($ctxt, $value, $index);
}
-
- if ($params->{'__submitting'} && ($value ne $params->{$name})) {
- die "Someone tried to change your hidden form value!";
+ if ($params->{'__submitting'} && ($value ne $params->{$name.$index})) {
+ die "Someone tried to change your hidden form value!";
}
-
+
return {
- hidden => {
+ hidden => {
name => $name,
value => $value,
+ index => $index,
}
};
}
@@ -464,8 +483,8 @@
};
}
-sub password ($;$$$) {
- my ($name, $default, $width, $maxlength) = @_;
+sub password ($;$$$$) {
+ my ($name, $default, $width, $maxlength, $index) = @_;
my ($package) = caller;
no strict 'refs';
@@ -480,7 +499,7 @@
if ($params->{'__submitting'}) {
if (defined &{"${package}::validate_${name}"}) {
eval {
- "${package}::validate_${name}"->($ctxt, $params->{$name});
+ "${package}::validate_${name}"->($ctxt, $params->{$name,$index},
+$index);
};
$error = $@;
$ctxt->{_Failed}++ if $error;
@@ -490,10 +509,10 @@
# load
if (defined &{"${package}::load_${name}"}) {
- $params->{$name} = "${package}::load_${name}"->($ctxt, $default,
$params->{$name});
+ $params->{$name.$index} = "${package}::load_${name}"->($ctxt, $default,
+$params->{$name,$index}, $index);
}
elsif (!$params->{'__submitting'}) {
- $params->{$name} = $default;
+ $params->{$name.$index} = $default;
}
return {
@@ -503,6 +522,7 @@
name => $name,
value => $params->{$name},
($error ? (error => $error) : ()),
+ index => $index,
}
};
}
@@ -576,8 +596,8 @@
};
}
-sub textarea ($;$$$$) {
- my ($name, $cols, $rows, $wrap, $default) = @_;
+sub textarea ($;$$$$$) {
+ my ($name, $cols, $rows, $wrap, $default, $index) = @_;
my ($package) = caller;
@@ -593,7 +613,7 @@
if ($params->{'__submitting'}) {
if (defined &{"${package}::validate_${name}"}) {
eval {
- "${package}::validate_${name}"->($ctxt, $params->{$name});
+ "${package}::validate_${name}"->($ctxt, $params->{$name.$index},
+$index);
};
$error = $@;
$ctxt->{_Failed}++ if $error;
@@ -603,10 +623,10 @@
# load
if (defined &{"${package}::load_${name}"}) {
- $params->{$name} = "${package}::load_${name}"->($ctxt, $default,
$params->{$name});
+ $params->{$name.$index} = "${package}::load_${name}"->($ctxt, $default,
+$params->{$name.$index}, $index);
}
elsif (!$params->{'__submitting'}) {
- $params->{$name} = $default;
+ $params->{$name.$index} = $default;
}
if ($wrap) {
@@ -626,6 +646,7 @@
name => $name,
value => $params->{$name},
($error ? (error => $error) : ()),
+ index => $index,
}
};
}
@@ -760,6 +781,42 @@
And you can later get at that in another callback via C<$ctxt->{my_key}>.
+=head1 ARRAYED FORM ELEMENTS
+
+Sometimes you need to display a list of items in your form where the
+number of items is not known until runtime. Use arrayed form elements to trigger
+the same callback for each item in the list. When setting up each element, use
+an index to identify each member of the list. The callbacks will be passed the
+index as a parameter. e.g.
+
+Your form may have a section like this:
+
+ <xsp:logic>
+ for $index (0..$#shoppinglist) {
+ <p>
+ <xsp:expr>$shoppinglist[$index]</xsp:expr>
+ <f:submit name="SubmitBuy" value="Buy me">
+ <f:index><xsp:expr>$index</xsp:expr></f:index>
+ </f:submit>
+ </p>
+ }
+ </xsp:logic>
+
+The submit callback might be:
+
+ sub submit_SubmitBuy {
+ my ($ctxt, $index) = @_;
+ return "purchase.xsp?item=$index";
+ }
+
+This example produces a list of items with a 'Buy me' button next to each one. Each
+button has an index that corresponds an array index of an item in the shopping list.
+When one of the submit buttons is pressed, the submit_SubmitBuy callback will be
+triggered (as part of the submission procedure) and the browser will redirect to a
+page
+that handles the purchase of the associated item.
+
+NOTE: arrays not supported for multi-select, single-select or file-upload elements.
+
=head1 TAG DOCUMENTATION
The following documentation uses the prefix I<f:> for all PerForm tags. This assumes
you
@@ -854,17 +911,24 @@
you won't use this unless you happen to not want to save the form values
in any way.
+=item index
+
+If your button is a member of an array, then set the index attribute to the
+corresponding array index.
+
=back
B<Callbacks:>
=over 4
-=item submit_<name> ( $ctxt )
+=item submit_<name> ( $ctxt , $index )
This callback is used to "do something" with the submitted form values. You
might write them to a database or a file, or change something in your application.
+The $index parameter identifies which button was pressed in an array of buttons.
+
The return value from submit_<name> is used to redirect the user to the "next"
page, whatever that might be.
@@ -885,7 +949,7 @@
=over 4
-=item cancel_<name> ( $ctxt )
+=item cancel_<name> ( $ctxt, $index )
Implement this method to override the goto attribute. Return the URI you want to
redirect
to. This can be used to dynamically generate the URI to redirect to.
@@ -919,13 +983,18 @@
The maximum number of characters you can enter into this text field.
+=item index
+
+If your text field is a member of an array, then set the index attribute to the
+corresponding array index.
+
=back
B<Callbacks:>
=over 4
-=item load_<name> ( $ctxt, $default, $current )
+=item load_<name> ( $ctxt, $default, $current, $index )
Used to load a value into the edit box. The default is from the attributes above. The
current value is only set if this form has been submitted once already, and contains
@@ -933,10 +1002,12 @@
Simply return the value you want to appear in the textfield.
+If the text field is a memeber of an array, then $index will be the array index.
+
If you do not implement this method, the value in the textfield defaults to
C<$current || $default>.
-=item validate_<name> ( $ctxt, $value )
+=item validate_<name> ( $ctxt, $value, $index )
Implement this method to validate the contents of the textfield. If the value is
valid,
you don't need to do anything. However if it invalid, throw an exception with the
reason
@@ -951,6 +1022,8 @@
die "Invalid characters" if $value =~ /\W/;
}
+If the text field is a memeber of an array, then $index will be the array index.
+
=back
=head2 <f:password/>
@@ -984,13 +1057,17 @@
Used in HTML 4.0, the label for the checkbox. Use this with care as most browsers
don't support it.
+=item index
+
+Use this to identify the array index when using arrayed form elements.
+
=back
B<Callbacks:>
=over 4
-=item load_<name> ( $ctxt, $current )
+=item load_<name> ( $ctxt, $current, $index )
If you implement this method, you can change the default checked state of the
checkbox, and the value returned by the checkbox if you need to.
@@ -999,7 +1076,7 @@
and the second optional value is what value is sent to the server when the checkbox
is checked and submitted.
-=item validate_<name> ( $ctxt, $value )
+=item validate_<name> ( $ctxt, $value, $index )
Validate the value in the checkbox. Throw an exception to indicate validation failure.
@@ -1069,13 +1146,17 @@
The value stored in the hidden field
+=item index
+
+Use this to identify the array index when using arrayed form elements.
+
=back
B<Callbacks:>
=over 4
-=item load_<name> ( $ctxt, $default )
+=item load_<name> ( $ctxt, $default, $index )
If you wish the value to be dynamic somehow, implement this callback and return a
new value for the hidden field.
@@ -1113,17 +1194,21 @@
The default text to put in the textarea.
+=item index
+
+Use this to identify the array index when using arrayed form elements.
+
=back
B<Callbacks:>
=over 4
-=item load_<name> ( $ctxt, $default, $current )
+=item load_<name> ( $ctxt, $default, $current, $index )
Load a new value into the widget. Return the string you want to appear in the box.
-=item validate_<name> ( $ctxt, $value )
+=item validate_<name> ( $ctxt, $value, $index )
Validate the contents of the textarea. If the contents are somehow invalid, throw
an exception in your code with the string of the error. One use for this might be
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]