Subject: | Field::Basename does not seem to handle filenames correctly |
When I use F::S::Field::FileSelector, I get errors like
Can't use string ("thefilename") as an ARRAY ref while "strict refs" in use at
...Form/Sensible/Field/FileSelector.pm line 14
Looking at F::S::Field::FileSelector, it looks like there's a confusion about the return value of
File::Basename::fileparse that results in this error. I've attached a patch that I believe addresses
this problem. it includes a test that demonstrates the problem and a one line patch to fix it.
Please look this over before simply applying the patch. It's entirely possible if not likely that I'm
confused about how this stuff is supposed to work and I'm doing it wrong. But, this makes my
code work better.
Subject: | 0001-fixing-problem-in-FileSelector-with-usage-of-File-Ba.patch |
From 184c83630cbbf5fc3fccca8ada4e107298d06685 Mon Sep 17 00:00:00 2001
From: Andew Moore <amoore@mooresystems.com>
Date: Thu, 27 Jan 2011 14:25:25 -0600
Subject: [PATCH] fixing problem in FileSelector with usage of File::Basename
It looks like Form::Sensible::Field::FileSelector was depending on different behavior of File::Basename.
This patch gets rid of an error of expecting the wrong type back from File:Basename::fileparse.
---
lib/Form/Sensible/Field/FileSelector.pm | 2 +-
t/fields/fileselector.t | 32 +++++++++++++++++++++++++++++++
2 files changed, 33 insertions(+), 1 deletions(-)
create mode 100644 t/fields/fileselector.t
diff --git a/lib/Form/Sensible/Field/FileSelector.pm b/lib/Form/Sensible/Field/FileSelector.pm
index 97a5b50..534f9f7 100644
--- a/lib/Form/Sensible/Field/FileSelector.pm
+++ b/lib/Form/Sensible/Field/FileSelector.pm
@@ -11,7 +11,7 @@ has 'filename' => (
isa => 'Str',
lazy => 1,
default => sub {
- return ${fileparse(shift->full_path)}[0];
+ return scalar fileparse(shift->full_path);
}
);
diff --git a/t/fields/fileselector.t b/t/fields/fileselector.t
new file mode 100644
index 0000000..0a00f04
--- /dev/null
+++ b/t/fields/fileselector.t
@@ -0,0 +1,32 @@
+use Test::More;
+use FindBin;
+use lib "$FindBin::Bin/../lib";
+use Data::Dumper;
+use Form::Sensible;
+
+use Form::Sensible::Form;
+
+my $lib_dir = $FindBin::Bin;
+my @dirs = split '/', $lib_dir;
+pop @dirs;
+$lib_dir = join('/', @dirs);
+
+my $form = Form::Sensible->create_form( {
+ name => 'test',
+ fields => [
+{
+ field_class => 'FileSelector',
+ name => 'upload_file',
+ valid_extensions => [ "jpg", "gif", "png" ],
+ maximum_size => 262144,
+},
+ ],
+ } );
+
+$form->set_values( {
+ upload_file => '/etc/motd',
+});
+my $validation_result = $form->validate();
+ok( !$validation_result->is_valid(), 'file upload with wrong extension' );
+
+done_testing();
--
1.7.2.2