AxKit-XSP-PerForm

 view release on metacpan or  search on metacpan

PerForm.pm  view on Meta::CPAN

$NS = 'http://axkit.org/NS/xsp/perform/v1';

@ISA = qw(Apache::AxKit::Language::XSP);

@EXPORT_TAGLIB = (
    'textfield($name;$default,$width,$maxlength,$index,$onvalidate,$onload,$disabled,$onchange)',
    'password($name;$default,$width,$maxlength,$index,$onvalidate,$onload,$disabled,$onchange)',
    'submit($name;$value,$image,$alt,$border,$align,$goto,$index,$onsubmit,$disabled,$onclick)',
    'cancel($name;$value,$image,$alt,$border,$align,$goto,$index,$oncancel,$disabled,$onclick)',
    'checkbox($name;$value,$checked,$label,$index,$onvalidate,$onload,$disabled,$onclick)',
    'file_upload($name;$value,$accept,$onvalidate,$onload,$disabled,$onclick)',
    'hidden($name;$value,$index,$onload)',
    'textarea($name;$cols,$rows,$wrap,$default,$index,$onvalidate,$onload,$disabled,$onchange)',
    'single_select($name;$default,$index,$onvalidate,$onload,$disabled,$onchange,*options):itemtag=option',
    'multi_select($name;@default,$index,$onvalidate,$onload,$disabled,$onclick,*option):itemtag=option',
);

use strict;

sub parse_char  { 
    Apache::AxKit::Language::XSP::TaglibHelper::parse_char(@_);

PerForm.pm  view on Meta::CPAN

            ( $checked ? (checked => "checked") : () ),
            label => $label,
            ( $error ? (error => $error) : () ),
	    index => $index,
            ($disabled ? (disabled => $disabled) : ()),
            ($onclick ? (onclick => $onclick) : ()),
        }
    };
}

sub file_upload ($;$$$$$$) {
    my ($name, $value, $accept, $onval, $onload, $disabled, $onclick) = @_;
    my ($package) = caller;
    
    no strict 'refs';
    
    my $ctxt = ${"${package}::_form_ctxt"};
    
    my $params = $ctxt->{Form};
    my $fname = $ctxt->{Name};
    
    my $error;
    
    # validate
    if ($params->{"__submitting_$fname"}) {
        if (my $sub = $package->can($onval || "validate_${name}")) {
            my $upload = Apache::Request->instance(Apache->request)->upload($name);
            
            my $filename;
            if ($upload) {
                $filename = $upload->filename;
                $filename =~ s/.*[\\\/]//; # strip to just a filename
                $filename =~ s/[^\w\.-]//g; # strip non-word chars
            }
    
            eval {
               $sub->($ctxt, 
                       ($upload ? 
                            (   $filename,
                                $upload->fh, 
                                $upload->size, 
                                $upload->type, 
                                $upload->info
                            ) : 
                            ()
                       )
                   );
            };
            $error = $@;
            $ctxt->{_Failed}++ if $error;
            $error =~ s/(.*) at .*? line \d+\.$/$1/;
        }
    }
    # load
    elsif (my $sub = $package->can($onload || "load_${name}")) {
        $params->{$name} = $sub->($ctxt, $value, $params->{$name});
    }
    else {
        $params->{$name} = $value;
    }
    
    return {
        file_upload => {
            name => $name,
            value => $params->{$name},
            accept => $accept,
            ($disabled ? (disabled => $disabled) : ()),
            ($onclick ? (onclick => $onclick) : ()),
            ($error ? (error => $error) : ()),
        }
    };
}

PerForm.pm  view on Meta::CPAN

    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 file-upload elements.

=head1 XSP INHERITANCE

Starting with AxKit 1.6.1 it is possible to specify a class which your XSP
page inherits from. All the validate, load, submit and cancel functions can
be in the class you inherit from, reducing code duplication, memory usage,
and complexity.

=head1 SPECIFYING CALLBACKS

PerForm.pm  view on Meta::CPAN

not, 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, $index )

Validate the value in the checkbox. Throw an exception to indicate
validation failure.

=back

=head2 <f:file-upload/>

A file upload field (normally in HTML, a text entry box, and a "Browse..."
button).

B<Attributes:>

=over 4

=item name (mandatory)

The name of the file upload field.

=item value

A default filename to put in the box. Use with care because putting
something in here is not very user friendly!

=item accept

A list of MIME types to accept in this dialog box. Some browsers might use
this in the Browse dialog to restrict the list of files to show.

PerForm.pm  view on Meta::CPAN

forms in plain HTML.

=back

B<Callbacks:>

=over 4

=item load_<name> ( $ctxt, $default, $current )

Load a new value into the file upload field. Return the value to go in the
field.

=item validate_<name> ( $ctxt, $filename, $fh, $size, $type, $info )

Validate the uploaded file. This is also actually the place where you would
save the file to disk somewhere, by reading from $fh and writing to
somewhere else, or using File::Copy to do that for you. It is much harder to
access the file from the submit callback.

If the file is somehow invalid, throw an exception with the text of why it
is invalid.

=back

=head2 <f:hidden/>

stylesheets/perform_html.xps  view on Meta::CPAN

  else {
    $wrap = '';
  }
  $t->{'pre'} = qq|
    <textarea name="$name$index" cols="$cols" rows="$rows" $wrap >$value|;
  $t->{'post'} = q|  </textarea> <br /> |.apply_templates('error', $node);
  $t->{'showtag'} = 0;
  return -1;
};

$t->{'file_upload'}{'testcode'} = sub {
  my ($node, $t) = @_;
  my $name = findvalue('@name|name', $node);
  my $value = findvalue('@value|value', $node);
  my $accept = findvalue('@accept|accept', $node);
  $t->{'pre'} = qq|
    <input type="file"  name="$name" accept="$accept" />|.apply_templates('error', $node);
  $t->{'showtag'} = 0;
  return -1;
};



( run in 2.345 seconds using v1.01-cache-2.11-cpan-b16cb0d3907 )