Permalink
Browse files

upload field, None wrapper, css_class rendering

  • Loading branch information...
1 parent 490985e commit 6dd53e09fee0fc5e3d39e2ae295efc4db88988f7 @gshank gshank committed Aug 31, 2009
View
1 MANIFEST.SKIP
@@ -15,4 +15,3 @@
^dev_notes.txt$
^*.Captcha.pm$
^t/captcha.t$
-^*.File.pm$
View
141 lib/HTML/FormHandler/Field/File.pm
@@ -1,141 +0,0 @@
-package HTML::FormHandler::Field::File;
-
-use Moose;
-use Moose::Util::TypeConstraints;
-use MooseX::AttributeHelpers;
-use Carp 'croak';
-use List::MoreUtils 'any';
-
-extends 'HTML::FormHandler::Field';
-
-our $VERSION = '0.01';
-
-=head1 NAME
-
-HTML::FormHandler::Field::File - File upload field
-
-=head1 DESCRIPTION
-
-Validates that the input is an uploaded file.
-
-=head1 DEPENDENCIES
-
-=head2 widget
-
-Widget type is 'file'
-
-=cut
-
-has '+widget' => ( default => 'file' );
-
-subtype 'ArrayRefOfStr' => as 'ArrayRef[Str]';
-
-coerce 'ArrayRefOfStr' => from 'Str' => via { [$_] };
-
-has 'accept' => (
- metaclass => 'Collection::Array',
- is => 'rw',
- isa => 'ArrayRefOfStr',
- predicate => 'has_accept',
- coerce => 1,
- default => sub { [] },
- provides => { elements => 'accept_list', }
-);
-
-has minimum => ( is => 'rw', isa => 'Int', default => 1 );
-has maximum => ( is => 'rw', isa => 'Int', default => 1_048_576 );
-
-sub BUILD
-{
- for my $form ( $_[0]->form ) {
- $form->enctype('multipart/form-data');
- $form->http_method('post');
- }
-}
-
-sub minimum_kilobyte
-{
- croak "minimum_kilobyte() cannot be used as a getter"
- if @_ < 2;
-
- return $_[0]->minimum( $_[1] << 10 );
-}
-
-sub minimum_megabyte
-{
- croak "minimum_megabyte() cannot be used as a getter"
- if @_ < 2;
-
- return $_[0]->minimum( $_[1] << 20 );
-}
-
-sub maximum_kilobyte
-{
- croak "maximum_kilobyte() cannot be used as a getter"
- if @_ < 2;
-
- return $_[0]->maximum( $_[1] << 10 );
-}
-
-sub maximum_megabyte
-{
- croak "maximum_megabyte() cannot be used as a getter"
- if @_ < 2;
-
- return $_[0]->maximum( $_[1] << 20 );
-}
-
-sub validate
-{
- my $self = $_[0];
- my $value = $self->value;
-
- return unless $self->SUPER::validate;
-
- blessed $value and
- $value->can('fh') and
- $value->can('slurp') and
- $value->can('type') or
- return $self->add_error('This is not valid file upload data');
-
- if ( $self->has_accept ) {
- my $type = $value->type;
- any { $type eq $_ } $self->accept_list or
- return $self->add_error( 'Invalid content-type "[_1]"', $type );
- }
-
- my $size = $value->can('size') ? $value->size : ( -s $value->fh );
-
- $size >= $self->minimum or
- return $self->add_error( 'File is too small (< [_1] bytes)', $self->minimum );
-
- $size <= $self->maximum or
- return $self->add_error( 'File is too big (> [_1] bytes)', $self->maximum );
-
- return $self->transform;
-}
-
-sub transform
-{
- my $self = $_[0];
-
- eval { $self->value( $self->value->slurp ) } or
- return $self->add_error('Cannot read uploaded data');
-
- return 1;
-}
-
-=head1 AUTHOR
-
-Bernhard Graf
-
-=head1 COPYRIGHT
-
-This library is free software, you can redistribute it and/or modify it under
-the same terms as Perl itself.
-
-=cut
-
-__PACKAGE__->meta->make_immutable;
-no Moose;
-1;
View
113 lib/HTML/FormHandler/Field/Upload.pm
@@ -0,0 +1,113 @@
+package HTML::FormHandler::Field::Upload;
+
+use Moose;
+use Moose::Util::TypeConstraints;
+use MooseX::AttributeHelpers;
+
+extends 'HTML::FormHandler::Field';
+
+our $VERSION = '0.01';
+
+=head1 NAME
+
+HTML::FormHandler::Field::Upload - File upload field
+
+=head1 DESCRIPTION
+
+Validates that the input is an uploaded file.
+
+=head1 DEPENDENCIES
+
+=head2 widget
+
+Widget type is 'file'
+
+=cut
+
+has '+widget' => ( default => 'upload', );
+has minimum => ( is => 'rw', isa => 'Int', default => 1 );
+has maximum => ( is => 'rw', isa => 'Int', default => 1_048_576 );
+
+sub BUILD
+{
+ for my $form ( $_[0]->form ) {
+ $form->enctype('multipart/form-data');
+ $form->http_method('post');
+ }
+}
+
+sub minimum_kilobyte
+{
+ my ( $self, $value ) = @_;
+ return $self->minimum >> 10 unless $value;
+ return $self->minimum( $value << 10 );
+}
+
+sub minimum_megabyte
+{
+ my ( $self, $value ) = @_;
+ return $self->minimum >> 20 unless $value;
+ return $self->minimum( $value << 20 );
+}
+
+sub maximum_kilobyte
+{
+ my ( $self, $value ) = @_;
+ return $self->maximum >> 10 unless $value;
+ return $self->maximum( $value << 10 );
+}
+
+sub maximum_megabyte
+{
+ my ( $self, $value ) = @_;
+ return $self->maximum >> 20 unless $value;
+ return $self->maximum( $value << 20 );
+}
+
+sub validate
+{
+ my $self = shift;
+ my $upload = $self->upload();
+
+ blessed $upload and
+ $upload->size > 0 or
+ return $self->add_error('This is not valid file upload data');
+
+ my $size = $upload->size;
+
+ $size >= $self->minimum or
+ return $self->add_error( 'File is too small (< [_1] bytes)', $self->minimum );
+
+ $size <= $self->maximum or
+ return $self->add_error( 'File is too big (> [_1] bytes)', $self->maximum );
+
+ return $upload;
+}
+
+sub upload
+{
+ my $self = shift;
+
+ return $self->form->ctx->req->upload( $self->name );
+}
+
+
+__PACKAGE__->meta->make_immutable;
+
+=head1 AUTHOR
+
+Bernhard Graf
+
+Oleg Kostyuk, cub.uanic@gmail.com
+
+and FormHandler contributors
+
+=head1 LICENSE
+
+This library is free software, you can redistribute it and/or modify it under
+the same terms as Perl itself.
+
+=cut
+
+1;
+
View
22 lib/HTML/FormHandler/Render/Simple.pm
@@ -182,16 +182,22 @@ sub render_field
return '' if $field->widget eq 'no_render';
my $rendered_field;
my $form_render = 'render_' . $field->widget;
- die "Widget method $form_render not implemented in H::F::Render::Simple"
- unless $self->can($form_render);
- $rendered_field = $self->$form_render($field);
+ if ( $self->can($form_render) ) {
+ $rendered_field = $self->$form_render($field);
+ }
+ elsif ( $field->can('render') ) {
+ $rendered_field = $field->render;
+ }
+ else {
+ die "No widget method found for '" . $field->widget . "' in H::F::Render::Simple";
+ }
my $class = '';
if ( $field->css_class || $field->has_errors ) {
my @css_class;
- push(@css_class, split( /[ ,]+/, $field->css_class )) if $field->css_class;
- push(@css_class, 'error') if $field->has_errors;
+ push( @css_class, split( /[ ,]+/, $field->css_class ) ) if $field->css_class;
+ push( @css_class, 'error' ) if $field->has_errors;
$class .= ' class="';
- $class .= join(' ' => @css_class);
+ $class .= join( ' ' => @css_class );
$class .= '"';
}
return $self->render_field_struct( $field, $rendered_field, $class );
@@ -265,7 +271,7 @@ sub render_select
$output .= ' size="' . $field->size . '"' if $field->size;
$output .= '>';
my $index = 0;
- foreach my $option ( @{$field->options} ) {
+ foreach my $option ( @{ $field->options } ) {
$output .= '<option value="' . $option->{value} . '" ';
$output .= 'id="' . $field->id . ".$index\" ";
if ( $field->fif ) {
@@ -312,7 +318,7 @@ sub render_radio_group
my $output = " <br />";
my $index = 0;
- foreach my $option ( @{$field->options} ) {
+ foreach my $option ( @{ $field->options } ) {
$output .= '<input type="radio" value="' . $option->{value} . '"';
$output .= ' name="' . $field->html_name . '" id="' . $field->id . ".$index\"";
$output .= ' checked="checked"' if $option->{value} eq $field->fif;
View
18 lib/HTML/FormHandler/Widget/Field/Upload.pm
@@ -0,0 +1,18 @@
+package HTML::FormHandler::Widget::Field::Upload;
+
+use Moose::Role;
+
+sub render
+{
+ my ( $self, $result ) = @_;
+
+ $result ||= $self->result;
+ my $output;
+ $output = '<input type="file" name="';
+ $output .= $self->html_name . '"';
+ $output .= ' id="' . $self->id . '"/>';
+ return $self->wrap_field($output);
+}
+
+no Moose::Role;
+1;
View
8 lib/HTML/FormHandler/Widget/Wrapper/None.pm
@@ -0,0 +1,8 @@
+package HTML::FormHandler::Widget::Wrapper::None;
+
+use Moose::Role;
+
+sub wrap_field { $_[2] }
+
+no Moose::Role;
+1;

0 comments on commit 6dd53e0

Please sign in to comment.