From 94924907306f0997ef29ab0e9f0837f67b920c70 Mon Sep 17 00:00:00 2001 From: Gerda Shank Date: Fri, 14 May 2010 14:40:14 -0400 Subject: [PATCH] make + unnecessary in front of field name space types --- lib/HTML/FormHandler/BuildFields.pm | 67 +++++++++------------------ lib/HTML/FormHandler/Manual/Intro.pod | 9 +++- t/xt/load_field.t | 3 ++ 3 files changed, 33 insertions(+), 46 deletions(-) diff --git a/lib/HTML/FormHandler/BuildFields.pm b/lib/HTML/FormHandler/BuildFields.pm index 744afd88..7d3c15d7 100644 --- a/lib/HTML/FormHandler/BuildFields.pm +++ b/lib/HTML/FormHandler/BuildFields.pm @@ -199,53 +199,32 @@ sub _make_field { $field_attr->{name} = $name = $1; $do_update = 1; } - - my $class; - if( $type =~ s/^\+// ) { - # type prefixed with '+', not a built-in - if( ref $self->field_name_space eq 'ARRAY' ) { - my $loaded; - foreach my $ns (@{$self->field_name_space}) { - $class = $ns . "::" . $type; - try { - Class::MOP::load_class($class); - $loaded++; - }; - last if $loaded; - } - die "Could not load field class '$type' for field '$name'" - unless $loaded; - } - elsif ( $self->field_name_space ) { - $class = $self->field_name_space . "::" . $type; - Class::MOP::load_class($class) or - die "Could not load field class '$class' for field '$name'"; - } - else { - $class = $type; - Class::MOP::load_class($class) or - die "Could not load field class '$class' for field '$name'"; - } + my $field_ns = $self->field_name_space; + my @field_name_space = ref $field_ns eq 'ARRAY' ? @$field_ns : $field_ns; + my @classes; + # '+'-prefixed fields could be full namespaces + if ( $type =~ s/^\+// ) + { + push @classes, $type; } - else { - # built-in. look in HTML::FormHandler::Field - # and HTML::FormHandlerX::Field - $class = 'HTML::FormHandler::Field::' . $type; - my @errors; - try { - Class::MOP::load_class($class); - } catch { - $class = 'HTML::FormHandlerX::Field::' . $type; - push @errors, $_; - try { - Class::MOP::load_class($class); - } catch { - push @errors, $_; - die "Could not load field class '$type' for field '$name'.". - join("\n[+] ", @errors); - }; + foreach my $ns ( @field_name_space, 'HTML::FormHandler::Field', 'HTML::FormHandlerX::Field' ) + { + push @classes, $ns . "::" . $type; + } + # look for Field in possible namespaces + my $loaded; + my $class; + foreach my $try ( @classes ) { + try { + Class::MOP::load_class($try); + $loaded++; + $class = $try; }; + last if $loaded; } + die "Could not load field class '$type' for field '$name'" + unless $loaded; + $field_attr->{form} = $self->form if $self->form; # parent and name correction for names with dots diff --git a/lib/HTML/FormHandler/Manual/Intro.pod b/lib/HTML/FormHandler/Manual/Intro.pod index 75896c35..85e9f5fc 100644 --- a/lib/HTML/FormHandler/Manual/Intro.pod +++ b/lib/HTML/FormHandler/Manual/Intro.pod @@ -528,13 +528,18 @@ Field errors are in C<< $field->errors >>. The fields are assumed to be in the HTML::FormHandler::Field name space. If you want to explicitly list the field's package, prefix it -with a plus sign. The field name space for "+" prefixed fields can -be set with the form's "field_name_space" attribute: +with a plus sign. The field_name_space plus the default name spaces +'HTML::FormHandler::Field' and 'HTML::FormHandlerX::Field' will be +searched for fields. has '+field_name_space' => ( default => 'MyApp::Form::Field' ); has_field 'name' => ( type => 'Text' ); # HTML::FormHandler::Field::Text + has_field 'name' => ( type => '+My::FieldType' ); # My::Fieldtype has_field 'foo' => ( type => +Foo' ); # MyApp::Form::Field::Foo + or + has_field 'foo' => ( type => 'Foo' ); # MyApp::Form::Field::Foo + The most basic type is "Text", which takes a single scalar value. (If the type of a field is not specified, it will be set to 'Text'.) A "Select" diff --git a/t/xt/load_field.t b/t/xt/load_field.t index 473334c4..af826d88 100644 --- a/t/xt/load_field.t +++ b/t/xt/load_field.t @@ -15,6 +15,7 @@ use lib 't/lib'; has_field 'field_one' => ( type => '+AltText', another_attribute => 'one' ); has_field 'field_two' => ( type => '+AltText', another_attribute => 'two' ); has_field 'field_three' => ( type => '+AltText', another_attribute => 'three' ); + has_field 'field_four' => ( type => 'AltText', another_attribute => 'four' ); } @@ -25,6 +26,7 @@ my $params = { field_one => 'one two three four', field_two => 'one three four', field_three => 'one three four', + field_four => 'four', }; $form->process( $params ); @@ -38,6 +40,7 @@ is( $form->field('field_two')->errors->[0], 'Fails AltText validation', 'get error message' ); ok( !$form->field('field_three')->has_errors, 'field three has no error'); +ok( !$form->field('field_four')->has_errors, 'field four has no error'); { package Field::Text;