| Form-Sensible documentation | Contained in the Form-Sensible distribution. |
value
The local filename of the file selected.full_pathfilename
The filename of the file as provided by the user. By default, this is the filename
only portion full_path. Extension based validation is performed on filename.maximum_size
The maximum file size allowed for the file.valid_extensions
An array ref containing the valid extensions for this file. must_exist
A true / false indicating whether the file must exist by the time the field is validated. Defaults to true.must_be_readable
A true / false indicating whether the file must be readable by the time the field is validated. Defaults to true.file_ref
A reference to the file. This will only be defined if appropriate for your interface type. This will be defined,
for example, within a Catalyst app to hold the Catalyst::Request::Upload object.
Form::Sensible::Field::FileSelector - Field used for file selection
use Form::Sensible::Field::FileSelector;
my $object = Form::Sensible::Field::FileSelector->new({
name => 'upload_file',
valid_extensions => [ "jpg", "gif", "png" ],
maximum_size => 262144,
});
This field represents a File. When the FileSelector field type is used, the user will be prompted to select a file. Depending on the user interface, it may be prompting for a local file or a file upload.
value
The local filename of the file selected.full_pathThe full local path to the file selected. NOTE that in the case that the filename provided
by the user is different from the actual file on the local filesystem (such as when using
Catalyst file upload) the filename portion of full_path may be different than the result
of filename. File based validation (such as file size, etc.) is performed on full_path.
filename
The filename of the file as provided by the user. By default, this is the filename
only portion full_path. Extension based validation is performed on filename.maximum_size
The maximum file size allowed for the file.valid_extensions
An array ref containing the valid extensions for this file. must_exist
A true / false indicating whether the file must exist by the time the field is validated. Defaults to true.must_be_readable
A true / false indicating whether the file must be readable by the time the field is validated. Defaults to true.file_ref
A reference to the file. This will only be defined if appropriate for your interface type. This will be defined,
for example, within a Catalyst app to hold the Catalyst::Request::Upload object.Jay Kuri - <jayk@cpan.org>
Ionzero LLC. http://ionzero.com/
Copyright 2009 by Jay Kuri <jayk@cpan.org>
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
| Form-Sensible documentation | Contained in the Form-Sensible distribution. |
package Form::Sensible::Field::FileSelector; use File::Basename; use Moose; use namespace::autoclean; extends 'Form::Sensible::Field'; has 'filename' => ( is => 'rw', isa => 'Str', lazy => 1, default => sub { return scalar fileparse(shift->full_path); } ); has 'full_path' => ( is => 'rw', isa => 'Str', lazy => 1, default => sub { return shift->value(); } ); has 'valid_extensions' => ( is => 'rw', isa => 'ArrayRef', required => 1, default => sub { return [] }, ); has 'maximum_size' => ( is => 'rw', isa => 'Int', required => 1, default => 0, lazy => 1, ); has 'must_exist' => ( is => 'rw', isa => 'Bool', required => 1, default => 1, lazy => 1, ); has 'must_be_readable' => ( is => 'rw', isa => 'Bool', required => 1, default => 1, lazy => 1, ); sub get_additional_configuration { my ($self) = @_; return { 'maximum_size' => $self->maximum_size, 'valid_extensions' => $self->valid_extensions, 'must_exist' => $self->must_exist, 'must_be_readable' => $self->must_be_readable }; } around 'validate' => sub { my $orig = shift; my $self = shift; my @errors; push @errors, $self->$orig(@_); # file must exist. if (defined($self->filename)) { if ($self->must_exist && ! -e $self->full_path) { push @errors, "_FIELDNAME_ does not exist."; } if ($#{$self->valid_extensions} != -1) { my $extensions = "." . join('|.', @{$self->valid_extensions}); if ($self->filename !~ /($extensions)$/) { push @errors, "_FIELDNAME_ is not a valid file type"; } } if ($self->must_be_readable && ! -r $self->full_path ) { push @errors, "_FIELDNAME_ is not readable"; } if ($self->maximum_size) { my $filesize = -s $self->full_path; if ($filesize > $self->maximum_size) { push @errors, "_FIELDNAME_ is too large"; } } } return @errors; }; __PACKAGE__->meta->make_immutable; 1; __END__