Skip to content

Commit

Permalink
make + unnecessary in front of field name space types
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed May 14, 2010
1 parent b0d24b5 commit 9492490
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 46 deletions.
67 changes: 23 additions & 44 deletions lib/HTML/FormHandler/BuildFields.pm
Expand Up @@ -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
Expand Down
9 changes: 7 additions & 2 deletions lib/HTML/FormHandler/Manual/Intro.pod
Expand Up @@ -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"
Expand Down
3 changes: 3 additions & 0 deletions t/xt/load_field.t
Expand Up @@ -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' );

}

Expand All @@ -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 );
Expand All @@ -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;
Expand Down

0 comments on commit 9492490

Please sign in to comment.